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"
"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) {
@ -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,
}
}

View File

@ -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)