diff --git a/modules/inventory_transaction/service/inventory_transaction_service.go b/modules/inventory_transaction/service/inventory_transaction_service.go index 64cbc83..117292d 100644 --- a/modules/inventory_transaction/service/inventory_transaction_service.go +++ b/modules/inventory_transaction/service/inventory_transaction_service.go @@ -7,8 +7,10 @@ import ( dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_transaction/dto" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_transaction/query" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_transaction/repository" + "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" "github.com/Caknoooo/go-gin-clean-starter/pkg/utils" "github.com/google/uuid" + "github.com/sirupsen/logrus" "gorm.io/gorm" ) @@ -23,6 +25,7 @@ type InventoryTransactionService interface { type inventoryTransactionService struct { db *gorm.DB inventoryTransactionRepo repository.InventoryTransactionRepository + log *logrus.Logger } func (s *inventoryTransactionService) Create(ctx context.Context, req dtodomain.InventoryTransactionCreateRequest) (dtodomain.InventoryTransactionResponse, error) { @@ -77,6 +80,7 @@ func (s *inventoryTransactionService) Create(ctx context.Context, req dtodomain. InvReceiptID: invReceiptUUID, InvIssueID: invIssueUUID, InvMoveID: invMoveUUID, + FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE), } created, err := s.inventoryTransactionRepo.Create(ctx, tx, entity) if err != nil { @@ -84,6 +88,12 @@ func (s *inventoryTransactionService) Create(ctx context.Context, req dtodomain. return dtodomain.InventoryTransactionResponse{}, err } tx.Commit() + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": constants.CREATE, + "entity": "inventory_transaction", + "entity_id": created.ID.String(), + }).Info("Inventory Transaction created") result, err := s.inventoryTransactionRepo.GetById(ctx, nil, created.ID.String()) if err != nil { return dtodomain.InventoryTransactionResponse{}, err @@ -126,6 +136,9 @@ func (s *inventoryTransactionService) Update(ctx context.Context, req dtodomain. tx.Rollback() return dtodomain.InventoryTransactionResponse{}, err } + + before := entity + if req.TransactionType != "" { entity.TransactionType = req.TransactionType } @@ -163,12 +176,23 @@ func (s *inventoryTransactionService) Update(ctx context.Context, req dtodomain. entity.InvMoveID = &invMoveUUID } } + entity.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE) updated, err := s.inventoryTransactionRepo.Update(ctx, tx, entity) if err != nil { tx.Rollback() return dtodomain.InventoryTransactionResponse{}, err } tx.Commit() + + changes := utils.GetChangedFields(before, updated) + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": constants.UPDATE, + "entity": "inventory_transaction", + "entity_id": updated.ID.String(), + "changes": changes, + }).Info("Inventory Transaction updated") + result, err := s.inventoryTransactionRepo.GetById(ctx, nil, updated.ID.String()) if err != nil { return dtodomain.InventoryTransactionResponse{}, err @@ -183,17 +207,37 @@ func (s *inventoryTransactionService) Delete(ctx context.Context, inventoryTrans tx.Rollback() } }() + + invTransaction, err := s.inventoryTransactionRepo.GetById(ctx, tx, inventoryTransactionId) + if err != nil { + tx.Rollback() + return err + } + invTransaction.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE) + if _, err := s.inventoryTransactionRepo.Update(ctx, tx, invTransaction); err != nil { + tx.Rollback() + return err + } if err := s.inventoryTransactionRepo.Delete(ctx, tx, inventoryTransactionId); err != nil { tx.Rollback() return err } tx.Commit() + + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": constants.DELETE, + "entity": "inventory_transaction", + "entity_id": inventoryTransactionId, + }).Info("Inventory Transaction deleted") + return nil } -func NewInventoryTransactionService(db *gorm.DB, repo repository.InventoryTransactionRepository) InventoryTransactionService { +func NewInventoryTransactionService(db *gorm.DB, repo repository.InventoryTransactionRepository, log *logrus.Logger) InventoryTransactionService { return &inventoryTransactionService{ db: db, inventoryTransactionRepo: repo, + log: log, } } diff --git a/providers/core.go b/providers/core.go index 4d47387..81dd0d4 100644 --- a/providers/core.go +++ b/providers/core.go @@ -194,7 +194,7 @@ func RegisterDependencies(injector *do.Injector) { inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository, sequenceServ, log) inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ, log) inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository, log) - inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository) + inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository, log) quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository) // Controller