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/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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -205,17 +246,25 @@ func (s *inventoryMovementService) CreateLine(ctx context.Context, movementId st
|
||||||
return dtodomain.InventoryMovementLineResponse{}, err
|
return dtodomain.InventoryMovementLineResponse{}, err
|
||||||
}
|
}
|
||||||
line := entities.TInventoryMovementLineEntity{
|
line := entities.TInventoryMovementLineEntity{
|
||||||
MovedQuantity: req.MovedQuantity,
|
MovedQuantity: req.MovedQuantity,
|
||||||
InvMovementID: movementUUID,
|
InvMovementID: movementUUID,
|
||||||
ProductID: productUUID,
|
ProductID: productUUID,
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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{
|
return &inventoryMovementService{
|
||||||
db: db,
|
db: db,
|
||||||
movementRepo: movementRepo,
|
movementRepo: movementRepo,
|
||||||
movementLineRepo: movementLineRepo,
|
movementLineRepo: movementLineRepo,
|
||||||
sequenceService: sequenceService,
|
sequenceService: sequenceService,
|
||||||
|
log: log,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue