feat: integrate logging and audit trail in inventory issue service
This commit is contained in:
parent
e6c025f675
commit
41dacd0e3e
|
|
@ -8,9 +8,11 @@ import (
|
|||
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/query"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_issue/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 inventoryIssueService struct {
|
|||
issueRepo repository.InventoryIssueRepository
|
||||
issueLineRepo repository.InventoryIssueLineRepository
|
||||
sequenceService sequenceservice.SequenceService
|
||||
log *logrus.Logger
|
||||
}
|
||||
|
||||
// GetLinesByIssueId implements InventoryIssueService.
|
||||
|
|
@ -42,8 +45,8 @@ func (s *inventoryIssueService) GetLinesByIssueId(ctx context.Context, issueId s
|
|||
return dtodomain.ToInventoryIssueLineResponses(lines), nil
|
||||
}
|
||||
|
||||
func NewInventoryIssueService(db *gorm.DB, issueRepo repository.InventoryIssueRepository, issueLineRepo repository.InventoryIssueLineRepository, sequenceService sequenceservice.SequenceService) InventoryIssueService {
|
||||
return &inventoryIssueService{db: db, issueRepo: issueRepo, issueLineRepo: issueLineRepo, sequenceService: sequenceService}
|
||||
func NewInventoryIssueService(db *gorm.DB, issueRepo repository.InventoryIssueRepository, issueLineRepo repository.InventoryIssueLineRepository, sequenceService sequenceservice.SequenceService, log *logrus.Logger) InventoryIssueService {
|
||||
return &inventoryIssueService{db: db, issueRepo: issueRepo, issueLineRepo: issueLineRepo, sequenceService: sequenceService, log: log}
|
||||
}
|
||||
|
||||
func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.InventoryIssueCreateRequest) (dtodomain.InventoryIssueResponse, error) {
|
||||
|
|
@ -85,6 +88,7 @@ func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.Invent
|
|||
InvRequestID: invRequestUUID,
|
||||
ClientID: clientUUID,
|
||||
Status: req.Status,
|
||||
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||
}
|
||||
created, err := s.issueRepo.Create(ctx, tx, issue)
|
||||
if err != nil {
|
||||
|
|
@ -129,6 +133,12 @@ func (s *inventoryIssueService) Create(ctx context.Context, req dtodomain.Invent
|
|||
}
|
||||
}
|
||||
tx.Commit()
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": "create",
|
||||
"entity": "inventory_issue",
|
||||
"entity_id": created.ID.String(),
|
||||
}).Info("Inventory Issue created")
|
||||
result, err := s.issueRepo.GetById(ctx, nil, created.ID.String())
|
||||
if err != nil {
|
||||
return dtodomain.InventoryIssueResponse{}, err
|
||||
|
|
@ -171,19 +181,34 @@ func (s *inventoryIssueService) Update(ctx context.Context, req dtodomain.Invent
|
|||
tx.Rollback()
|
||||
return dtodomain.InventoryIssueResponse{}, err
|
||||
}
|
||||
|
||||
before := issue
|
||||
|
||||
issue.DocumentDate = utils.StringToDateTime(req.DocumentDate)
|
||||
issue.DueDate = utils.StringToDateTime(req.DueDate)
|
||||
issue.Status = req.Status
|
||||
issue.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||
updated, err := s.issueRepo.Update(ctx, tx, issue)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return dtodomain.InventoryIssueResponse{}, err
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
result, err := s.issueRepo.GetById(ctx, nil, updated.ID.String())
|
||||
if err != nil {
|
||||
return dtodomain.InventoryIssueResponse{}, err
|
||||
}
|
||||
|
||||
changes := utils.GetChangedFields(before, result)
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": "update",
|
||||
"entity": "inventory_issue",
|
||||
"entity_id": id,
|
||||
"changes": changes,
|
||||
}).Info("Inventory Issue updated")
|
||||
|
||||
return dtodomain.ToInventoryIssueResponse(result), nil
|
||||
}
|
||||
|
||||
|
|
@ -194,11 +219,27 @@ func (s *inventoryIssueService) Delete(ctx context.Context, id string) error {
|
|||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
issue, err := s.issueRepo.GetById(ctx, tx, id)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
issue.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||
if _, err := s.issueRepo.Update(ctx, tx, issue); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if err := s.issueRepo.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_issue",
|
||||
"entity_id": id,
|
||||
}).Info("Inventory Issue deleted")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -190,7 +190,7 @@ func RegisterDependencies(injector *do.Injector) {
|
|||
inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ)
|
||||
assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository)
|
||||
inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ)
|
||||
inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository, 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)
|
||||
inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository)
|
||||
|
|
|
|||
Loading…
Reference in New Issue