mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-26 00:05:44 +00:00
feat[BE-127]: Createing transfer laying create one, approvals, get one, get all, update, delete, but Still unfinished
This commit is contained in:
@@ -4,61 +4,252 @@ import (
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
|
||||
type TransferLayingBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Id uint `json:"id"`
|
||||
TransferNumber string `json:"transfer_number"`
|
||||
TransferDate time.Time `json:"transfer_date"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
type ProjectFlockSummaryDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Period int `json:"period"`
|
||||
Category string `json:"category"`
|
||||
}
|
||||
|
||||
type ProductSummaryDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type WarehouseSummaryDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type ProductWarehouseSummaryDTO struct {
|
||||
Product *ProductSummaryDTO `json:"product,omitempty"`
|
||||
Warehouse *WarehouseSummaryDTO `json:"warehouse,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectFlockKandangSummaryDTO struct {
|
||||
Id uint `json:"id"`
|
||||
KandangId uint `json:"kandang_id"`
|
||||
}
|
||||
|
||||
type LayingTransferSourceDTO struct {
|
||||
SourceProjectFlockKandang *ProjectFlockKandangSummaryDTO `json:"source_project_flock_kandang,omitempty"`
|
||||
Qty float64 `json:"qty"`
|
||||
ProductWarehouse *ProductWarehouseSummaryDTO `json:"product_warehouse,omitempty"`
|
||||
Note string `json:"note,omitempty"`
|
||||
}
|
||||
|
||||
type LayingTransferTargetDTO struct {
|
||||
TargetProjectFlockKandang *ProjectFlockKandangSummaryDTO `json:"target_project_flock_kandang,omitempty"`
|
||||
Qty float64 `json:"qty"`
|
||||
ProductWarehouse *ProductWarehouseSummaryDTO `json:"product_warehouse,omitempty"`
|
||||
Note string `json:"note,omitempty"`
|
||||
}
|
||||
|
||||
type TransferLayingListDTO struct {
|
||||
TransferLayingBaseDTO
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
FromProjectFlock *ProjectFlockSummaryDTO `json:"from_project_flock,omitempty"`
|
||||
ToProjectFlock *ProjectFlockSummaryDTO `json:"to_project_flock,omitempty"`
|
||||
PendingUsageQty *float64 `json:"pending_usage_qty"`
|
||||
UsageQty *float64 `json:"usage_qty"`
|
||||
CreatedBy uint `json:"created_by"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Approval *approvalDTO.ApprovalBaseDTO `json:"approval,omitempty"`
|
||||
}
|
||||
|
||||
type TransferLayingDetailDTO struct {
|
||||
TransferLayingListDTO
|
||||
Sources []LayingTransferSourceDTO `json:"sources,omitempty"`
|
||||
Targets []LayingTransferTargetDTO `json:"targets,omitempty"`
|
||||
Approval *approvalDTO.ApprovalBaseDTO `json:"approval,omitempty"`
|
||||
}
|
||||
|
||||
// === Mapper Functions ===
|
||||
|
||||
func ToProjectFlockSummaryDTO(pf *entity.ProjectFlock) *ProjectFlockSummaryDTO {
|
||||
if pf == nil || pf.Id == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ProjectFlockSummaryDTO{
|
||||
Id: pf.Id,
|
||||
Period: pf.Period,
|
||||
Category: pf.Category,
|
||||
}
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangSummaryDTO(pfk *entity.ProjectFlockKandang) *ProjectFlockKandangSummaryDTO {
|
||||
if pfk == nil || pfk.Id == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ProjectFlockKandangSummaryDTO{
|
||||
Id: pfk.Id,
|
||||
KandangId: pfk.KandangId,
|
||||
}
|
||||
}
|
||||
|
||||
func ToProductSummaryDTO(product *entity.Product) *ProductSummaryDTO {
|
||||
if product == nil || product.Id == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ProductSummaryDTO{
|
||||
Id: product.Id,
|
||||
Name: product.Name,
|
||||
}
|
||||
}
|
||||
|
||||
func ToWarehouseSummaryDTO(warehouse *entity.Warehouse) *WarehouseSummaryDTO {
|
||||
if warehouse == nil || warehouse.Id == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &WarehouseSummaryDTO{
|
||||
Id: warehouse.Id,
|
||||
Name: warehouse.Name,
|
||||
Type: warehouse.Type,
|
||||
}
|
||||
}
|
||||
|
||||
func ToProductWarehouseSummaryDTO(pw *entity.ProductWarehouse) *ProductWarehouseSummaryDTO {
|
||||
if pw == nil || pw.Id == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ProductWarehouseSummaryDTO{
|
||||
Product: ToProductSummaryDTO(&pw.Product),
|
||||
Warehouse: ToWarehouseSummaryDTO(&pw.Warehouse),
|
||||
}
|
||||
}
|
||||
|
||||
func ToLayingTransferSourceDTO(source entity.LayingTransferSource) LayingTransferSourceDTO {
|
||||
return LayingTransferSourceDTO{
|
||||
SourceProjectFlockKandang: ToProjectFlockKandangSummaryDTO(source.SourceProjectFlockKandang),
|
||||
Qty: source.Qty,
|
||||
ProductWarehouse: ToProductWarehouseSummaryDTO(source.ProductWarehouse),
|
||||
Note: source.Note,
|
||||
}
|
||||
}
|
||||
|
||||
func ToLayingTransferSourceDTOs(sources []entity.LayingTransferSource) []LayingTransferSourceDTO {
|
||||
if len(sources) == 0 {
|
||||
return []LayingTransferSourceDTO{}
|
||||
}
|
||||
result := make([]LayingTransferSourceDTO, len(sources))
|
||||
for i, s := range sources {
|
||||
result[i] = ToLayingTransferSourceDTO(s)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ToLayingTransferTargetDTO(target entity.LayingTransferTarget) LayingTransferTargetDTO {
|
||||
return LayingTransferTargetDTO{
|
||||
TargetProjectFlockKandang: ToProjectFlockKandangSummaryDTO(target.TargetProjectFlockKandang),
|
||||
Qty: target.Qty,
|
||||
ProductWarehouse: ToProductWarehouseSummaryDTO(target.ProductWarehouse),
|
||||
Note: target.Note,
|
||||
}
|
||||
}
|
||||
|
||||
func ToLayingTransferTargetDTOs(targets []entity.LayingTransferTarget) []LayingTransferTargetDTO {
|
||||
if len(targets) == 0 {
|
||||
return []LayingTransferTargetDTO{}
|
||||
}
|
||||
result := make([]LayingTransferTargetDTO, len(targets))
|
||||
for i, t := range targets {
|
||||
result[i] = ToLayingTransferTargetDTO(t)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ToTransferLayingBaseDTO(e entity.LayingTransfer) TransferLayingBaseDTO {
|
||||
return TransferLayingBaseDTO{
|
||||
Id: e.Id,
|
||||
|
||||
Id: e.Id,
|
||||
TransferNumber: e.TransferNumber,
|
||||
TransferDate: e.TransferDate,
|
||||
Notes: e.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
func ToTransferLayingListDTO(e entity.LayingTransfer) TransferLayingListDTO {
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if e.CreatedUser.Id != 0 {
|
||||
if e.CreatedUser != nil && e.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(*e.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
return TransferLayingListDTO{
|
||||
TransferLayingBaseDTO: ToTransferLayingBaseDTO(e),
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
CreatedUser: createdUser,
|
||||
FromProjectFlock: ToProjectFlockSummaryDTO(e.FromProjectFlock),
|
||||
ToProjectFlock: ToProjectFlockSummaryDTO(e.ToProjectFlock),
|
||||
PendingUsageQty: e.PendingUsageQty,
|
||||
UsageQty: e.UsageQty,
|
||||
CreatedBy: e.CreatedBy,
|
||||
CreatedUser: createdUser,
|
||||
CreatedAt: e.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToTransferLayingListDTOs(e []entity.LayingTransfer) []TransferLayingListDTO {
|
||||
result := make([]TransferLayingListDTO, len(e))
|
||||
for i, r := range e {
|
||||
result[i] = ToTransferLayingListDTO(r)
|
||||
func ToTransferLayingDetailDTO(e entity.LayingTransfer, approvals []entity.Approval) TransferLayingDetailDTO {
|
||||
var latestApproval *approvalDTO.ApprovalBaseDTO
|
||||
|
||||
// Use LatestApproval from entity if available
|
||||
if e.LatestApproval != nil {
|
||||
mapped := approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||
latestApproval = &mapped
|
||||
} else if len(approvals) > 0 {
|
||||
// Fallback to approvals slice
|
||||
latest := approvalDTO.ToApprovalDTO(approvals[len(approvals)-1])
|
||||
latestApproval = &latest
|
||||
}
|
||||
|
||||
return TransferLayingDetailDTO{
|
||||
TransferLayingListDTO: ToTransferLayingListDTO(e),
|
||||
Sources: ToLayingTransferSourceDTOs(e.Sources),
|
||||
Targets: ToLayingTransferTargetDTOs(e.Targets),
|
||||
Approval: latestApproval,
|
||||
}
|
||||
}
|
||||
|
||||
func ToTransferLayingDetailDTOWithSingleApproval(e entity.LayingTransfer, approval *entity.Approval) TransferLayingDetailDTO {
|
||||
var mappedApproval *approvalDTO.ApprovalBaseDTO
|
||||
|
||||
// Prefer LatestApproval from entity
|
||||
if e.LatestApproval != nil && e.LatestApproval.Id != 0 {
|
||||
mapped := approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||
mappedApproval = &mapped
|
||||
} else if approval != nil && approval.Id != 0 {
|
||||
// Fallback to passed approval parameter
|
||||
mapped := approvalDTO.ToApprovalDTO(*approval)
|
||||
mappedApproval = &mapped
|
||||
}
|
||||
|
||||
return TransferLayingDetailDTO{
|
||||
TransferLayingListDTO: ToTransferLayingListDTO(e),
|
||||
Sources: ToLayingTransferSourceDTOs(e.Sources),
|
||||
Targets: ToLayingTransferTargetDTOs(e.Targets),
|
||||
Approval: mappedApproval,
|
||||
}
|
||||
}
|
||||
|
||||
func ToTransferLayingListDTOs(items []entity.LayingTransfer) []TransferLayingListDTO {
|
||||
result := make([]TransferLayingListDTO, len(items))
|
||||
for i, item := range items {
|
||||
result[i] = ToTransferLayingListDTO(item)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ToTransferLayingDetailDTO(e entity.LayingTransfer) TransferLayingDetailDTO {
|
||||
return TransferLayingDetailDTO{
|
||||
TransferLayingListDTO: ToTransferLayingListDTO(e),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user