58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
from time import time
|
|
|
|
from fastapi import APIRouter
|
|
|
|
from app import mongo
|
|
from app.utils.response_util import response
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/add")
|
|
async def add_product(params: dict):
|
|
start_time = time()
|
|
|
|
deal_id = params["dealId"]
|
|
product_data = {
|
|
"productId": params["product"]["product"]["id"],
|
|
"quantity": params["product"]["quantity"],
|
|
"services": [
|
|
{"serviceId": service["service"]["id"], "price": service["price"], "isFixedPrice": service.get("isFixedPrice", False), "quantity": 1}
|
|
for service in params["product"]["services"]
|
|
]
|
|
}
|
|
|
|
deal = await mongo.deals_collection.find_one({"id": deal_id})
|
|
if not deal:
|
|
return response({"message": "Сделка не найдена", "ok": False}, start_time=start_time)
|
|
|
|
products = deal.get("products", [])
|
|
products.append(product_data)
|
|
|
|
await mongo.deals_collection.update_one({"id": deal_id}, {"$set": {"products": products}})
|
|
return response({
|
|
"message": "Товар добавлен к сделке",
|
|
"ok": True
|
|
}, start_time=start_time)
|
|
|
|
|
|
@router.post("/delete")
|
|
async def delete_product(params: dict):
|
|
start_time = time()
|
|
|
|
deal_id = params["dealId"]
|
|
product_id = params["productId"]
|
|
|
|
deal = await mongo.deals_collection.find_one({"id": deal_id})
|
|
if not deal:
|
|
return response({"message": "Сделка не найдена", "ok": False}, start_time=start_time)
|
|
|
|
products = deal.get("products", [])
|
|
products = [product for product in products if product["productId"] != product_id]
|
|
|
|
await mongo.deals_collection.update_one({"id": deal_id}, {"$set": {"products": products}})
|
|
return response({
|
|
"message": "Товар удалён из сделки",
|
|
"ok": True
|
|
}, start_time=start_time)
|