From c91d84b652468a7a19e9149518ff8dffe9e8e2b5 Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Fri, 31 Oct 2025 14:30:45 +0700 Subject: [PATCH] feat[BE-127]: inisiate transfer laying for base template API --- internal/entities/laying_kandang_transfer.go | 22 +++++ internal/entities/laying_transfer.go | 24 +++++ internal/entities/transfer_laying.go | 18 ---- .../dto/transfer_laying.dto.go | 12 +-- ...itory.go => laying_transfer.repository.go} | 9 +- .../services/transfer_laying.service.go | 88 ++++++++++++++----- .../validations/transfer_laying.validation.go | 13 ++- 7 files changed, 134 insertions(+), 52 deletions(-) create mode 100644 internal/entities/laying_kandang_transfer.go create mode 100644 internal/entities/laying_transfer.go delete mode 100644 internal/entities/transfer_laying.go rename internal/modules/production/transfer_layings/repositories/{transfer_laying.repository.go => laying_transfer.repository.go} (67%) diff --git a/internal/entities/laying_kandang_transfer.go b/internal/entities/laying_kandang_transfer.go new file mode 100644 index 00000000..8f514f71 --- /dev/null +++ b/internal/entities/laying_kandang_transfer.go @@ -0,0 +1,22 @@ +package entities + +import ( + "time" + + "gorm.io/gorm" +) + +type LayingKandangTransfer struct { + Id uint `gorm:"primaryKey"` + KandangId uint + ProductWarehouseId uint + Qty float64 `gorm:"type:numeric(15,3)"` + LayingTransferId uint `gorm:"not null"` + CreatedAt time.Time `gorm:"autoCreateTime"` + UpdatedAt time.Time `gorm:"autoUpdateTime"` + DeletedAt gorm.DeletedAt `gorm:"index"` + + Kandang *Kandang `gorm:"foreignKey:KandangId;references:Id"` + ProductWarehouse *ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"` + LayingTransfer *LayingTransfer `gorm:"foreignKey:LayingTransferId;references:Id"` +} diff --git a/internal/entities/laying_transfer.go b/internal/entities/laying_transfer.go new file mode 100644 index 00000000..75c5e23a --- /dev/null +++ b/internal/entities/laying_transfer.go @@ -0,0 +1,24 @@ +package entities + +import ( + "time" + + "gorm.io/gorm" +) + +type LayingTransfer struct { + Id uint `gorm:"primaryKey"` + FromProjectFlockId uint `gorm:"not null"` + ToProjectFlockId uint `gorm:"not null"` + TotalQty float64 `gorm:"type:numeric(15,3)"` + TransferDate time.Time `gorm:"type:date;not null"` + Notes string `gorm:"type:text"` + CreatedBy uint `gorm:"not null"` + CreatedAt time.Time `gorm:"autoCreateTime"` + UpdatedAt time.Time `gorm:"autoUpdateTime"` + DeletedAt gorm.DeletedAt `gorm:"index"` + + FromProjectFlock *ProjectFlock `gorm:"foreignKey:FromProjectFlockId;references:Id"` + ToProjectFlock *ProjectFlock `gorm:"foreignKey:ToProjectFlockId;references:Id"` + CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"` +} diff --git a/internal/entities/transfer_laying.go b/internal/entities/transfer_laying.go deleted file mode 100644 index fd60a435..00000000 --- a/internal/entities/transfer_laying.go +++ /dev/null @@ -1,18 +0,0 @@ -package entities - -import ( - "time" - - "gorm.io/gorm" -) - -type TransferLaying struct { - Id uint `gorm:"primaryKey"` - Name string `gorm:"not null;uniqueIndex:idx_name,where:deleted_at IS NULL"` - CreatedBy uint `gorm:"not null"` - CreatedAt time.Time `gorm:"autoCreateTime"` - UpdatedAt time.Time `gorm:"autoUpdateTime"` - DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` - - CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"` -} diff --git a/internal/modules/production/transfer_layings/dto/transfer_laying.dto.go b/internal/modules/production/transfer_layings/dto/transfer_laying.dto.go index 103c4c35..69aced33 100644 --- a/internal/modules/production/transfer_layings/dto/transfer_laying.dto.go +++ b/internal/modules/production/transfer_layings/dto/transfer_laying.dto.go @@ -27,17 +27,17 @@ type TransferLayingDetailDTO struct { // === Mapper Functions === -func ToTransferLayingBaseDTO(e entity.TransferLaying) TransferLayingBaseDTO { +func ToTransferLayingBaseDTO(e entity.LayingTransfer) TransferLayingBaseDTO { return TransferLayingBaseDTO{ Id: e.Id, - Name: e.Name, + } } -func ToTransferLayingListDTO(e entity.TransferLaying) TransferLayingListDTO { +func ToTransferLayingListDTO(e entity.LayingTransfer) TransferLayingListDTO { var createdUser *userDTO.UserBaseDTO if e.CreatedUser.Id != 0 { - mapped := userDTO.ToUserBaseDTO(e.CreatedUser) + mapped := userDTO.ToUserBaseDTO(*e.CreatedUser) createdUser = &mapped } @@ -49,7 +49,7 @@ func ToTransferLayingListDTO(e entity.TransferLaying) TransferLayingListDTO { } } -func ToTransferLayingListDTOs(e []entity.TransferLaying) []TransferLayingListDTO { +func ToTransferLayingListDTOs(e []entity.LayingTransfer) []TransferLayingListDTO { result := make([]TransferLayingListDTO, len(e)) for i, r := range e { result[i] = ToTransferLayingListDTO(r) @@ -57,7 +57,7 @@ func ToTransferLayingListDTOs(e []entity.TransferLaying) []TransferLayingListDTO return result } -func ToTransferLayingDetailDTO(e entity.TransferLaying) TransferLayingDetailDTO { +func ToTransferLayingDetailDTO(e entity.LayingTransfer) TransferLayingDetailDTO { return TransferLayingDetailDTO{ TransferLayingListDTO: ToTransferLayingListDTO(e), } diff --git a/internal/modules/production/transfer_layings/repositories/transfer_laying.repository.go b/internal/modules/production/transfer_layings/repositories/laying_transfer.repository.go similarity index 67% rename from internal/modules/production/transfer_layings/repositories/transfer_laying.repository.go rename to internal/modules/production/transfer_layings/repositories/laying_transfer.repository.go index 32ea27ee..e92e8f95 100644 --- a/internal/modules/production/transfer_layings/repositories/transfer_laying.repository.go +++ b/internal/modules/production/transfer_layings/repositories/laying_transfer.repository.go @@ -1,21 +1,22 @@ package repository import ( - entity "gitlab.com/mbugroup/lti-api.git/internal/entities" "gitlab.com/mbugroup/lti-api.git/internal/common/repository" + entity "gitlab.com/mbugroup/lti-api.git/internal/entities" "gorm.io/gorm" ) type TransferLayingRepository interface { - repository.BaseRepository[entity.TransferLaying] + repository.BaseRepository[entity.LayingTransfer] + } type TransferLayingRepositoryImpl struct { - *repository.BaseRepositoryImpl[entity.TransferLaying] + *repository.BaseRepositoryImpl[entity.LayingTransfer] } func NewTransferLayingRepository(db *gorm.DB) TransferLayingRepository { return &TransferLayingRepositoryImpl{ - BaseRepositoryImpl: repository.NewBaseRepository[entity.TransferLaying](db), + BaseRepositoryImpl: repository.NewBaseRepository[entity.LayingTransfer](db), } } diff --git a/internal/modules/production/transfer_layings/services/transfer_laying.service.go b/internal/modules/production/transfer_layings/services/transfer_laying.service.go index 36541600..bc66bd13 100644 --- a/internal/modules/production/transfer_layings/services/transfer_laying.service.go +++ b/internal/modules/production/transfer_layings/services/transfer_laying.service.go @@ -3,7 +3,9 @@ package service import ( "errors" + common "gitlab.com/mbugroup/lti-api.git/internal/common/service" entity "gitlab.com/mbugroup/lti-api.git/internal/entities" + ProjectFlockRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories" repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories" validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/validations" "gitlab.com/mbugroup/lti-api.git/internal/utils" @@ -15,24 +17,28 @@ import ( ) type TransferLayingService interface { - GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.TransferLaying, int64, error) - GetOne(ctx *fiber.Ctx, id uint) (*entity.TransferLaying, error) - CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.TransferLaying, error) - UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.TransferLaying, error) + GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.LayingTransfer, int64, error) + GetOne(ctx *fiber.Ctx, id uint) (*entity.LayingTransfer, error) + CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.LayingTransfer, error) + UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.LayingTransfer, error) DeleteOne(ctx *fiber.Ctx, id uint) error } type transferLayingService struct { - Log *logrus.Logger - Validate *validator.Validate - Repository repository.TransferLayingRepository + Log *logrus.Logger + Validate *validator.Validate + Repository repository.TransferLayingRepository + ProjectFlockRepo ProjectFlockRepository.ProjectflockRepository + ProjectFlockKandangRepo ProjectFlockRepository.ProjectFlockKandangRepository } -func NewTransferLayingService(repo repository.TransferLayingRepository, validate *validator.Validate) TransferLayingService { +func NewTransferLayingService(repo repository.TransferLayingRepository, projectFlockRepo ProjectFlockRepository.ProjectflockRepository, projectFlockKandangRepo ProjectFlockRepository.ProjectFlockKandangRepository, validate *validator.Validate) TransferLayingService { return &transferLayingService{ - Log: utils.Log, - Validate: validate, - Repository: repo, + Log: utils.Log, + Validate: validate, + Repository: repo, + ProjectFlockRepo: projectFlockRepo, + ProjectFlockKandangRepo: projectFlockKandangRepo, } } @@ -40,7 +46,7 @@ func (s transferLayingService) withRelations(db *gorm.DB) *gorm.DB { return db.Preload("CreatedUser") } -func (s transferLayingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.TransferLaying, int64, error) { +func (s transferLayingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.LayingTransfer, int64, error) { if err := s.Validate.Struct(params); err != nil { return nil, 0, err } @@ -62,7 +68,7 @@ func (s transferLayingService) GetAll(c *fiber.Ctx, params *validation.Query) ([ return transferLayings, total, nil } -func (s transferLayingService) GetOne(c *fiber.Ctx, id uint) (*entity.TransferLaying, error) { +func (s transferLayingService) GetOne(c *fiber.Ctx, id uint) (*entity.LayingTransfer, error) { transferLaying, err := s.Repository.GetByID(c.Context(), id, s.withRelations) if errors.Is(err, gorm.ErrRecordNotFound) { return nil, fiber.NewError(fiber.StatusNotFound, "TransferLaying not found") @@ -74,24 +80,62 @@ func (s transferLayingService) GetOne(c *fiber.Ctx, id uint) (*entity.TransferLa return transferLaying, nil } -func (s *transferLayingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.TransferLaying, error) { +func (s *transferLayingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.LayingTransfer, error) { if err := s.Validate.Struct(req); err != nil { return nil, err } - createBody := &entity.TransferLaying{ - Name: req.Name, - } - - if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil { - s.Log.Errorf("Failed to create transferLaying: %+v", err) + if err := common.EnsureRelations(c.Context(), + common.RelationCheck{Name: "Source Project Flock", ID: &req.SourceProjectFlockId, Exists: s.ProjectFlockRepo.IdExists}, + common.RelationCheck{Name: "Target Project Flock", ID: &req.TargetProjectFlockId, Exists: s.ProjectFlockRepo.IdExists}, + ); err != nil { return nil, err } - return s.GetOne(c, createBody.Id) + for _, detail := range req.Details { + if err := common.EnsureRelations(c.Context(), + common.RelationCheck{Name: "Project Flock Kandang", ID: &detail.SourceProjectFlockKandangId, Exists: s.ProjectFlockKandangRepo.IdExists}, + ); err != nil { + return nil, err + } + } + + transferDate, err := utils.ParseDateString(req.TransferDate) + if err != nil { + return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid transfer date format") + } + + var totalQty float64 + for _, item := range req.Details { + totalQty += item.Quantity + } + + err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error { + + createBody := &entity.LayingTransfer{ + Notes: req.Reason, + FromProjectFlockId: req.SourceProjectFlockId, + ToProjectFlockId: req.TargetProjectFlockId, + TransferDate: transferDate, + TotalQty: totalQty, + CreatedBy: 1, //todo : harus diambil dari auth + } + + if err := s.Repository.WithTx(dbTransaction).CreateOne(c.Context(), createBody, nil); err != nil { + return err + } + + return nil + }) + + if err != nil { + return nil, err + } + + return nil, err } -func (s transferLayingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.TransferLaying, error) { +func (s transferLayingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.LayingTransfer, error) { if err := s.Validate.Struct(req); err != nil { return nil, err } diff --git a/internal/modules/production/transfer_layings/validations/transfer_laying.validation.go b/internal/modules/production/transfer_layings/validations/transfer_laying.validation.go index 7d16d3ee..3b83132f 100644 --- a/internal/modules/production/transfer_layings/validations/transfer_laying.validation.go +++ b/internal/modules/production/transfer_layings/validations/transfer_laying.validation.go @@ -1,11 +1,20 @@ package validation +type CreateDetail struct { + SourceProjectFlockKandangId uint `json:"source_project_flock_kandang_id" validate:"required"` + Quantity float64 `json:"quantity" validate:"required,gt=0"` +} + type Create struct { - Name string `json:"name" validate:"required_strict,min=3"` + TransferDate string `json:"transfer_date" validate:"required,datetime=2006-01-02"` + SourceProjectFlockId uint `json:"source_project_flock_id" validate:"required"` + TargetProjectFlockId uint `json:"target_project_flock_id" validate:"required"` + Details []CreateDetail `json:"details" validate:"required,min=1,dive,required"` + Reason string `json:"reason" validate:"omitempty,max=1000"` } type Update struct { - Name *string `json:"name,omitempty" validate:"omitempty"` + Name *string `json:"name,omitempty" validate:"omitempty"` } type Query struct {