feat: integrate logging and audit trail in inventory issue service

This commit is contained in:
Habib Fatkhul Rohman 2025-12-04 13:20:14 +07:00
parent e6c025f675
commit 41dacd0e3e
2 changed files with 44 additions and 3 deletions

View File

@ -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/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/repository" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/repository"
sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service" 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" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils" "github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/sirupsen/logrus"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -31,6 +33,7 @@ type inventoryIssueService struct {
issueRepo repository.InventoryIssueRepository issueRepo repository.InventoryIssueRepository
issueLineRepo repository.InventoryIssueLineRepository issueLineRepo repository.InventoryIssueLineRepository
sequenceService sequenceservice.SequenceService sequenceService sequenceservice.SequenceService
log *logrus.Logger
} }
// GetLinesByIssueId implements InventoryIssueService. // GetLinesByIssueId implements InventoryIssueService.
@ -42,8 +45,8 @@ func (s *inventoryIssueService) GetLinesByIssueId(ctx context.Context, issueId s
return dtodomain.ToInventoryIssueLineResponses(lines), nil return dtodomain.ToInventoryIssueLineResponses(lines), nil
} }
func NewInventoryIssueService(db *gorm.DB, issueRepo repository.InventoryIssueRepository, issueLineRepo repository.InventoryIssueLineRepository, sequenceService sequenceservice.SequenceService) InventoryIssueService { 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} 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) { 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, InvRequestID: invRequestUUID,
ClientID: clientUUID, ClientID: clientUUID,
Status: req.Status, Status: req.Status,
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
} }
created, err := s.issueRepo.Create(ctx, tx, issue) created, err := s.issueRepo.Create(ctx, tx, issue)
if err != nil { if err != nil {
@ -129,6 +133,12 @@ func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.Invent
} }
} }
tx.Commit() 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()) result, err := s.issueRepo.GetById(ctx, nil, created.ID.String())
if err != nil { if err != nil {
return dtodomain.InventoryIssueResponse{}, err return dtodomain.InventoryIssueResponse{}, err
@ -171,19 +181,34 @@ func (s *inventoryIssueService) Update(ctx context.Context, req dtodomain.Invent
tx.Rollback() tx.Rollback()
return dtodomain.InventoryIssueResponse{}, err return dtodomain.InventoryIssueResponse{}, err
} }
before := issue
issue.DocumentDate = utils.StringToDateTime(req.DocumentDate) issue.DocumentDate = utils.StringToDateTime(req.DocumentDate)
issue.DueDate = utils.StringToDateTime(req.DueDate) issue.DueDate = utils.StringToDateTime(req.DueDate)
issue.Status = req.Status issue.Status = req.Status
issue.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
updated, err := s.issueRepo.Update(ctx, tx, issue) updated, err := s.issueRepo.Update(ctx, tx, issue)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return dtodomain.InventoryIssueResponse{}, err return dtodomain.InventoryIssueResponse{}, err
} }
tx.Commit() tx.Commit()
result, err := s.issueRepo.GetById(ctx, nil, updated.ID.String()) result, err := s.issueRepo.GetById(ctx, nil, updated.ID.String())
if err != nil { if err != nil {
return dtodomain.InventoryIssueResponse{}, err 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 return dtodomain.ToInventoryIssueResponse(result), nil
} }
@ -194,11 +219,27 @@ func (s *inventoryIssueService) Delete(ctx context.Context, id string) error {
tx.Rollback() 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 { if err := s.issueRepo.Delete(ctx, tx, id); err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
tx.Commit() 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 return nil
} }

View File

@ -190,7 +190,7 @@ func RegisterDependencies(injector *do.Injector) {
inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ) inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ)
assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository) assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository)
inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ) 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) inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository, sequenceServ)
inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ) inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ)
inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository) inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository)