From 79700420d49945438747148411d5e4308bd857bf Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Fri, 17 Oct 2025 12:04:19 +0700 Subject: [PATCH] fix(BE): add missing product json in transfer get all & support flag param filter in product warehouses --- .../product_warehouse.controller.go | 3 +- .../services/product_warehouse.service.go | 12 +++- .../product_warehouse.validation.go | 9 +-- .../inventory/transfers/dto/transfer.dto.go | 64 ++++++++++++++----- .../transfers/services/transfer.service.go | 2 + 5 files changed, 66 insertions(+), 24 deletions(-) diff --git a/internal/modules/inventory/product-warehouses/controllers/product_warehouse.controller.go b/internal/modules/inventory/product-warehouses/controllers/product_warehouse.controller.go index a0b72a4d..f21eef96 100644 --- a/internal/modules/inventory/product-warehouses/controllers/product_warehouse.controller.go +++ b/internal/modules/inventory/product-warehouses/controllers/product_warehouse.controller.go @@ -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), }) } - - diff --git a/internal/modules/inventory/product-warehouses/services/product_warehouse.service.go b/internal/modules/inventory/product-warehouses/services/product_warehouse.service.go index 9afe5707..4871dfe1 100644 --- a/internal/modules/inventory/product-warehouses/services/product_warehouse.service.go +++ b/internal/modules/inventory/product-warehouses/services/product_warehouse.service.go @@ -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") }) diff --git a/internal/modules/inventory/product-warehouses/validations/product_warehouse.validation.go b/internal/modules/inventory/product-warehouses/validations/product_warehouse.validation.go index 02648300..30a5bed1 100644 --- a/internal/modules/inventory/product-warehouses/validations/product_warehouse.validation.go +++ b/internal/modules/inventory/product-warehouses/validations/product_warehouse.validation.go @@ -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"` } diff --git a/internal/modules/inventory/transfers/dto/transfer.dto.go b/internal/modules/inventory/transfers/dto/transfer.dto.go index 217e5038..1b08ecbb 100644 --- a/internal/modules/inventory/transfers/dto/transfer.dto.go +++ b/internal/modules/inventory/transfers/dto/transfer.dto.go @@ -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, diff --git a/internal/modules/inventory/transfers/services/transfer.service.go b/internal/modules/inventory/transfers/services/transfer.service.go index 7f18d257..4579d4c0 100644 --- a/internal/modules/inventory/transfers/services/transfer.service.go +++ b/internal/modules/inventory/transfers/services/transfer.service.go @@ -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") }