fix(BE-273): add object nonstock and supplier in response get one and fix name base to relation in dto

This commit is contained in:
Hafizh A. Y
2025-11-20 14:59:50 +07:00
parent b4b860b9d4
commit 228aedc215
64 changed files with 964 additions and 3576 deletions
@@ -9,7 +9,7 @@ import (
// === DTO Structs ===
type SupplierBaseDTO struct {
type SupplierRelationDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Alias string `json:"alias"`
@@ -17,30 +17,32 @@ type SupplierBaseDTO struct {
}
type SupplierListDTO struct {
SupplierBaseDTO
Pic string `json:"pic"`
Type string `json:"type"`
Hatchery *string `json:"hatchery,omitempty"`
Phone string `json:"phone"`
Email string `json:"email"`
Address string `json:"address"`
Npwp *string `json:"npwp,omitempty"`
AccountNumber *string `json:"account_number,omitempty"`
Balance float64 `json:"balance"`
DueDate int `json:"due_date"`
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
SupplierRelationDTO
Pic string `json:"pic"`
Type string `json:"type"`
Hatchery *string `json:"hatchery,omitempty"`
Phone string `json:"phone"`
Email string `json:"email"`
Address string `json:"address"`
Npwp *string `json:"npwp,omitempty"`
AccountNumber *string `json:"account_number,omitempty"`
Balance float64 `json:"balance"`
DueDate int `json:"due_date"`
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type SupplierDetailDTO struct {
SupplierListDTO
Products []SupplierProductDTO `json:"products"`
Nonstocks []SupplierNonstockDTO `json:"nonstocks"`
}
// === Mapper Functions ===
func ToSupplierBaseDTO(e entity.Supplier) SupplierBaseDTO {
return SupplierBaseDTO{
func ToSupplierRelationDTO(e entity.Supplier) SupplierRelationDTO {
return SupplierRelationDTO{
Id: e.Id,
Name: e.Name,
Alias: e.Alias,
@@ -49,27 +51,27 @@ func ToSupplierBaseDTO(e entity.Supplier) SupplierBaseDTO {
}
func ToSupplierListDTO(e entity.Supplier) SupplierListDTO {
var createdUser *userDTO.UserBaseDTO
var createdUser *userDTO.UserRelationDTO
if e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
createdUser = &mapped
}
return SupplierListDTO{
Pic: e.Pic,
Type: e.Type,
Hatchery: e.Hatchery,
Phone: e.Phone,
Email: e.Email,
Address: e.Address,
Npwp: e.Npwp,
AccountNumber: e.AccountNumber,
Balance: e.Balance,
DueDate: e.DueDate,
SupplierBaseDTO: ToSupplierBaseDTO(e),
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
Pic: e.Pic,
Type: e.Type,
Hatchery: e.Hatchery,
Phone: e.Phone,
Email: e.Email,
Address: e.Address,
Npwp: e.Npwp,
AccountNumber: e.AccountNumber,
Balance: e.Balance,
DueDate: e.DueDate,
SupplierRelationDTO: ToSupplierRelationDTO(e),
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
}
}
@@ -84,5 +86,7 @@ func ToSupplierListDTOs(e []entity.Supplier) []SupplierListDTO {
func ToSupplierDetailDTO(e entity.Supplier) SupplierDetailDTO {
return SupplierDetailDTO{
SupplierListDTO: ToSupplierListDTO(e),
Products: toSupplierProductDTOs(e.ProductSuppliers),
Nonstocks: toSupplierNonstockDTOs(e.NonstockSuppliers),
}
}
@@ -0,0 +1,50 @@
package dto
import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
uomDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/dto"
)
// === DTO Structs ===
type SupplierNonstockDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Uom *uomDTO.UomRelationDTO `json:"uom,omitempty"`
Flags []string `json:"flags"`
}
// === Mapper Functions ===
func toSupplierNonstockDTOs(relations []entity.NonstockSupplier) []SupplierNonstockDTO {
if len(relations) == 0 {
return nil
}
result := make([]SupplierNonstockDTO, 0, len(relations))
for _, relation := range relations {
Nonstock := relation.Nonstock
if Nonstock.Id == 0 {
continue
}
flags := make([]string, len(Nonstock.Flags))
for i, f := range Nonstock.Flags {
flags[i] = f.Name
}
var uomRef *uomDTO.UomRelationDTO
if Nonstock.Uom.Id != 0 {
mapped := uomDTO.ToUomRelationDTO(Nonstock.Uom)
uomRef = &mapped
}
result = append(result, SupplierNonstockDTO{
Id: Nonstock.Id,
Name: Nonstock.Name,
Uom: uomRef,
Flags: flags,
})
}
return result
}
@@ -0,0 +1,54 @@
package dto
import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
uomDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/dto"
)
// === DTO Structs ===
type SupplierProductDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
ProductPrice float64 `gorm:"type:numeric(15,3);not null"`
SellingPrice *float64 `gorm:"type:numeric(15,3)"`
Uom *uomDTO.UomRelationDTO `json:"uom,omitempty"`
Flags []string `json:"flags"`
}
// === Mapper Functions ===
func toSupplierProductDTOs(relations []entity.ProductSupplier) []SupplierProductDTO {
if len(relations) == 0 {
return nil
}
result := make([]SupplierProductDTO, 0, len(relations))
for _, relation := range relations {
product := relation.Product
if product.Id == 0 {
continue
}
flags := make([]string, len(product.Flags))
for i, f := range product.Flags {
flags[i] = f.Name
}
var uomRef *uomDTO.UomRelationDTO
if product.Uom.Id != 0 {
mapped := uomDTO.ToUomRelationDTO(product.Uom)
uomRef = &mapped
}
result = append(result, SupplierProductDTO{
Id: product.Id,
Name: product.Name,
ProductPrice: product.ProductPrice,
SellingPrice: product.SellingPrice,
Uom: uomRef,
Flags: flags,
})
}
return result
}