change product list to retrieve its data from product outlets
This commit is contained in:
@@ -47,15 +47,17 @@ type ProductProcessorImpl struct {
|
||||
productVariantRepo repository.ProductVariantRepository
|
||||
inventoryRepo repository.InventoryRepository
|
||||
outletRepo OutletRepository
|
||||
outletPriceRepo repository.ProductOutletPriceRepository
|
||||
}
|
||||
|
||||
func NewProductProcessorImpl(productRepo ProductRepository, categoryRepo CategoryRepository, productVariantRepo repository.ProductVariantRepository, inventoryRepo repository.InventoryRepository, outletRepo OutletRepository) *ProductProcessorImpl {
|
||||
func NewProductProcessorImpl(productRepo ProductRepository, categoryRepo CategoryRepository, productVariantRepo repository.ProductVariantRepository, inventoryRepo repository.InventoryRepository, outletRepo OutletRepository, outletPriceRepo repository.ProductOutletPriceRepository) *ProductProcessorImpl {
|
||||
return &ProductProcessorImpl{
|
||||
productRepo: productRepo,
|
||||
categoryRepo: categoryRepo,
|
||||
productVariantRepo: productVariantRepo,
|
||||
inventoryRepo: inventoryRepo,
|
||||
outletRepo: outletRepo,
|
||||
outletPriceRepo: outletPriceRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,11 +229,36 @@ func (p *ProductProcessorImpl) GetProductByID(ctx context.Context, id uuid.UUID)
|
||||
func (p *ProductProcessorImpl) ListProducts(ctx context.Context, filters map[string]interface{}, page, limit int) ([]models.ProductResponse, int, error) {
|
||||
offset := (page - 1) * limit
|
||||
|
||||
var outletID uuid.UUID
|
||||
if oid, ok := filters["outlet_id"]; ok {
|
||||
outletID = oid.(uuid.UUID)
|
||||
delete(filters, "outlet_id")
|
||||
}
|
||||
|
||||
productEntities, total, err := p.productRepo.List(ctx, filters, limit, offset)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to list products: %w", err)
|
||||
}
|
||||
|
||||
if outletID != uuid.Nil && len(productEntities) > 0 {
|
||||
productIDs := make([]uuid.UUID, len(productEntities))
|
||||
for i, pe := range productEntities {
|
||||
productIDs[i] = pe.ID
|
||||
}
|
||||
outletPrices, err := p.outletPriceRepo.GetByProductsAndOutlet(ctx, productIDs, outletID)
|
||||
if err == nil {
|
||||
priceMap := make(map[uuid.UUID]float64, len(outletPrices))
|
||||
for _, op := range outletPrices {
|
||||
priceMap[op.ProductID] = op.Price
|
||||
}
|
||||
for _, pe := range productEntities {
|
||||
if price, ok := priceMap[pe.ID]; ok {
|
||||
pe.Price = price
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
responses := make([]models.ProductResponse, len(productEntities))
|
||||
for i, entity := range productEntities {
|
||||
response := mappers.ProductEntityToResponse(entity)
|
||||
|
||||
Reference in New Issue
Block a user