From 4af631a1d3283dc2382f5294dc49d30e4cc71bb6 Mon Sep 17 00:00:00 2001 From: giovanni-ce Date: Thu, 4 Dec 2025 16:08:30 +0700 Subject: [PATCH] adjust response inventory stock-product --- .../product-stocks/dto/product-stock.dto.go | 38 ++++++++++++++----- .../services/product-stock.service.go | 1 + 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/internal/modules/inventory/product-stocks/dto/product-stock.dto.go b/internal/modules/inventory/product-stocks/dto/product-stock.dto.go index 2bad7ae7..e571d2b6 100644 --- a/internal/modules/inventory/product-stocks/dto/product-stock.dto.go +++ b/internal/modules/inventory/product-stocks/dto/product-stock.dto.go @@ -34,6 +34,7 @@ type ProductStockListDTO struct { UpdatedAt time.Time `json:"updated_at"` Suppliers []SupplierDTO `json:"suppliers,omitempty"` ProductWarehouses []ProductWarehouseDTO `json:"product_warehouses,omitempty"` + TotalStock float64 `json:"total_stock"` } type ProductStockDetailDTO struct { @@ -58,15 +59,16 @@ type ProductWarehouseDTO struct { } type StockLogDetailDTO struct { - Id uint `json:"id"` - Increase float64 `json:"increase"` - Decrease float64 `json:"decrease"` - LoggableType string `json:"loggable_type"` - LoggableId uint `json:"loggable_id"` - Notes *string `json:"notes"` - ProductWarehouseId uint `json:"product_warehouse_id"` - CreatedBy uint `json:"created_by"` - CreatedAt time.Time `json:"created_at"` + Id uint `json:"id"` + Increase float64 `json:"increase"` + Decrease float64 `json:"decrease"` + LoggableType string `json:"loggable_type"` + LoggableId uint `json:"loggable_id"` + Notes *string `json:"notes"` + ProductWarehouseId uint `json:"product_warehouse_id"` + CreatedBy uint `json:"created_by"` + CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"` + CreatedAt time.Time `json:"created_at"` } // === Mapper Functions === @@ -110,6 +112,7 @@ func ToProductStockListDTO(e entity.Product) ProductStockListDTO { CreatedUser: createdUser, ProductCategory: categoryRef, Suppliers: mapSupplierDTOs(e.ProductSuppliers), + TotalStock: calculateTotalStock(e.ProductWarehouses), } } @@ -197,8 +200,25 @@ func mapStockLogs(src []entity.StockLog) []StockLogDetailDTO { Notes: notes, ProductWarehouseId: log.ProductWarehouseId, CreatedBy: log.CreatedBy, + CreatedUser: mapCreatedUser(log.CreatedUser), CreatedAt: log.CreatedAt, }) } return result } + +func mapCreatedUser(user *entity.User) *userDTO.UserRelationDTO { + if user == nil || user.Id == 0 { + return nil + } + mapped := userDTO.ToUserRelationDTO(*user) + return &mapped +} + +func calculateTotalStock(productWarehouses []entity.ProductWarehouse) float64 { + var total float64 + for _, pw := range productWarehouses { + total += pw.Quantity + } + return total +} diff --git a/internal/modules/inventory/product-stocks/services/product-stock.service.go b/internal/modules/inventory/product-stocks/services/product-stock.service.go index 4de4af67..a0765d84 100644 --- a/internal/modules/inventory/product-stocks/services/product-stock.service.go +++ b/internal/modules/inventory/product-stocks/services/product-stock.service.go @@ -49,6 +49,7 @@ func (s productStockService) withRelations(db *gorm.DB) *gorm.DB { Preload("ProductWarehouses.StockLogs", func(db *gorm.DB) *gorm.DB { return db.Order("created_at ASC") }). + Preload("ProductWarehouses.StockLogs.CreatedUser"). Preload("ProductSuppliers"). Preload("ProductSuppliers.Supplier", func(db *gorm.DB) *gorm.DB { return db.Order("suppliers.name ASC")