Merge branch 'feat/BE/US-284/Report-counting-sapronak' into 'feat/BE/Sprint-6'

Feat/be/us 284/report counting sapronak

See merge request mbugroup/lti-api!94
This commit is contained in:
Hafizh A. Y.
2025-12-16 04:15:43 +00:00
16 changed files with 1459 additions and 38 deletions
@@ -101,13 +101,22 @@ func (u *ProjectFlockKandangController) Closing(c *fiber.Ctx) error {
return err
}
detail, availableQtys, productWarehouses, err := u.ProjectFlockKandangService.GetOne(c, result.Id)
if err != nil {
return err
}
detailDTO := dto.ToProjectFlockKandangDetailDTOWithAvailableQty(*detail, availableQtys, productWarehouses)
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Status closing kandang diperbarui",
// Data: dto.ProjectFlockKandangDetailDTO(*result),
Data: result,
Data: fiber.Map{
"detail": detailDTO,
"approval": detailDTO.Approval,
},
})
}
@@ -432,16 +432,30 @@ func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validati
}
if s.ApprovalSvc != nil {
closeAction := entity.ApprovalActionApproved
if _, aerr := s.ApprovalSvc.CreateApproval(
c.Context(),
utils.ApprovalWorkflowProjectFlockKandang,
id,
utils.ProjectFlockKandangStepClosed,
&closeAction,
actorID,
nil,
); aerr != nil {
return nil, aerr
// Hindari duplikasi jika approval terakhir sudah Closed + Approved
latestPFK, lerr := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowProjectFlockKandang, id, nil)
if lerr != nil {
return nil, lerr
}
shouldCreate := true
if latestPFK != nil &&
latestPFK.StepNumber == uint16(utils.ProjectFlockKandangStepClosed) &&
latestPFK.Action != nil && *latestPFK.Action == closeAction {
shouldCreate = false
}
if shouldCreate {
if _, aerr := s.ApprovalSvc.CreateApproval(
c.Context(),
utils.ApprovalWorkflowProjectFlockKandang,
id,
utils.ProjectFlockKandangStepClosed,
&closeAction,
actorID,
nil,
); aerr != nil {
return nil, aerr
}
}
// Jika semua kandang dalam project sudah ditutup, set approval project flock ke SELESAI.
@@ -499,6 +513,34 @@ func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validati
return nil, err
}
}
if s.ApprovalSvc != nil {
reopenAction := entity.ApprovalActionUpdated
// Hindari duplikasi jika approval terakhir sudah Disetujui + Updated
latestPFK, lerr := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowProjectFlockKandang, id, nil)
if lerr != nil {
return nil, lerr
}
shouldCreate := true
if latestPFK != nil &&
latestPFK.StepNumber == uint16(utils.ProjectFlockKandangStepDisetujui) &&
latestPFK.Action != nil && *latestPFK.Action == reopenAction {
shouldCreate = false
}
if shouldCreate {
if _, aerr := s.ApprovalSvc.CreateApproval(
c.Context(),
utils.ApprovalWorkflowProjectFlockKandang,
id,
utils.ProjectFlockKandangStepDisetujui,
&reopenAction,
actorID,
nil,
); aerr != nil && !errors.Is(aerr, gorm.ErrDuplicatedKey) {
return nil, aerr
}
}
}
default:
return nil, fiber.NewError(fiber.StatusBadRequest, "action harus close atau unclose")
}
@@ -28,6 +28,7 @@ type ProjectFlockKandangRepository interface {
ProjectPeriodsByProjectIDs(ctx context.Context, projectIDs []uint) (map[uint]int, error)
HasOpenNewerPeriod(ctx context.Context, kandangID uint, currentPeriod int, excludeID *uint) (bool, error)
WithTx(tx *gorm.DB) ProjectFlockKandangRepository
DB() *gorm.DB
IdExists(ctx context.Context, id uint) (bool, error)
}