feat: implement ZonaResponse conversion and enhance logging in Zona service
This commit is contained in:
parent
db82ad3364
commit
02c3c97a76
|
|
@ -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 (
|
||||
|
|
@ -63,3 +65,34 @@ type ZonaResponse struct {
|
|||
Warehouse pkgdto.IdNameResponse `json:"warehouse"`
|
||||
Client pkgdto.IdNameResponse `json:"client"`
|
||||
}
|
||||
|
||||
func ToZonaResponse(e entities.MZonaEntity) ZonaResponse {
|
||||
warehouse := pkgdto.IdNameResponse{}
|
||||
if e.Warehouse.ID != uuid.Nil {
|
||||
warehouse = pkgdto.IdNameResponse{
|
||||
ID: e.Warehouse.ID.String(),
|
||||
Name: e.Warehouse.Name,
|
||||
}
|
||||
}
|
||||
|
||||
client := pkgdto.IdNameResponse{}
|
||||
if e.Client.ID != uuid.Nil {
|
||||
client = pkgdto.IdNameResponse{
|
||||
ID: e.Client.ID.String(),
|
||||
Name: e.Client.Name,
|
||||
}
|
||||
}
|
||||
|
||||
return ZonaResponse{
|
||||
ID: e.ID.String(),
|
||||
Code: e.Code,
|
||||
Name: e.Name,
|
||||
Type: e.Type,
|
||||
Temperature: e.Temperature,
|
||||
Hazardous: e.Hazardous,
|
||||
QRCodeZone: e.QRCodeZone,
|
||||
IsActive: e.IsActive,
|
||||
Warehouse: warehouse,
|
||||
Client: client,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,10 @@ import (
|
|||
"github.com/Caknoooo/go-gin-clean-starter/modules/zona/dto"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/zona/query"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/zona/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,38 +25,7 @@ type ZonaService interface {
|
|||
type zonaService struct {
|
||||
db *gorm.DB
|
||||
zonaRepo repository.ZonaRepository
|
||||
}
|
||||
|
||||
func toZonaResponse(e entities.MZonaEntity) dto.ZonaResponse {
|
||||
|
||||
warehouse := pkgdto.IdNameResponse{}
|
||||
if e.Warehouse.ID != uuid.Nil {
|
||||
warehouse = pkgdto.IdNameResponse{
|
||||
ID: e.Warehouse.ID.String(),
|
||||
Name: e.Warehouse.Name,
|
||||
}
|
||||
}
|
||||
|
||||
client := pkgdto.IdNameResponse{}
|
||||
if e.Client.ID != uuid.Nil {
|
||||
client = pkgdto.IdNameResponse{
|
||||
ID: e.Client.ID.String(),
|
||||
Name: e.Client.Name,
|
||||
}
|
||||
}
|
||||
|
||||
return dto.ZonaResponse{
|
||||
ID: e.ID.String(),
|
||||
Code: e.Code,
|
||||
Name: e.Name,
|
||||
Type: e.Type,
|
||||
Temperature: e.Temperature,
|
||||
Hazardous: e.Hazardous,
|
||||
QRCodeZone: e.QRCodeZone,
|
||||
IsActive: e.IsActive,
|
||||
Warehouse: warehouse,
|
||||
Client: client,
|
||||
}
|
||||
log *logrus.Logger
|
||||
}
|
||||
|
||||
func (s *zonaService) Create(ctx context.Context, req dto.ZonaCreateRequest) (dto.ZonaResponse, error) {
|
||||
|
|
@ -75,15 +46,14 @@ func (s *zonaService) Create(ctx context.Context, req dto.ZonaCreateRequest) (dt
|
|||
return dto.ZonaResponse{}, err
|
||||
}
|
||||
zona := entities.MZonaEntity{
|
||||
Code: req.Code,
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Temperature: req.Temperature,
|
||||
Hazardous: req.Hazardous,
|
||||
// QRCodeZone: req.QRCodeZone,
|
||||
// IsActive: req.IsActive,
|
||||
WarehouseID: warehouseUUID,
|
||||
ClientID: clientUUID,
|
||||
Code: req.Code,
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Temperature: req.Temperature,
|
||||
Hazardous: req.Hazardous,
|
||||
WarehouseID: warehouseUUID,
|
||||
ClientID: clientUUID,
|
||||
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||
}
|
||||
created, err := s.zonaRepo.Create(ctx, tx, zona)
|
||||
if err != nil {
|
||||
|
|
@ -95,7 +65,13 @@ func (s *zonaService) Create(ctx context.Context, req dto.ZonaCreateRequest) (dt
|
|||
if err != nil {
|
||||
return dto.ZonaResponse{}, err
|
||||
}
|
||||
return toZonaResponse(result), nil
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": "create",
|
||||
"entity": "zona",
|
||||
"entity_id": created.ID.String(),
|
||||
}).Info("Zona created")
|
||||
return dto.ToZonaResponse(result), nil
|
||||
}
|
||||
|
||||
func (s *zonaService) GetById(ctx context.Context, zonaId string) (dto.ZonaResponse, error) {
|
||||
|
|
@ -103,7 +79,7 @@ func (s *zonaService) GetById(ctx context.Context, zonaId string) (dto.ZonaRespo
|
|||
if err != nil {
|
||||
return dto.ZonaResponse{}, err
|
||||
}
|
||||
return toZonaResponse(zona), nil
|
||||
return dto.ToZonaResponse(zona), nil
|
||||
}
|
||||
|
||||
func (s *zonaService) GetAll(ctx context.Context, filter query.ZonaFilter) ([]dto.ZonaResponse, int64, error) {
|
||||
|
|
@ -113,7 +89,7 @@ func (s *zonaService) GetAll(ctx context.Context, filter query.ZonaFilter) ([]dt
|
|||
}
|
||||
var responses []dto.ZonaResponse
|
||||
for _, e := range zonas {
|
||||
responses = append(responses, toZonaResponse(e))
|
||||
responses = append(responses, dto.ToZonaResponse(e))
|
||||
}
|
||||
if responses == nil {
|
||||
responses = make([]dto.ZonaResponse, 0)
|
||||
|
|
@ -133,6 +109,7 @@ func (s *zonaService) Update(ctx context.Context, req dto.ZonaUpdateRequest, zon
|
|||
tx.Rollback()
|
||||
return dto.ZonaResponse{}, err
|
||||
}
|
||||
before := zona
|
||||
if req.Code != "" {
|
||||
zona.Code = req.Code
|
||||
}
|
||||
|
|
@ -144,20 +121,8 @@ func (s *zonaService) Update(ctx context.Context, req dto.ZonaUpdateRequest, zon
|
|||
}
|
||||
zona.Temperature = req.Temperature
|
||||
zona.Hazardous = req.Hazardous
|
||||
// zona.QRCodeZone = req.QRCodeZone
|
||||
zona.IsActive = req.IsActive
|
||||
// if req.WarehouseID != "" {
|
||||
// warehouseUUID, err := uuid.Parse(req.WarehouseID)
|
||||
// if err == nil {
|
||||
// zona.WarehouseID = warehouseUUID
|
||||
// }
|
||||
// }
|
||||
// if req.ClientID != "" {
|
||||
// clientUUID, err := uuid.Parse(req.ClientID)
|
||||
// if err == nil {
|
||||
// zona.ClientID = clientUUID
|
||||
// }
|
||||
// }
|
||||
zona.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||
updated, err := s.zonaRepo.Update(ctx, tx, zona)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
|
@ -168,7 +133,15 @@ func (s *zonaService) Update(ctx context.Context, req dto.ZonaUpdateRequest, zon
|
|||
if err != nil {
|
||||
return dto.ZonaResponse{}, err
|
||||
}
|
||||
return toZonaResponse(result), nil
|
||||
changes := utils.GetChangedFields(before, result)
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": "update",
|
||||
"entity": "zona",
|
||||
"entity_id": zonaId,
|
||||
"changes": changes,
|
||||
}).Info("Zona updated")
|
||||
return dto.ToZonaResponse(result), nil
|
||||
}
|
||||
|
||||
func (s *zonaService) Delete(ctx context.Context, zonaId string) error {
|
||||
|
|
@ -178,17 +151,34 @@ func (s *zonaService) Delete(ctx context.Context, zonaId string) error {
|
|||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
zona, err := s.zonaRepo.GetById(ctx, tx, zonaId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
zona.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||
if _, err := s.zonaRepo.Update(ctx, tx, zona); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if err := s.zonaRepo.Delete(ctx, tx, zonaId); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
tx.Commit()
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": utils.GetUserID(ctx),
|
||||
"action": "delete",
|
||||
"entity": "zona",
|
||||
"entity_id": zonaId,
|
||||
}).Info("Zona deleted")
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewZonaService(zonaRepo repository.ZonaRepository, db *gorm.DB) ZonaService {
|
||||
func NewZonaService(zonaRepo repository.ZonaRepository, db *gorm.DB, log *logrus.Logger) ZonaService {
|
||||
return &zonaService{
|
||||
zonaRepo: zonaRepo,
|
||||
db: db,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -185,7 +185,7 @@ func RegisterDependencies(injector *do.Injector) {
|
|||
uomServ := uomService.NewUomService(uomRepository, sequenceServ, db, log)
|
||||
mvendorServ := mvendorService.NewVendorService(mvendorRepository, sequenceServ, db)
|
||||
warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db, log)
|
||||
zonaServ := zonaService.NewZonaService(zonaRepository, db)
|
||||
zonaServ := zonaService.NewZonaService(zonaRepository, db, log)
|
||||
aisleServ := aisleService.NewAisleService(aisleRepository, db)
|
||||
inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ)
|
||||
assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository)
|
||||
|
|
|
|||
Loading…
Reference in New Issue