feat: integrate logging and audit trail in inventory request service
This commit is contained in:
parent
7e4e7cc5a8
commit
844a41eb78
|
|
@ -8,9 +8,11 @@ import (
|
|||
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/query"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/repository"
|
||||
sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
|
@ -31,6 +33,7 @@ type inventoryRequestService struct {
|
|||
requestRepo repository.InventoryRequestRepository
|
||||
requestLineRepo repository.InventoryRequestLineRepository
|
||||
sequenceService sequenceservice.SequenceService
|
||||
log *logrus.Logger
|
||||
}
|
||||
|
||||
// GetLinesByRequestId implements InventoryRequestService.
|
||||
|
|
@ -43,7 +46,29 @@ func (s *inventoryRequestService) GetLinesByRequestId(ctx context.Context, reque
|
|||
}
|
||||
|
||||
func (s *inventoryRequestService) DeleteLine(ctx context.Context, lineId string) error {
|
||||
return s.requestLineRepo.Delete(ctx, nil, lineId)
|
||||
line, err := s.requestLineRepo.GetById(ctx, nil, lineId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
line.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||
_, err = s.requestLineRepo.Update(ctx, nil, line)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result := s.requestLineRepo.Delete(ctx, nil, lineId)
|
||||
if result != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.DELETE,
|
||||
"entity": "inventory_request_line",
|
||||
"entity_id": lineId,
|
||||
}).Info("Inventory Request Line deleted")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *inventoryRequestService) UpdateLine(ctx context.Context, lineId string, req dtodomain.InventoryRequestLineUpdateRequest) (dtodomain.InventoryRequestLineResponse, error) {
|
||||
|
|
@ -51,6 +76,10 @@ func (s *inventoryRequestService) UpdateLine(ctx context.Context, lineId string,
|
|||
if err != nil {
|
||||
return dtodomain.InventoryRequestLineResponse{}, err
|
||||
}
|
||||
before := line
|
||||
|
||||
line.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||
|
||||
if req.Quantity != nil {
|
||||
line.Quantity = *req.Quantity
|
||||
}
|
||||
|
|
@ -69,6 +98,16 @@ func (s *inventoryRequestService) UpdateLine(ctx context.Context, lineId string,
|
|||
if err != nil {
|
||||
return dtodomain.InventoryRequestLineResponse{}, err
|
||||
}
|
||||
|
||||
changes := utils.GetChangedFields(before, updated)
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.UPDATE,
|
||||
"entity": "inventory_request_line",
|
||||
"entity_id": lineId,
|
||||
"changes": changes,
|
||||
}).Info("Inventory Request Line updated")
|
||||
|
||||
product := dtodomain.InventoryRequestLineProductResponse{}
|
||||
if updated.Product.ID != uuid.Nil {
|
||||
product = dtodomain.InventoryRequestLineProductResponse{
|
||||
|
|
@ -119,6 +158,7 @@ func (s *inventoryRequestService) Create(ctx context.Context, req dtodomain.Inve
|
|||
Note: req.Note,
|
||||
ClientID: clientUUID,
|
||||
Status: req.Status,
|
||||
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||
}
|
||||
created, err := s.requestRepo.Create(ctx, tx, request)
|
||||
if err != nil {
|
||||
|
|
@ -157,6 +197,15 @@ func (s *inventoryRequestService) Create(ctx context.Context, req dtodomain.Inve
|
|||
}
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
// Log the creation of the inventory request
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.CREATE,
|
||||
"entity": "inventory_request",
|
||||
"entity_id": created.ID.String(),
|
||||
}).Info("Inventory Request created")
|
||||
|
||||
result, err := s.requestRepo.GetById(ctx, nil, created.ID.String())
|
||||
if err != nil {
|
||||
return dtodomain.InventoryRequestResponse{}, err
|
||||
|
|
@ -199,6 +248,11 @@ func (s *inventoryRequestService) Update(ctx context.Context, req dtodomain.Inve
|
|||
tx.Rollback()
|
||||
return dtodomain.InventoryRequestResponse{}, err
|
||||
}
|
||||
|
||||
before := request
|
||||
|
||||
request.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||
|
||||
if req.ReferenceNumber != "" {
|
||||
request.ReferenceNumber = req.ReferenceNumber
|
||||
}
|
||||
|
|
@ -217,6 +271,15 @@ func (s *inventoryRequestService) Update(ctx context.Context, req dtodomain.Inve
|
|||
if err != nil {
|
||||
return dtodomain.InventoryRequestResponse{}, err
|
||||
}
|
||||
|
||||
changes := utils.GetChangedFields(before, result)
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.UPDATE,
|
||||
"entity": "inventory_request",
|
||||
"entity_id": id,
|
||||
"changes": changes,
|
||||
}).Info("Inventory Request updated")
|
||||
return dtodomain.ToInventoryRequestResponse(result), nil
|
||||
}
|
||||
|
||||
|
|
@ -227,11 +290,31 @@ func (s *inventoryRequestService) Delete(ctx context.Context, id string) error {
|
|||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
request, err := s.requestRepo.GetById(ctx, tx, id)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
request.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||
if _, err := s.requestRepo.Update(ctx, tx, request); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.requestRepo.Delete(ctx, tx, id); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.DELETE,
|
||||
"entity": "inventory_request",
|
||||
"entity_id": id,
|
||||
}).Info("Inventory Request deleted")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -283,11 +366,12 @@ func (s *inventoryRequestService) CreateLine(ctx context.Context, requestId stri
|
|||
}, nil
|
||||
}
|
||||
|
||||
func NewInventoryRequestService(db *gorm.DB, requestRepo repository.InventoryRequestRepository, requestLineRepo repository.InventoryRequestLineRepository, sequenceService sequenceservice.SequenceService) InventoryRequestService {
|
||||
func NewInventoryRequestService(db *gorm.DB, requestRepo repository.InventoryRequestRepository, requestLineRepo repository.InventoryRequestLineRepository, sequenceService sequenceservice.SequenceService, log *logrus.Logger) InventoryRequestService {
|
||||
return &inventoryRequestService{
|
||||
db: db,
|
||||
requestRepo: requestRepo,
|
||||
requestLineRepo: requestLineRepo,
|
||||
sequenceService: sequenceService,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -189,7 +189,7 @@ func RegisterDependencies(injector *do.Injector) {
|
|||
aisleServ := aisleService.NewAisleService(aisleRepository, db, log)
|
||||
inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ, log)
|
||||
assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository)
|
||||
inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ)
|
||||
inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ, log)
|
||||
inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository, sequenceServ, log)
|
||||
inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository, sequenceServ)
|
||||
inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ, log)
|
||||
|
|
|
|||
Loading…
Reference in New Issue