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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -63,3 +65,34 @@ type ZonaResponse struct {
|
||||||
Warehouse pkgdto.IdNameResponse `json:"warehouse"`
|
Warehouse pkgdto.IdNameResponse `json:"warehouse"`
|
||||||
Client pkgdto.IdNameResponse `json:"client"`
|
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/dto"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/zona/query"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/zona/query"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/zona/repository"
|
"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/google/uuid"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -23,38 +25,7 @@ type ZonaService interface {
|
||||||
type zonaService struct {
|
type zonaService struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
zonaRepo repository.ZonaRepository
|
zonaRepo repository.ZonaRepository
|
||||||
}
|
log *logrus.Logger
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *zonaService) Create(ctx context.Context, req dto.ZonaCreateRequest) (dto.ZonaResponse, error) {
|
func (s *zonaService) Create(ctx context.Context, req dto.ZonaCreateRequest) (dto.ZonaResponse, error) {
|
||||||
|
|
@ -80,10 +51,9 @@ func (s *zonaService) Create(ctx context.Context, req dto.ZonaCreateRequest) (dt
|
||||||
Type: req.Type,
|
Type: req.Type,
|
||||||
Temperature: req.Temperature,
|
Temperature: req.Temperature,
|
||||||
Hazardous: req.Hazardous,
|
Hazardous: req.Hazardous,
|
||||||
// QRCodeZone: req.QRCodeZone,
|
|
||||||
// IsActive: req.IsActive,
|
|
||||||
WarehouseID: warehouseUUID,
|
WarehouseID: warehouseUUID,
|
||||||
ClientID: clientUUID,
|
ClientID: clientUUID,
|
||||||
|
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||||
}
|
}
|
||||||
created, err := s.zonaRepo.Create(ctx, tx, zona)
|
created, err := s.zonaRepo.Create(ctx, tx, zona)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -95,7 +65,13 @@ func (s *zonaService) Create(ctx context.Context, req dto.ZonaCreateRequest) (dt
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.ZonaResponse{}, err
|
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) {
|
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 {
|
if err != nil {
|
||||||
return dto.ZonaResponse{}, err
|
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) {
|
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
|
var responses []dto.ZonaResponse
|
||||||
for _, e := range zonas {
|
for _, e := range zonas {
|
||||||
responses = append(responses, toZonaResponse(e))
|
responses = append(responses, dto.ToZonaResponse(e))
|
||||||
}
|
}
|
||||||
if responses == nil {
|
if responses == nil {
|
||||||
responses = make([]dto.ZonaResponse, 0)
|
responses = make([]dto.ZonaResponse, 0)
|
||||||
|
|
@ -133,6 +109,7 @@ func (s *zonaService) Update(ctx context.Context, req dto.ZonaUpdateRequest, zon
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return dto.ZonaResponse{}, err
|
return dto.ZonaResponse{}, err
|
||||||
}
|
}
|
||||||
|
before := zona
|
||||||
if req.Code != "" {
|
if req.Code != "" {
|
||||||
zona.Code = 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.Temperature = req.Temperature
|
||||||
zona.Hazardous = req.Hazardous
|
zona.Hazardous = req.Hazardous
|
||||||
// zona.QRCodeZone = req.QRCodeZone
|
|
||||||
zona.IsActive = req.IsActive
|
zona.IsActive = req.IsActive
|
||||||
// if req.WarehouseID != "" {
|
zona.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||||
// 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
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
updated, err := s.zonaRepo.Update(ctx, tx, zona)
|
updated, err := s.zonaRepo.Update(ctx, tx, zona)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|
@ -168,7 +133,15 @@ func (s *zonaService) Update(ctx context.Context, req dto.ZonaUpdateRequest, zon
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.ZonaResponse{}, err
|
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 {
|
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()
|
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 {
|
if err := s.zonaRepo.Delete(ctx, tx, zonaId); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
s.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": utils.GetUserID(ctx),
|
||||||
|
"action": "delete",
|
||||||
|
"entity": "zona",
|
||||||
|
"entity_id": zonaId,
|
||||||
|
}).Info("Zona deleted")
|
||||||
return nil
|
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{
|
return &zonaService{
|
||||||
zonaRepo: zonaRepo,
|
zonaRepo: zonaRepo,
|
||||||
db: db,
|
db: db,
|
||||||
|
log: log,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -185,7 +185,7 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
uomServ := uomService.NewUomService(uomRepository, sequenceServ, db, log)
|
uomServ := uomService.NewUomService(uomRepository, sequenceServ, db, log)
|
||||||
mvendorServ := mvendorService.NewVendorService(mvendorRepository, sequenceServ, db)
|
mvendorServ := mvendorService.NewVendorService(mvendorRepository, sequenceServ, db)
|
||||||
warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db, log)
|
warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db, log)
|
||||||
zonaServ := zonaService.NewZonaService(zonaRepository, db)
|
zonaServ := zonaService.NewZonaService(zonaRepository, db, log)
|
||||||
aisleServ := aisleService.NewAisleService(aisleRepository, db)
|
aisleServ := aisleService.NewAisleService(aisleRepository, db)
|
||||||
inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ)
|
inventoryReceiptServ := inventoryReceiptService.NewInventoryReceiptService(db, inventoryReceiptRepository, inventoryReceiptLineRepository, productRepository, uomRepository, inventoryStorageRepository, sequenceServ)
|
||||||
assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository)
|
assignmentServ := assignmentService.NewAssignmentService(db, assignmentRepository, assignmentUserRepository)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue