Merge branch 'dev/gio' into 'feat/BE/Sprint-6'

adjust response inventory stock-product

See merge request mbugroup/lti-api!79
This commit is contained in:
Hafizh A. Y.
2025-12-04 09:39:25 +00:00
2 changed files with 30 additions and 9 deletions
@@ -34,6 +34,7 @@ type ProductStockListDTO struct {
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
Suppliers []SupplierDTO `json:"suppliers,omitempty"` Suppliers []SupplierDTO `json:"suppliers,omitempty"`
ProductWarehouses []ProductWarehouseDTO `json:"product_warehouses,omitempty"` ProductWarehouses []ProductWarehouseDTO `json:"product_warehouses,omitempty"`
TotalStock float64 `json:"total_stock"`
} }
type ProductStockDetailDTO struct { type ProductStockDetailDTO struct {
@@ -58,15 +59,16 @@ type ProductWarehouseDTO struct {
} }
type StockLogDetailDTO struct { type StockLogDetailDTO struct {
Id uint `json:"id"` Id uint `json:"id"`
Increase float64 `json:"increase"` Increase float64 `json:"increase"`
Decrease float64 `json:"decrease"` Decrease float64 `json:"decrease"`
LoggableType string `json:"loggable_type"` LoggableType string `json:"loggable_type"`
LoggableId uint `json:"loggable_id"` LoggableId uint `json:"loggable_id"`
Notes *string `json:"notes"` Notes *string `json:"notes"`
ProductWarehouseId uint `json:"product_warehouse_id"` ProductWarehouseId uint `json:"product_warehouse_id"`
CreatedBy uint `json:"created_by"` CreatedBy uint `json:"created_by"`
CreatedAt time.Time `json:"created_at"` CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
CreatedAt time.Time `json:"created_at"`
} }
// === Mapper Functions === // === Mapper Functions ===
@@ -110,6 +112,7 @@ func ToProductStockListDTO(e entity.Product) ProductStockListDTO {
CreatedUser: createdUser, CreatedUser: createdUser,
ProductCategory: categoryRef, ProductCategory: categoryRef,
Suppliers: mapSupplierDTOs(e.ProductSuppliers), Suppliers: mapSupplierDTOs(e.ProductSuppliers),
TotalStock: calculateTotalStock(e.ProductWarehouses),
} }
} }
@@ -197,8 +200,25 @@ func mapStockLogs(src []entity.StockLog) []StockLogDetailDTO {
Notes: notes, Notes: notes,
ProductWarehouseId: log.ProductWarehouseId, ProductWarehouseId: log.ProductWarehouseId,
CreatedBy: log.CreatedBy, CreatedBy: log.CreatedBy,
CreatedUser: mapCreatedUser(log.CreatedUser),
CreatedAt: log.CreatedAt, CreatedAt: log.CreatedAt,
}) })
} }
return result 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
}
@@ -49,6 +49,7 @@ func (s productStockService) withRelations(db *gorm.DB) *gorm.DB {
Preload("ProductWarehouses.StockLogs", func(db *gorm.DB) *gorm.DB { Preload("ProductWarehouses.StockLogs", func(db *gorm.DB) *gorm.DB {
return db.Order("created_at ASC") return db.Order("created_at ASC")
}). }).
Preload("ProductWarehouses.StockLogs.CreatedUser").
Preload("ProductSuppliers"). Preload("ProductSuppliers").
Preload("ProductSuppliers.Supplier", func(db *gorm.DB) *gorm.DB { Preload("ProductSuppliers.Supplier", func(db *gorm.DB) *gorm.DB {
return db.Order("suppliers.name ASC") return db.Order("suppliers.name ASC")