feat: integrate logging and audit trail in inventory storage service

This commit is contained in:
Habib Fatkhul Rohman 2025-12-04 15:00:43 +07:00
parent bcc73a0f99
commit e6b19a2478
2 changed files with 49 additions and 3 deletions

View File

@ -7,7 +7,10 @@ import (
dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/dto" 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/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/repository" "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/google/uuid"
"github.com/sirupsen/logrus"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -22,6 +25,7 @@ type InventoryStorageService interface {
type inventoryStorageService struct { type inventoryStorageService struct {
db *gorm.DB db *gorm.DB
inventoryStorageRepo repository.InventoryStorageRepository inventoryStorageRepo repository.InventoryStorageRepository
log *logrus.Logger
} }
func (s *inventoryStorageService) Create(ctx context.Context, req dtodomain.InventoryStorageCreateRequest) (dtodomain.InventoryStorageResponse, error) { 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 { } else {
InvReceiptID = uuid.Nil InvReceiptID = uuid.Nil
} }
inventoryStorage := entities.InventoryStorageEntity{ inventoryStorage := entities.InventoryStorageEntity{
ProductID: productID, ProductID: productID,
AisleID: aisleID, AisleID: aisleID,
@ -80,6 +84,7 @@ func (s *inventoryStorageService) Create(ctx context.Context, req dtodomain.Inve
InvReceiptID: InvReceiptID, InvReceiptID: InvReceiptID,
OnHandQuantity: req.OnHandQuantity, OnHandQuantity: req.OnHandQuantity,
AvailableQuantity: req.AvailableQuantity, AvailableQuantity: req.AvailableQuantity,
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
} }
created, err := s.inventoryStorageRepo.Create(ctx, tx, inventoryStorage) created, err := s.inventoryStorageRepo.Create(ctx, tx, inventoryStorage)
if err != nil { if err != nil {
@ -87,6 +92,14 @@ func (s *inventoryStorageService) Create(ctx context.Context, req dtodomain.Inve
return dtodomain.InventoryStorageResponse{}, err return dtodomain.InventoryStorageResponse{}, err
} }
tx.Commit() 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()) result, err := s.inventoryStorageRepo.GetById(ctx, nil, created.ID.String())
if err != nil { if err != nil {
return dtodomain.InventoryStorageResponse{}, err return dtodomain.InventoryStorageResponse{}, err
@ -129,8 +142,10 @@ func (s *inventoryStorageService) Update(ctx context.Context, req dtodomain.Inve
tx.Rollback() tx.Rollback()
return dtodomain.InventoryStorageResponse{}, err return dtodomain.InventoryStorageResponse{}, err
} }
before := inventoryStorage
inventoryStorage.OnHandQuantity = req.OnHandQuantity inventoryStorage.OnHandQuantity = req.OnHandQuantity
inventoryStorage.AvailableQuantity = req.AvailableQuantity inventoryStorage.AvailableQuantity = req.AvailableQuantity
inventoryStorage.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
updated, err := s.inventoryStorageRepo.Update(ctx, tx, inventoryStorage) updated, err := s.inventoryStorageRepo.Update(ctx, tx, inventoryStorage)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
@ -141,6 +156,16 @@ func (s *inventoryStorageService) Update(ctx context.Context, req dtodomain.Inve
if err != nil { if err != nil {
return dtodomain.InventoryStorageResponse{}, err 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 return dtodomain.ToInventoryStorageResponse(result), nil
} }
@ -151,17 +176,38 @@ func (s *inventoryStorageService) Delete(ctx context.Context, inventoryStorageId
tx.Rollback() 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 { if err := s.inventoryStorageRepo.Delete(ctx, tx, inventoryStorageId); 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": constants.DELETE,
"entity": "inventory_storage",
"entity_id": inventoryStorageId,
}).Info("Inventory Storage deleted")
return nil 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{ return &inventoryStorageService{
db: db, db: db,
inventoryStorageRepo: inventoryStorageRepo, inventoryStorageRepo: inventoryStorageRepo,
log: log,
} }
} }

View File

@ -193,7 +193,7 @@ func RegisterDependencies(injector *do.Injector) {
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, log) inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository, sequenceServ, log)
inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, 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) inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository)
quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository) quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository)