Skip to content

Commit

Permalink
Merge pull request #215 from ISPP-07/fix/214-update-product
Browse files Browse the repository at this point in the history
Fix/214 update product
  • Loading branch information
josgarber6 authored Apr 1, 2024
2 parents f4f7e6f + 2f4ab77 commit d4e746c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/modules/cyc/warehouse/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ async def update_product_controller(
result = []
warehouses_id = [p.warehouse_id for p in update_products.products]
warehouses = await service.get_warehouses_service(db, query={'id': {'$in': warehouses_id}})
products_by_warehouse: dict[UUID4, list[model.Product]] = {}
products_by_warehouse: dict[UUID4, list[dict]] = {}
product_ids_count = Counter(p.product_id for p in update_products.products)
if any(value > 1 for value in product_ids_count.values):
if any(value > 1 for value in product_ids_count.values()):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail='There cannot be duplicated products'
Expand All @@ -142,15 +142,15 @@ async def update_product_controller(
lambda p: p.id == product.product_id,
warehouse.products
)).pop()
warehouse.products.remove(old_product)
updated_product = model.Product(
id=product.product_id,
name=old_product.name if product.name is None else product.name,
quantity=old_product.quantity if product.quantity is None else product. quantity,
exp_date=product.exp_date if product.update_exp_date else old_product.exp_date)
if warehouse.id not in products_by_warehouse:
products_by_warehouse[warehouse.id] = []
products_by_warehouse[warehouse.id].append(updated_product)
products_by_warehouse[warehouse.id].append(
updated_product.model_dump())
result.append(model.ProductOut(
id=product.product_id,
warehouse_id=warehouse.id,
Expand All @@ -160,12 +160,13 @@ async def update_product_controller(
))
for key, value in products_by_warehouse.items():
warehouse = [w for w in warehouses if w.id == key][0]
value_ids = [p['id'] for p in value]
await service.update_warehouse_service(
db,
warehouse_id=warehouse.id,
warehouse_update=model.WarehouseUpdate(
products=warehouse.products + value
)
warehouse_update={
'products': [p.model_dump() for p in warehouse.products if p.id not in value_ids] + value
}
)
return result

Expand Down

0 comments on commit d4e746c

Please sign in to comment.