feat: integrate logging and audit trail in inventory movement service
This commit is contained in:
parent
41dacd0e3e
commit
f36380873c
|
|
@ -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/repository"
|
||||
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/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
|
@ -30,6 +32,7 @@ type inventoryMovementService struct {
|
|||
movementRepo repository.InventoryMovementRepository
|
||||
movementLineRepo repository.InventoryMovementLineRepository
|
||||
sequenceService sequenceservice.SequenceService
|
||||
log *logrus.Logger
|
||||
}
|
||||
|
||||
// GetLinesByMovementId implements InventoryMovementService.
|
||||
|
|
@ -73,6 +76,7 @@ func (s *inventoryMovementService) Create(ctx context.Context, req dtodomain.Inv
|
|||
Status: req.Status,
|
||||
SourceLocationID: uuid.MustParse(req.SourceLocationID),
|
||||
DestinationLocationID: uuid.MustParse(req.DestinationLocationID),
|
||||
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||
}
|
||||
created, err := s.movementRepo.Create(ctx, tx, movement)
|
||||
if err != nil {
|
||||
|
|
@ -116,6 +120,14 @@ func (s *inventoryMovementService) Create(ctx context.Context, req dtodomain.Inv
|
|||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -154,12 +166,14 @@ func (s *inventoryMovementService) Update(ctx context.Context, req dtodomain.Inv
|
|||
tx.Rollback()
|
||||
return dtodomain.InventoryMovementResponse{}, err
|
||||
}
|
||||
// if req.MovementNumber != "" {
|
||||
// movement.MovementNumber = req.MovementNumber
|
||||
// }
|
||||
|
||||
before := movement
|
||||
|
||||
movement.MovementDate = utils.StringToDateTime(req.MovementDate)
|
||||
movement.MovementType = req.MovementType
|
||||
movement.Status = req.Status
|
||||
movement.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||
|
||||
updated, err := s.movementRepo.Update(ctx, tx, movement)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
|
@ -170,6 +184,15 @@ func (s *inventoryMovementService) Update(ctx context.Context, req dtodomain.Inv
|
|||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -180,11 +203,29 @@ func (s *inventoryMovementService) Delete(ctx context.Context, id string) error
|
|||
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 {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -211,11 +252,19 @@ func (s *inventoryMovementService) CreateLine(ctx context.Context, movementId st
|
|||
StorageID: uuid.MustParse(req.StorageID),
|
||||
ClientID: clientLineUUID,
|
||||
Status: req.Status,
|
||||
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||
}
|
||||
created, err := s.movementLineRepo.Create(ctx, nil, line)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -224,6 +273,7 @@ func (s *inventoryMovementService) UpdateLine(ctx context.Context, lineId string
|
|||
if err != nil {
|
||||
return dtodomain.InventoryMovementLineResponse{}, err
|
||||
}
|
||||
before := line
|
||||
if req.MovedQuantity != nil {
|
||||
line.MovedQuantity = *req.MovedQuantity
|
||||
}
|
||||
|
|
@ -234,18 +284,47 @@ func (s *inventoryMovementService) UpdateLine(ctx context.Context, lineId string
|
|||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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) InventoryMovementService {
|
||||
func NewInventoryMovementService(db *gorm.DB, movementRepo repository.InventoryMovementRepository, movementLineRepo repository.InventoryMovementLineRepository, sequenceService sequenceservice.SequenceService, log *logrus.Logger) InventoryMovementService {
|
||||
return &inventoryMovementService{
|
||||
db: db,
|
||||
movementRepo: movementRepo,
|
||||
movementLineRepo: movementLineRepo,
|
||||
sequenceService: sequenceService,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@ func RegisterDependencies(injector *do.Injector) {
|
|||
inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, 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)
|
||||
inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ, log)
|
||||
inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository)
|
||||
inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository)
|
||||
quarantineServ := quarantineService.NewQuarantineService(db, quarantineRepository, quarantineLineRepository, productRepository, uomRepository, inventoryStorageRepository)
|
||||
|
|
|
|||
Loading…
Reference in New Issue