diff --git a/modules/aisle/dto/aisle_dto.go b/modules/aisle/dto/aisle_dto.go index 6874901..0898f37 100644 --- a/modules/aisle/dto/aisle_dto.go +++ b/modules/aisle/dto/aisle_dto.go @@ -3,7 +3,9 @@ package dto import ( "errors" + "github.com/Caknoooo/go-gin-clean-starter/database/entities" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" + "github.com/google/uuid" ) const ( @@ -75,3 +77,48 @@ type AisleResponse struct { WeightUom pkgdto.IdNameResponse `json:"weight_uom_id"` Client pkgdto.IdNameResponse `json:"client_id"` } + +func ToAisleResponse(e entities.MAisleEntity) AisleResponse { + zona := pkgdto.IdNameResponse{} + if e.Zona.ID != uuid.Nil { + zona.ID = e.Zona.ID.String() + zona.Name = e.Zona.Name + } + + dimUom := pkgdto.IdNameResponse{} + if e.DimUom.ID != uuid.Nil { + dimUom.ID = e.DimUom.ID.String() + dimUom.Name = e.DimUom.Name + } + + weightUom := pkgdto.IdNameResponse{} + if e.WeightUom.ID != uuid.Nil { + weightUom.ID = e.WeightUom.ID.String() + weightUom.Name = e.WeightUom.Name + } + + client := pkgdto.IdNameResponse{} + if e.Client.ID != uuid.Nil { + client.ID = e.Client.ID.String() + client.Name = e.Client.Name + } + + return AisleResponse{ + ID: e.ID.String(), + Code: e.Code, + Name: e.Name, + IsleX: e.IsleX, + BinY: e.BinY, + LevelZ: e.LevelZ, + DimLength: e.DimLength, + DimWidth: e.DimWidth, + DimHeight: e.DimHeight, + Weight: e.Weight, + QrCodeAisle: e.QrCodeAisle, + IsActive: e.IsActive, + Zone: zona, + DimUom: dimUom, + WeightUom: weightUom, + Client: client, + } +} diff --git a/modules/aisle/service/aisle_service.go b/modules/aisle/service/aisle_service.go index 12762c3..47cf0f4 100644 --- a/modules/aisle/service/aisle_service.go +++ b/modules/aisle/service/aisle_service.go @@ -7,8 +7,10 @@ import ( "github.com/Caknoooo/go-gin-clean-starter/modules/aisle/dto" "github.com/Caknoooo/go-gin-clean-starter/modules/aisle/query" "github.com/Caknoooo/go-gin-clean-starter/modules/aisle/repository" - pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" + "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" ) @@ -23,51 +25,7 @@ type AisleService interface { type aisleService struct { db *gorm.DB aisleRepo repository.AisleRepository -} - -func toAisleResponse(e entities.MAisleEntity) dto.AisleResponse { - zona := pkgdto.IdNameResponse{} - if e.Zona.ID != uuid.Nil { - zona.ID = e.Zona.ID.String() - zona.Name = e.Zona.Name - } - - dimUom := pkgdto.IdNameResponse{} - if e.DimUom.ID != uuid.Nil { - dimUom.ID = e.DimUom.ID.String() - dimUom.Name = e.DimUom.Name - } - - weightUom := pkgdto.IdNameResponse{} - if e.WeightUom.ID != uuid.Nil { - weightUom.ID = e.WeightUom.ID.String() - weightUom.Name = e.WeightUom.Name - } - - client := pkgdto.IdNameResponse{} - if e.Client.ID != uuid.Nil { - client.ID = e.Client.ID.String() - client.Name = e.Client.Name - } - - return dto.AisleResponse{ - ID: e.ID.String(), - Code: e.Code, - Name: e.Name, - IsleX: e.IsleX, - BinY: e.BinY, - LevelZ: e.LevelZ, - DimLength: e.DimLength, - DimWidth: e.DimWidth, - DimHeight: e.DimHeight, - Weight: e.Weight, - QrCodeAisle: e.QrCodeAisle, - IsActive: e.IsActive, - Zone: zona, - DimUom: dimUom, - WeightUom: weightUom, - Client: client, - } + log *logrus.Logger } func (s *aisleService) Create(ctx context.Context, req dto.AisleCreateRequest) (dto.AisleResponse, error) { @@ -98,21 +56,22 @@ func (s *aisleService) Create(ctx context.Context, req dto.AisleCreateRequest) ( return dto.AisleResponse{}, err } aisle := entities.MAisleEntity{ - Code: req.Code, - Name: req.Name, - IsleX: req.IsleX, - BinY: req.BinY, - LevelZ: req.LevelZ, - DimLength: req.DimLength, - DimWidth: req.DimWidth, - DimHeight: req.DimHeight, - Weight: req.Weight, - QrCodeAisle: req.QrCodeAisle, - IsActive: req.IsActive, - ZoneID: zoneUUID, - DimUomID: dimUomUUID, - WeightUomID: weightUomUUID, - ClientID: clientUUID, + Code: req.Code, + Name: req.Name, + IsleX: req.IsleX, + BinY: req.BinY, + LevelZ: req.LevelZ, + DimLength: req.DimLength, + DimWidth: req.DimWidth, + DimHeight: req.DimHeight, + Weight: req.Weight, + QrCodeAisle: req.QrCodeAisle, + IsActive: req.IsActive, + ZoneID: zoneUUID, + DimUomID: dimUomUUID, + WeightUomID: weightUomUUID, + ClientID: clientUUID, + FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE), } created, err := s.aisleRepo.Create(ctx, tx, aisle) if err != nil { @@ -124,7 +83,13 @@ func (s *aisleService) Create(ctx context.Context, req dto.AisleCreateRequest) ( if err != nil { return dto.AisleResponse{}, err } - return toAisleResponse(result), nil + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": "create", + "entity": "aisle", + "entity_id": created.ID.String(), + }).Info("Aisle created") + return dto.ToAisleResponse(result), nil } func (s *aisleService) GetById(ctx context.Context, aisleId string) (dto.AisleResponse, error) { @@ -132,7 +97,7 @@ func (s *aisleService) GetById(ctx context.Context, aisleId string) (dto.AisleRe if err != nil { return dto.AisleResponse{}, err } - return toAisleResponse(aisle), nil + return dto.ToAisleResponse(aisle), nil } func (s *aisleService) GetAll(ctx context.Context, filter query.AisleFilter) ([]dto.AisleResponse, int64, error) { @@ -142,7 +107,7 @@ func (s *aisleService) GetAll(ctx context.Context, filter query.AisleFilter) ([] } var responses []dto.AisleResponse for _, e := range aisles { - responses = append(responses, toAisleResponse(e)) + responses = append(responses, dto.ToAisleResponse(e)) } if responses == nil { responses = make([]dto.AisleResponse, 0) @@ -193,6 +158,7 @@ func (s *aisleService) Update(ctx context.Context, req dto.AisleUpdateRequest, a aisle.QrCodeAisle = req.QrCodeAisle } aisle.IsActive = req.IsActive + aisle.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE) updated, err := s.aisleRepo.Update(ctx, tx, aisle) if err != nil { tx.Rollback() @@ -203,7 +169,13 @@ func (s *aisleService) Update(ctx context.Context, req dto.AisleUpdateRequest, a if err != nil { return dto.AisleResponse{}, err } - return toAisleResponse(result), nil + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": "update", + "entity": "aisle", + "entity_id": aisleId, + }).Info("Aisle updated") + return dto.ToAisleResponse(result), nil } func (s *aisleService) Delete(ctx context.Context, aisleId string) error { @@ -213,17 +185,34 @@ func (s *aisleService) Delete(ctx context.Context, aisleId string) error { tx.Rollback() } }() + aisle, err := s.aisleRepo.GetById(ctx, tx, aisleId) + if err != nil { + tx.Rollback() + return err + } + aisle.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE) + if _, err := s.aisleRepo.Update(ctx, tx, aisle); err != nil { + tx.Rollback() + return err + } if err := s.aisleRepo.Delete(ctx, tx, aisleId); err != nil { tx.Rollback() return err } tx.Commit() + s.log.WithFields(logrus.Fields{ + "user_id": utils.GetUserID(ctx), + "action": "delete", + "entity": "aisle", + "entity_id": aisleId, + }).Info("Aisle deleted") return nil } -func NewAisleService(aisleRepo repository.AisleRepository, db *gorm.DB) AisleService { +func NewAisleService(aisleRepo repository.AisleRepository, db *gorm.DB, log *logrus.Logger) AisleService { return &aisleService{ aisleRepo: aisleRepo, db: db, + log: log, } } diff --git a/providers/core.go b/providers/core.go index 7a85362..73804b7 100644 --- a/providers/core.go +++ b/providers/core.go @@ -186,7 +186,7 @@ func RegisterDependencies(injector *do.Injector) { mvendorServ := mvendorService.NewVendorService(mvendorRepository, sequenceServ, db, log) warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db, log) zonaServ := zonaService.NewZonaService(zonaRepository, db, log) - aisleServ := aisleService.NewAisleService(aisleRepository, db) + aisleServ := aisleService.NewAisleService(aisleRepository, db, log) inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ) assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository) inventoryRequestServ := inventoryRequestService.NewInventoryRequestService(db, inventoryRequestRepository, inventoryRequestLineRepository, sequenceServ)