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
@@ -24,9 +24,10 @@ func NewSupplierController(supplierService service.SupplierService) *SupplierCon
func (u *SupplierController) GetAll(c *fiber.Ctx) error {
query := &validation.Query{
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""),
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""),
Category: c.Query("category", ""),
}
if query.Page < 1 || query.Limit < 1 {
@@ -71,7 +72,7 @@ func (u *SupplierController) GetOne(c *fiber.Ctx) error {
Code: fiber.StatusOK,
Status: "success",
Message: "Get supplier successfully",
Data: dto.ToSupplierListDTO(*result),
Data: dto.ToSupplierDetailDTO(*result),
})
}
@@ -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
}
+2 -2
View File
@@ -1,7 +1,7 @@
package suppliers
import (
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/controllers"
supplier "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
@@ -13,7 +13,7 @@ func SupplierRoutes(v1 fiber.Router, u user.UserService, s supplier.SupplierServ
ctrl := controller.NewSupplierController(s)
route := v1.Group("/suppliers")
route.Use(m.Auth(u))
// route.Use(m.Auth(u))
route.Get("/", ctrl.GetAll)
route.Post("/", ctrl.CreateOne)
@@ -39,7 +39,12 @@ func NewSupplierService(repo repository.SupplierRepository, validate *validator.
}
func (s supplierService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("CreatedUser")
return db.
Preload("CreatedUser").
Preload("ProductSuppliers.Product.Uom").
Preload("ProductSuppliers.Product.Flags").
Preload("NonstockSuppliers.Nonstock.Uom").
Preload("NonstockSuppliers.Nonstock.Flags")
}
func (s supplierService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Supplier, int64, error) {
@@ -47,6 +52,14 @@ func (s supplierService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
return nil, 0, err
}
if params.Category != "" {
category := strings.ToUpper(params.Category)
if category != "BOP" && category != "SAPRONAK" {
return nil, 0, fiber.NewError(fiber.StatusBadRequest, "Invalid supplier category")
}
params.Category = category
}
offset := (params.Page - 1) * params.Limit
suppliers, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
@@ -54,6 +67,11 @@ func (s supplierService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
if params.Search != "" {
return db.Where("name LIKE ?", "%"+params.Search+"%")
}
if params.Category != "" {
db = db.Where("category LIKE ?", "%"+params.Category+"%")
}
return db.Order("created_at DESC").Order("updated_at DESC")
})
@@ -31,7 +31,8 @@ type Update struct {
}
type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
Search string `query:"search" validate:"omitempty,max=50"`
Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
Search string `query:"search" validate:"omitempty,max=50"`
Category string `query:"category" validate:"omitempty,max=50"`
}