From e6b19a2478872082e292a62c3cc1fe8aeabb7886 Mon Sep 17 00:00:00 2001 From: Habib Fatkhul Rohman Date: Thu, 4 Dec 2025 15:00:43 +0700 Subject: [PATCH] feat: integrate logging and audit trail in inventory storage service --- .../service/inventory_storage_service.go | 50 ++++++++++++++++++- providers/core.go | 2 +- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/modules/inventory_storage/service/inventory_storage_service.go b/modules/inventory_storage/service/inventory_storage_service.go index 051ff50..57b015e 100644 --- a/modules/inventory_storage/service/inventory_storage_service.go +++ b/modules/inventory_storage/service/inventory_storage_service.go @@ -7,7 +7,10 @@ import ( dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/dto" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/query" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/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" ) @@ -22,6 +25,7 @@ type InventoryStorageService interface { type inventoryStorageService struct { db *gorm.DB inventoryStorageRepo repository.InventoryStorageRepository + log *logrus.Logger } func (s *inventoryStorageService) Create(ctx context.Context, req dtodomain.InventoryStorageCreateRequest) (dtodomain.InventoryStorageResponse, error) { @@ -70,7 +74,7 @@ func (s *inventoryStorageService) Create(ctx context.Context, req dtodomain.Inve } else { InvReceiptID = uuid.Nil } - + inventoryStorage := entities.InventoryStorageEntity{ ProductID: productID, AisleID: aisleID, @@ -80,6 +84,7 @@ func (s *inventoryStorageService) Create(ctx context.Context, req dtodomain.Inve InvReceiptID: InvReceiptID, OnHandQuantity: req.OnHandQuantity, AvailableQuantity: req.AvailableQuantity, + FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE), } created, err := s.inventoryStorageRepo.Create(ctx, tx, inventoryStorage) if err != nil { @@ -87,6 +92,14 @@ func (s *inventoryStorageService) Create(ctx context.Context, req dtodomain.Inve return dtodomain.InventoryStorageResponse{}, err } tx.Commit() + + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": constants.CREATE, + "entity": "inventory_storage", + "entity_id": created.ID.String(), + }).Info("Inventory Storage created") + result, err := s.inventoryStorageRepo.GetById(ctx, nil, created.ID.String()) if err != nil { return dtodomain.InventoryStorageResponse{}, err @@ -129,8 +142,10 @@ func (s *inventoryStorageService) Update(ctx context.Context, req dtodomain.Inve tx.Rollback() return dtodomain.InventoryStorageResponse{}, err } + before := inventoryStorage inventoryStorage.OnHandQuantity = req.OnHandQuantity inventoryStorage.AvailableQuantity = req.AvailableQuantity + inventoryStorage.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE) updated, err := s.inventoryStorageRepo.Update(ctx, tx, inventoryStorage) if err != nil { tx.Rollback() @@ -141,6 +156,16 @@ func (s *inventoryStorageService) Update(ctx context.Context, req dtodomain.Inve if err != nil { return dtodomain.InventoryStorageResponse{}, err } + + changes := utils.GetChangedFields(before, inventoryStorage) + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": constants.UPDATE, + "entity": "inventory_storage", + "entity_id": inventoryStorageId, + "changes": changes, + }).Info("Inventory Storage updated") + return dtodomain.ToInventoryStorageResponse(result), nil } @@ -151,17 +176,38 @@ func (s *inventoryStorageService) Delete(ctx context.Context, inventoryStorageId tx.Rollback() } }() + + invStorage, err := s.inventoryStorageRepo.GetById(ctx, tx, inventoryStorageId) + if err != nil { + tx.Rollback() + return err + } + invStorage.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE) + if _, err := s.inventoryStorageRepo.Update(ctx, tx, invStorage); err != nil { + tx.Rollback() + return err + } + if err := s.inventoryStorageRepo.Delete(ctx, tx, inventoryStorageId); err != nil { tx.Rollback() return err } tx.Commit() + + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": constants.DELETE, + "entity": "inventory_storage", + "entity_id": inventoryStorageId, + }).Info("Inventory Storage deleted") + return nil } -func NewInventoryStorageService(db *gorm.DB, inventoryStorageRepo repository.InventoryStorageRepository) InventoryStorageService { +func NewInventoryStorageService(db *gorm.DB, inventoryStorageRepo repository.InventoryStorageRepository, log *logrus.Logger) InventoryStorageService { return &inventoryStorageService{ db: db, inventoryStorageRepo: inventoryStorageRepo, + log: log, } } diff --git a/providers/core.go b/providers/core.go index d4d4800..4d47387 100644 --- a/providers/core.go +++ b/providers/core.go @@ -193,7 +193,7 @@ func RegisterDependencies(injector *do.Injector) { inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository, sequenceServ, log) inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository, sequenceServ, log) inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ, log) - inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository) + inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository, log) inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository) quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository)