From 41dacd0e3ec43ea6af51efca35c06c03362b5045 Mon Sep 17 00:00:00 2001 From: Habib Fatkhul Rohman Date: Thu, 4 Dec 2025 13:20:14 +0700 Subject: [PATCH] feat: integrate logging and audit trail in inventory issue service --- .../service/inventory_issue_service.go | 45 ++++++++++++++++++- providers/core.go | 2 +- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/modules/inventory_issue/service/inventory_issue_service.go b/modules/inventory_issue/service/inventory_issue_service.go index 8369bb2..911eaca 100644 --- a/modules/inventory_issue/service/inventory_issue_service.go +++ b/modules/inventory_issue/service/inventory_issue_service.go @@ -8,9 +8,11 @@ import ( "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/query" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/repository" sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service" + "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" "github.com/Caknoooo/go-gin-clean-starter/pkg/utils" "github.com/google/uuid" + "github.com/sirupsen/logrus" "gorm.io/gorm" ) @@ -31,6 +33,7 @@ type inventoryIssueService struct { issueRepo repository.InventoryIssueRepository issueLineRepo repository.InventoryIssueLineRepository sequenceService sequenceservice.SequenceService + log *logrus.Logger } // GetLinesByIssueId implements InventoryIssueService. @@ -42,8 +45,8 @@ func (s *inventoryIssueService) GetLinesByIssueId(ctx context.Context, issueId s return dtodomain.ToInventoryIssueLineResponses(lines), nil } -func NewInventoryIssueService(db *gorm.DB, issueRepo repository.InventoryIssueRepository, issueLineRepo repository.InventoryIssueLineRepository, sequenceService sequenceservice.SequenceService) InventoryIssueService { - return &inventoryIssueService{db: db, issueRepo: issueRepo, issueLineRepo: issueLineRepo, sequenceService: sequenceService} +func NewInventoryIssueService(db *gorm.DB, issueRepo repository.InventoryIssueRepository, issueLineRepo repository.InventoryIssueLineRepository, sequenceService sequenceservice.SequenceService, log *logrus.Logger) InventoryIssueService { + return &inventoryIssueService{db: db, issueRepo: issueRepo, issueLineRepo: issueLineRepo, sequenceService: sequenceService, log: log} } func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.InventoryIssueCreateRequest) (dtodomain.InventoryIssueResponse, error) { @@ -85,6 +88,7 @@ func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.Invent InvRequestID: invRequestUUID, ClientID: clientUUID, Status: req.Status, + FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE), } created, err := s.issueRepo.Create(ctx, tx, issue) if err != nil { @@ -129,6 +133,12 @@ func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.Invent } } tx.Commit() + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": "create", + "entity": "inventory_issue", + "entity_id": created.ID.String(), + }).Info("Inventory Issue created") result, err := s.issueRepo.GetById(ctx, nil, created.ID.String()) if err != nil { return dtodomain.InventoryIssueResponse{}, err @@ -171,19 +181,34 @@ func (s *inventoryIssueService) Update(ctx context.Context, req dtodomain.Invent tx.Rollback() return dtodomain.InventoryIssueResponse{}, err } + + before := issue + issue.DocumentDate = utils.StringToDateTime(req.DocumentDate) issue.DueDate = utils.StringToDateTime(req.DueDate) issue.Status = req.Status + issue.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE) updated, err := s.issueRepo.Update(ctx, tx, issue) if err != nil { tx.Rollback() return dtodomain.InventoryIssueResponse{}, err } tx.Commit() + result, err := s.issueRepo.GetById(ctx, nil, updated.ID.String()) if err != nil { return dtodomain.InventoryIssueResponse{}, err } + + changes := utils.GetChangedFields(before, result) + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": "update", + "entity": "inventory_issue", + "entity_id": id, + "changes": changes, + }).Info("Inventory Issue updated") + return dtodomain.ToInventoryIssueResponse(result), nil } @@ -194,11 +219,27 @@ func (s *inventoryIssueService) Delete(ctx context.Context, id string) error { tx.Rollback() } }() + issue, err := s.issueRepo.GetById(ctx, tx, id) + if err != nil { + tx.Rollback() + return err + } + issue.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE) + if _, err := s.issueRepo.Update(ctx, tx, issue); err != nil { + tx.Rollback() + return err + } if err := s.issueRepo.Delete(ctx, tx, id); err != nil { tx.Rollback() return err } tx.Commit() + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": "delete", + "entity": "inventory_issue", + "entity_id": id, + }).Info("Inventory Issue deleted") return nil } diff --git a/providers/core.go b/providers/core.go index 73804b7..3aa41bf 100644 --- a/providers/core.go +++ b/providers/core.go @@ -190,7 +190,7 @@ func RegisterDependencies(injector *do.Injector) { inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ) assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository) inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ) - inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository, sequenceServ) + inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository, sequenceServ, log) inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository, sequenceServ) inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ) inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository)