fix(BE): add missing product json in transfer get all & support flag param filter in product warehouses

This commit is contained in:
aguhh18
2025-10-17 12:04:19 +07:00
parent c9b4b3008e
commit 79700420d4
5 changed files with 66 additions and 24 deletions
@@ -28,6 +28,7 @@ func (u *ProductWarehouseController) GetAll(c *fiber.Ctx) error {
Limit: c.QueryInt("limit", 10),
ProductId: uint(c.QueryInt("product_id", 0)),
WarehouseId: uint(c.QueryInt("warehouse_id", 0)),
Flag: c.Query("flag"),
}
result, totalResults, err := u.ProductWarehouseService.GetAll(c, query)
@@ -71,5 +72,3 @@ func (u *ProductWarehouseController) GetOne(c *fiber.Ctx) error {
Data: dto.ToProductWarehouseListDTO(*result),
})
}
@@ -34,7 +34,11 @@ func NewProductWarehouseService(repo repository.ProductWarehouseRepository, vali
}
func (s productWarehouseService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("Product.Flags").Preload("Product").Preload("Warehouse").Preload("CreatedUser")
return db.
Preload("Product.Flags").
Preload("Product").
Preload("Warehouse").
Preload("CreatedUser")
}
func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProductWarehouse, int64, error) {
@@ -55,6 +59,12 @@ func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query)
db = db.Where("warehouse_id = ?", params.WarehouseId)
}
if params.Flag != "" {
db = db.Joins("JOIN products ON products.id = product_warehouses.product_id")
db = db.Joins("JOIN flags ON flags.flagable_id = products.id AND flags.flagable_type = ?", "products")
db = db.Where("flags.name = ?", params.Flag)
}
return db.Order("created_at DESC").Order("updated_at DESC")
})
@@ -13,8 +13,9 @@ 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"`
ProductId uint `query:"product_id" validate:"omitempty,number,min=1"`
WarehouseId uint `query:"warehouse_id" validate:"omitempty,number,min=1"`
Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
ProductId uint `query:"product_id" validate:"omitempty,number,min=1"`
WarehouseId uint `query:"warehouse_id" validate:"omitempty,number,min=1"`
Flag string `query:"flag" validate:"omitempty"`
}
@@ -57,17 +57,17 @@ type TransferDetailDTO struct {
// Detail produk
type TransferDetailItemDTO struct {
Id uint64 `json:"id"`
ProductId uint64 `json:"product_id"`
Quantity float64 `json:"quantity"`
BeforeQuantity float64 `json:"before_quantity"`
AfterQuantity float64 `json:"after_quantity"`
Id uint64 `json:"id"`
Product *ProductDTO `json:"product,omitempty"`
Quantity float64 `json:"quantity"`
BeforeQuantity float64 `json:"before_quantity"`
AfterQuantity float64 `json:"after_quantity"`
}
// Delivery ekspedisi
type TransferDeliveryDTO struct {
Id uint64 `json:"id"`
SupplierId uint64 `json:"supplier_id"`
Supplier *SupplierDTO `json:"supplier,omitempty"`
VehiclePlate string `json:"vehicle_plate"`
DriverName string `json:"driver_name"`
DocumentNumber string `json:"document_number"`
@@ -83,6 +83,16 @@ type TransferDeliveryItemDTO struct {
Quantity float64 `json:"quantity"`
}
type ProductDTO struct {
Id uint64 `json:"id"`
Name string `json:"name"`
}
type SupplierDTO struct {
Id uint64 `json:"id"`
Name string `json:"name"`
}
// === Mapper Functions ===
func ToTransferBaseDTO(e entity.StockTransfer) TransferBaseDTO {
@@ -114,6 +124,26 @@ func toAreaDTO(a *entity.Area) *AreaDTO {
}
}
func toProductDTO(p *entity.Product) *ProductDTO {
if p == nil {
return nil
}
return &ProductDTO{
Id: uint64(p.Id),
Name: p.Name,
}
}
func toSupplierDTO(s *entity.Supplier) *SupplierDTO {
if s == nil {
return nil
}
return &SupplierDTO{
Id: uint64(s.Id),
Name: s.Name,
}
}
func toLocationDTO(l *entity.Location) *LocationDTO {
if l == nil {
return nil
@@ -142,19 +172,19 @@ func ToTransferListDTO(e entity.StockTransfer) TransferListDTO {
mapped := userDTO.ToUserBaseDTO(*e.CreatedUser)
createdUser = &mapped
}
// Map details
var details []TransferDetailItemDTO
for _, d := range e.Details {
details = append(details, TransferDetailItemDTO{
Id: d.Id,
ProductId: d.ProductId,
Quantity: d.Quantity,
Id: d.Id,
Product: toProductDTO(d.Product),
Quantity: d.Quantity,
})
}
// Map deliveries
var deliveries []TransferDeliveryDTO
for _, del := range e.Deliveries {
// Map delivery items
var items []TransferDeliveryItemDTO
for _, item := range del.Items {
items = append(items, TransferDeliveryItemDTO{
@@ -165,8 +195,8 @@ func ToTransferListDTO(e entity.StockTransfer) TransferListDTO {
}
deliveries = append(deliveries, TransferDeliveryDTO{
Id: del.Id,
SupplierId: del.SupplierId,
VehiclePlate: del.VehiclePlate,
Supplier: toSupplierDTO(del.Supplier),
DriverName: del.DriverName,
DocumentNumber: del.DocumentNumber,
DocumentPath: del.DocumentPath,
@@ -198,9 +228,9 @@ func ToTransferDetailDTO(e entity.StockTransfer) TransferDetailDTO {
var details []TransferDetailItemDTO
for _, d := range e.Details {
details = append(details, TransferDetailItemDTO{
Id: d.Id,
ProductId: d.ProductId,
Quantity: d.Quantity,
Id: d.Id,
Product: toProductDTO(d.Product),
Quantity: d.Quantity,
})
}
// Map deliveries
@@ -208,7 +238,7 @@ func ToTransferDetailDTO(e entity.StockTransfer) TransferDetailDTO {
for _, del := range e.Deliveries {
deliveries = append(deliveries, TransferDeliveryDTO{
Id: del.Id,
SupplierId: del.SupplierId,
Supplier: toSupplierDTO(del.Supplier),
VehiclePlate: del.VehiclePlate,
DriverName: del.DriverName,
DocumentNumber: del.DocumentNumber,
@@ -60,6 +60,8 @@ func (s transferService) withRelations(db *gorm.DB) *gorm.DB {
Preload("ToWarehouse.Location").
Preload("ToWarehouse.Area").
Preload("Details").
Preload("Details.Product").
Preload("Deliveries.Supplier").
Preload("Deliveries.Items")
}