ini ar fifo

This commit is contained in:
giovanni
2026-05-29 00:59:42 +07:00
parent 2da476b276
commit 8da2b7a3ab
21 changed files with 1010 additions and 209 deletions
@@ -32,12 +32,14 @@ type paymentService struct {
Validate *validator.Validate
Repository repository.PaymentRepository
ApprovalSvc commonSvc.ApprovalService
FifoPaymentSvc commonSvc.FifoPaymentService
approvalWorkflow approvalutils.ApprovalWorkflowKey
}
func NewPaymentService(
repo repository.PaymentRepository,
approvalSvc commonSvc.ApprovalService,
fifoPaymentSvc commonSvc.FifoPaymentService,
validate *validator.Validate,
) PaymentService {
return &paymentService{
@@ -45,6 +47,7 @@ func NewPaymentService(
Validate: validate,
Repository: repo,
ApprovalSvc: approvalSvc,
FifoPaymentSvc: fifoPaymentSvc,
approvalWorkflow: utils.ApprovalWorkflowPayment,
}
}
@@ -159,6 +162,12 @@ func (s *paymentService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
}
}
if s.FifoPaymentSvc != nil {
if err := s.FifoPaymentSvc.ReallocateForParty(c.Context(), dbTransaction, createBody.PartyType, createBody.PartyId); err != nil {
return err
}
}
return nil
})
if err != nil {
@@ -251,7 +260,46 @@ func (s paymentService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
return s.GetOne(c, id)
}
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
// Snapshot party lama untuk reallocate kalau party baru berbeda.
existing, err := s.Repository.GetByID(c.Context(), id, nil)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Payment not found")
}
if err != nil {
s.Log.Errorf("Failed get payment for update: %+v", err)
return nil, err
}
oldPartyType := existing.PartyType
oldPartyID := existing.PartyId
newPartyType := oldPartyType
newPartyID := oldPartyID
if v, ok := updateBody["party_type"].(string); ok {
newPartyType = v
}
if v, ok := updateBody["party_id"].(uint); ok {
newPartyID = v
}
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
paymentRepoTx := repository.NewPaymentRepository(tx)
if err := paymentRepoTx.PatchOne(c.Context(), id, updateBody, nil); err != nil {
return err
}
if s.FifoPaymentSvc != nil {
if err := s.FifoPaymentSvc.ReallocateForParty(c.Context(), tx, newPartyType, newPartyID); err != nil {
return err
}
if oldPartyType != newPartyType || oldPartyID != newPartyID {
if err := s.FifoPaymentSvc.ReallocateForParty(c.Context(), tx, oldPartyType, oldPartyID); err != nil {
return err
}
}
}
return nil
})
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Payment not found")
}