feat: integrate logging and audit trail in inventory return service
This commit is contained in:
parent
844a41eb78
commit
bcc73a0f99
|
|
@ -11,9 +11,11 @@ import (
|
|||
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_return/repository"
|
||||
productrepository "github.com/Caknoooo/go-gin-clean-starter/modules/product/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"
|
||||
)
|
||||
|
||||
|
|
@ -36,6 +38,7 @@ type inventoryReturnService struct {
|
|||
issueLineRepo issueRepository.InventoryIssueLineRepository
|
||||
productRepo productrepository.ProductRepository
|
||||
sequenceService sequenceservice.SequenceService
|
||||
log *logrus.Logger
|
||||
}
|
||||
|
||||
// GetLinesByReturnId implements InventoryReturnService.
|
||||
|
|
@ -49,7 +52,29 @@ func (s *inventoryReturnService) GetLinesByReturnId(ctx context.Context, returnI
|
|||
|
||||
// DeleteLine implements InventoryReturnService.
|
||||
func (s *inventoryReturnService) DeleteLine(ctx context.Context, lineId string) error {
|
||||
return s.returnLineRepo.Delete(ctx, nil, lineId)
|
||||
ireturn, err := s.returnLineRepo.GetById(ctx, nil, lineId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ireturn.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||
_, err = s.returnLineRepo.Update(ctx, nil, ireturn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result := s.returnLineRepo.Delete(ctx, nil, lineId)
|
||||
|
||||
if result != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.DELETE,
|
||||
"entity": "inventory_return_line",
|
||||
"entity_id": lineId,
|
||||
}).Info("Inventory Return Line deleted")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateLine implements InventoryReturnService.
|
||||
|
|
@ -59,6 +84,8 @@ func (s *inventoryReturnService) UpdateLine(ctx context.Context, lineId string,
|
|||
return dtodomain.InventoryReturnLineResponse{}, err
|
||||
}
|
||||
|
||||
before := line
|
||||
|
||||
// Ambil product untuk cek returnable
|
||||
product, err := s.productRepo.GetById(ctx, nil, line.ProductID.String())
|
||||
if err != nil {
|
||||
|
|
@ -87,11 +114,27 @@ func (s *inventoryReturnService) UpdateLine(ctx context.Context, lineId string,
|
|||
if req.ClientID != nil {
|
||||
line.ClientID = uuid.MustParse(*req.ClientID)
|
||||
}
|
||||
line.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||
updatedLine, err := s.returnLineRepo.Update(ctx, nil, line)
|
||||
if err != nil {
|
||||
return dtodomain.InventoryReturnLineResponse{}, err
|
||||
}
|
||||
return dtodomain.ToInventoryReturnLineResponse(updatedLine), nil
|
||||
|
||||
result, err := s.returnLineRepo.GetById(ctx, nil, updatedLine.ID.String())
|
||||
if err != nil {
|
||||
return dtodomain.InventoryReturnLineResponse{}, err
|
||||
}
|
||||
|
||||
changes := utils.GetChangedFields(before, result)
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.UPDATE,
|
||||
"entity": "inventory_return_line",
|
||||
"entity_id": lineId,
|
||||
"changes": changes,
|
||||
}).Info("Inventory Return Line updated")
|
||||
|
||||
return dtodomain.ToInventoryReturnLineResponse(result), nil
|
||||
}
|
||||
|
||||
func (s *inventoryReturnService) Create(ctx context.Context, req dtodomain.InventoryReturnCreateRequest) (dtodomain.InventoryReturnResponse, error) {
|
||||
|
|
@ -128,6 +171,7 @@ func (s *inventoryReturnService) Create(ctx context.Context, req dtodomain.Inven
|
|||
Status: req.Status,
|
||||
InvIssueID: invIssueId,
|
||||
ClientID: clientUUID,
|
||||
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||
}
|
||||
created, err := s.returnRepo.Create(ctx, tx, entity)
|
||||
if err != nil {
|
||||
|
|
@ -169,6 +213,14 @@ func (s *inventoryReturnService) Create(ctx context.Context, req dtodomain.Inven
|
|||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.CREATE,
|
||||
"entity": "inventory_return",
|
||||
"entity_id": created.ID.String(),
|
||||
}).Info("Inventory Return created")
|
||||
|
||||
result, err := s.returnRepo.GetById(ctx, nil, created.ID.String())
|
||||
if err != nil {
|
||||
return dtodomain.InventoryReturnResponse{}, err
|
||||
|
|
@ -211,11 +263,13 @@ func (s *inventoryReturnService) Update(ctx context.Context, req dtodomain.Inven
|
|||
tx.Rollback()
|
||||
return dtodomain.InventoryReturnResponse{}, err
|
||||
}
|
||||
before := ret
|
||||
ret.DocumentDate = utils.StringToDateTime(req.DocumentDate)
|
||||
ret.Notes = req.Notes
|
||||
ret.Status = req.Status
|
||||
ret.InvIssueID = uuid.MustParse(req.InvIssueID)
|
||||
ret.ClientID = uuid.MustParse(req.ClientID)
|
||||
ret.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||
updated, err := s.returnRepo.Update(ctx, tx, ret)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
|
@ -226,6 +280,15 @@ func (s *inventoryReturnService) Update(ctx context.Context, req dtodomain.Inven
|
|||
if err != nil {
|
||||
return dtodomain.InventoryReturnResponse{}, err
|
||||
}
|
||||
changes := utils.GetChangedFields(before, result)
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.UPDATE,
|
||||
"entity": "inventory_return",
|
||||
"entity_id": id,
|
||||
"changes": changes,
|
||||
}).Info("Inventory Return updated")
|
||||
|
||||
return dtodomain.ToInventoryReturnResponse(result), nil
|
||||
}
|
||||
|
||||
|
|
@ -236,11 +299,31 @@ func (s *inventoryReturnService) Delete(ctx context.Context, id string) error {
|
|||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
ret, err := s.returnRepo.GetById(ctx, tx, id)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
ret.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||
if _, err := s.returnRepo.Update(ctx, tx, ret); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.returnRepo.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_return",
|
||||
"entity_id": id,
|
||||
}).Info("Inventory Return deleted")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -264,11 +347,18 @@ func (s *inventoryReturnService) CreateLine(ctx context.Context, returnId string
|
|||
InvReturnID: invReturnUUID,
|
||||
ProductID: productUUID,
|
||||
ClientID: clientUUID,
|
||||
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||
}
|
||||
created, err := s.returnLineRepo.Create(ctx, nil, line)
|
||||
if err != nil {
|
||||
return dtodomain.InventoryReturnLineResponse{}, err
|
||||
}
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": constants.CREATE,
|
||||
"entity": "inventory_return_line",
|
||||
"entity_id": created.ID.String(),
|
||||
}).Info("Inventory Return Line created")
|
||||
return dtodomain.ToInventoryReturnLineResponse(created), nil
|
||||
}
|
||||
|
||||
|
|
@ -279,6 +369,7 @@ func NewInventoryReturnService(
|
|||
issueLineRepo issueRepository.InventoryIssueLineRepository,
|
||||
productRepo productrepository.ProductRepository,
|
||||
sequenceService sequenceservice.SequenceService,
|
||||
log *logrus.Logger,
|
||||
) InventoryReturnService {
|
||||
return &inventoryReturnService{
|
||||
db: db,
|
||||
|
|
@ -287,5 +378,6 @@ func NewInventoryReturnService(
|
|||
issueLineRepo: issueLineRepo,
|
||||
productRepo: productRepo,
|
||||
sequenceService: sequenceService,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -191,7 +191,7 @@ func RegisterDependencies(injector *do.Injector) {
|
|||
assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository)
|
||||
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)
|
||||
inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository, sequenceServ, log)
|
||||
inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository, sequenceServ, log)
|
||||
inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository)
|
||||
inventoryTransactionServ := inventoryTransactionService.NewInventoryTransactionService(db, inventoryTransactionRepository)
|
||||
|
|
|
|||
Loading…
Reference in New Issue