feat: integrate logging and audit trail in inventory movement service

This commit is contained in:
Habib Fatkhul Rohman 2025-12-04 13:39:12 +07:00
parent 41dacd0e3e
commit f36380873c
2 changed files with 91 additions and 12 deletions

View File

@ -8,8 +8,10 @@ import (
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/query" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/repository" "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/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"
"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"
) )
@ -30,6 +32,7 @@ type inventoryMovementService struct {
movementRepo repository.InventoryMovementRepository movementRepo repository.InventoryMovementRepository
movementLineRepo repository.InventoryMovementLineRepository movementLineRepo repository.InventoryMovementLineRepository
sequenceService sequenceservice.SequenceService sequenceService sequenceservice.SequenceService
log *logrus.Logger
} }
// GetLinesByMovementId implements InventoryMovementService. // GetLinesByMovementId implements InventoryMovementService.
@ -73,6 +76,7 @@ func (s *inventoryMovementService) Create(ctx context.Context, req dtodomain.Inv
Status: req.Status, Status: req.Status,
SourceLocationID: uuid.MustParse(req.SourceLocationID), SourceLocationID: uuid.MustParse(req.SourceLocationID),
DestinationLocationID: uuid.MustParse(req.DestinationLocationID), DestinationLocationID: uuid.MustParse(req.DestinationLocationID),
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
} }
created, err := s.movementRepo.Create(ctx, tx, movement) created, err := s.movementRepo.Create(ctx, tx, movement)
if err != nil { if err != nil {
@ -116,6 +120,14 @@ func (s *inventoryMovementService) Create(ctx context.Context, req dtodomain.Inv
if err != nil { if err != nil {
return dtodomain.InventoryMovementResponse{}, err return dtodomain.InventoryMovementResponse{}, err
} }
s.log.WithFields(logrus.Fields{
"user_id": utils.GetUserID(ctx),
"action": "create",
"entity": "inventory_movement",
"entity_id": created.ID.String(),
}).Info("Inventory Movement created")
return dtodomain.ToInventoryMovementResponse(result), nil return dtodomain.ToInventoryMovementResponse(result), nil
} }
@ -154,12 +166,14 @@ func (s *inventoryMovementService) Update(ctx context.Context, req dtodomain.Inv
tx.Rollback() tx.Rollback()
return dtodomain.InventoryMovementResponse{}, err return dtodomain.InventoryMovementResponse{}, err
} }
// if req.MovementNumber != "" {
// movement.MovementNumber = req.MovementNumber before := movement
// }
movement.MovementDate = utils.StringToDateTime(req.MovementDate) movement.MovementDate = utils.StringToDateTime(req.MovementDate)
movement.MovementType = req.MovementType movement.MovementType = req.MovementType
movement.Status = req.Status movement.Status = req.Status
movement.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
updated, err := s.movementRepo.Update(ctx, tx, movement) updated, err := s.movementRepo.Update(ctx, tx, movement)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
@ -170,6 +184,15 @@ func (s *inventoryMovementService) Update(ctx context.Context, req dtodomain.Inv
if err != nil { if err != nil {
return dtodomain.InventoryMovementResponse{}, err return dtodomain.InventoryMovementResponse{}, err
} }
changes := utils.GetChangedFields(before, result)
s.log.WithFields(logrus.Fields{
"user_id": utils.GetUserID(ctx),
"action": "update",
"entity": "inventory_movement",
"entity_id": id,
"changes": changes,
}).Info("Inventory Movement updated")
return dtodomain.ToInventoryMovementResponse(result), nil return dtodomain.ToInventoryMovementResponse(result), nil
} }
@ -180,11 +203,29 @@ func (s *inventoryMovementService) Delete(ctx context.Context, id string) error
tx.Rollback() tx.Rollback()
} }
}() }()
movement, err := s.movementRepo.GetById(ctx, tx, id)
if err != nil {
tx.Rollback()
return err
}
movement.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
if _, err := s.movementRepo.Update(ctx, tx, movement); err != nil {
tx.Rollback()
return err
}
if err := s.movementRepo.Delete(ctx, tx, id); err != nil { if err := s.movementRepo.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_movement",
"entity_id": id,
}).Info("Inventory Movement deleted")
return nil return nil
} }
@ -211,11 +252,19 @@ func (s *inventoryMovementService) CreateLine(ctx context.Context, movementId st
StorageID: uuid.MustParse(req.StorageID), StorageID: uuid.MustParse(req.StorageID),
ClientID: clientLineUUID, ClientID: clientLineUUID,
Status: req.Status, Status: req.Status,
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
} }
created, err := s.movementLineRepo.Create(ctx, nil, line) created, err := s.movementLineRepo.Create(ctx, nil, line)
if err != nil { if err != nil {
return dtodomain.InventoryMovementLineResponse{}, err return dtodomain.InventoryMovementLineResponse{}, err
} }
s.log.WithFields(logrus.Fields{
"user_id": utils.GetUserID(ctx),
"action": "create",
"entity": "inventory_movement_line",
"entity_id": created.ID.String(),
}).Info("Inventory Movement Line created")
return dtodomain.ToInventoryMovementLineResponse(created), nil return dtodomain.ToInventoryMovementLineResponse(created), nil
} }
@ -224,6 +273,7 @@ func (s *inventoryMovementService) UpdateLine(ctx context.Context, lineId string
if err != nil { if err != nil {
return dtodomain.InventoryMovementLineResponse{}, err return dtodomain.InventoryMovementLineResponse{}, err
} }
before := line
if req.MovedQuantity != nil { if req.MovedQuantity != nil {
line.MovedQuantity = *req.MovedQuantity line.MovedQuantity = *req.MovedQuantity
} }
@ -234,18 +284,47 @@ func (s *inventoryMovementService) UpdateLine(ctx context.Context, lineId string
if err != nil { if err != nil {
return dtodomain.InventoryMovementLineResponse{}, err return dtodomain.InventoryMovementLineResponse{}, err
} }
changes := utils.GetChangedFields(before, updated)
s.log.WithFields(logrus.Fields{
"user_id": utils.GetUserID(ctx),
"action": "update",
"entity": "inventory_movement_line",
"entity_id": lineId,
"changes": changes,
}).Info("Inventory Movement Line updated")
return dtodomain.ToInventoryMovementLineResponse(updated), nil return dtodomain.ToInventoryMovementLineResponse(updated), nil
} }
func (s *inventoryMovementService) DeleteLine(ctx context.Context, lineId string) error { func (s *inventoryMovementService) DeleteLine(ctx context.Context, lineId string) error {
return s.movementLineRepo.Delete(ctx, nil, lineId) line, err := s.movementLineRepo.GetById(ctx, nil, lineId)
if err != nil {
return err
}
line.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
if _, err := s.movementLineRepo.Update(ctx, nil, line); err != nil {
return err
}
result := s.movementLineRepo.Delete(ctx, nil, lineId)
if result != nil {
return result
} }
func NewInventoryMovementService(db *gorm.DB, movementRepo repository.InventoryMovementRepository, movementLineRepo repository.InventoryMovementLineRepository, sequenceService sequenceservice.SequenceService) InventoryMovementService { s.log.WithFields(logrus.Fields{
"user_id": utils.GetUserID(ctx),
"action": "delete",
"entity": "inventory_movement_line",
"entity_id": lineId,
}).Info("Inventory Movement Line deleted")
return nil
}
func NewInventoryMovementService(db *gorm.DB, movementRepo repository.InventoryMovementRepository, movementLineRepo repository.InventoryMovementLineRepository, sequenceService sequenceservice.SequenceService, log *logrus.Logger) InventoryMovementService {
return &inventoryMovementService{ return &inventoryMovementService{
db: db, db: db,
movementRepo: movementRepo, movementRepo: movementRepo,
movementLineRepo: movementLineRepo, movementLineRepo: movementLineRepo,
sequenceService: sequenceService, sequenceService: sequenceService,
log: log,
} }
} }

View File

@ -192,7 +192,7 @@ func RegisterDependencies(injector *do.Injector) {
inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ) inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ)
inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository, sequenceServ, log) 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, log)
inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository) inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository)
inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository) inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository)
quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository) quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository)