From f2059b9b2d5e2e85b3370419a55053d0266e69b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?essp=C3=ADndola?= Date: Thu, 14 Aug 2025 14:51:51 -0300 Subject: [PATCH] refactor: update product update logic to handle multiple fields and user timezone --- .gitignore | 6 ---- backend/zato-csm-backend/.env.example | 9 ----- backend/zato-csm-backend/requirements.txt | 27 ++++++++++++++ backend/zato-csm-backend/routes/products.py | 35 +++++++++++++++++-- .../services/product_service.py | 10 ++++-- 5 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 backend/zato-csm-backend/requirements.txt diff --git a/.gitignore b/.gitignore index 192d7813..61c2fda4 100644 --- a/.gitignore +++ b/.gitignore @@ -111,9 +111,6 @@ installed.txt filter-repo/ .git/ -# Requirements obsoletos -requirements.txt -dependencies_*.txt # Logs logs *.log @@ -227,6 +224,3 @@ installed.txt filter-repo/ .git/ -# Requirements obsoletos -requirements.txt -dependencies_*.txt diff --git a/backend/zato-csm-backend/.env.example b/backend/zato-csm-backend/.env.example index 116575d9..596576c6 100644 --- a/backend/zato-csm-backend/.env.example +++ b/backend/zato-csm-backend/.env.example @@ -1,6 +1,3 @@ -# Database Configuration -DATABASE_TYPE=postgres - # PostgreSQL Configuration POSTGRES_HOST=localhost POSTGRES_USER=your_postgres_user @@ -8,12 +5,6 @@ POSTGRES_PASSWORD=your_postgres_password POSTGRES_DATABASE=your_database_name POSTGRES_PORT=5432 -# MySQL Configuration -MYSQL_HOST=localhost -MYSQL_USER=your_mysql_user -MYSQL_PASSWORD=your_mysql_password -MYSQL_DATABASE=your_database_name - # JWT Configuration SECRET_KEY=your_very_long_and_secure_secret_key_here ALGORITHM=HS256 diff --git a/backend/zato-csm-backend/requirements.txt b/backend/zato-csm-backend/requirements.txt new file mode 100644 index 00000000..3c929523 --- /dev/null +++ b/backend/zato-csm-backend/requirements.txt @@ -0,0 +1,27 @@ +annotated-types==0.7.0 +anyio==4.9.0 +argon2-cffi==25.1.0 +argon2-cffi-bindings==25.1.0 +cffi==1.17.1 +click==8.2.1 +colorama==0.4.6 +cryptography==45.0.5 +fastapi==0.116.1 +h11==0.16.0 +idna==3.10 +passlib==1.7.4 +psycopg2==2.9.10 +psycopg2-binary==2.9.10 +pycparser==2.22 +pydantic==2.11.7 +pydantic_core==2.33.2 +PyJWT==2.10.1 +PyMySQL==1.1.1 +python-dotenv==1.1.1 +python-multipart==0.0.20 +pytz==2025.2 +sniffio==1.3.1 +starlette==0.47.2 +typing-inspection==0.4.1 +typing_extensions==4.14.1 +uvicorn==0.35.0 \ No newline at end of file diff --git a/backend/zato-csm-backend/routes/products.py b/backend/zato-csm-backend/routes/products.py index 5492a19e..dbd0a41a 100644 --- a/backend/zato-csm-backend/routes/products.py +++ b/backend/zato-csm-backend/routes/products.py @@ -65,12 +65,43 @@ def get_product( @router.put("/{product_id}") def update_product( + request: Request, product_id: int, - updates: dict = Body(...), + name: Optional[str] = Form(None), + description: Optional[str] = Form(None), + price: Optional[float] = Form(None), + stock: Optional[int] = Form(None), + category: Optional[str] = Form(None), + images: List[UploadFile] = File(None), + body: Optional[dict] = Body(None), current_user=Depends(get_current_user), product_service=Depends(_get_product_service), ): - product_updated = product_service.update_product(product_id, updates) + user_timezone = get_user_timezone_from_request(request) + + updates = {} + + if body: + updates.update(body) + + if name is not None: + updates["name"] = name + if description is not None: + updates["description"] = description + if price is not None: + updates["price"] = price + if stock is not None: + updates["stock"] = stock + if category is not None: + updates["category"] = category + + if images: + updates["images"] = images + + if not updates: + raise HTTPException(status_code=400, detail="No update fields provided") + + product_updated = product_service.update_product(product_id, updates, user_timezone) return { "success": True, "message": "Product updated successfully", diff --git a/backend/zato-csm-backend/services/product_service.py b/backend/zato-csm-backend/services/product_service.py index 378c4754..d596107a 100644 --- a/backend/zato-csm-backend/services/product_service.py +++ b/backend/zato-csm-backend/services/product_service.py @@ -69,11 +69,11 @@ def get_product(self, product_id): raise HTTPException(status_code=404, detail="Product not found") return product - def update_product(self, product_id: int, updates: dict): + def update_product(self, product_id: int, updates: dict, user_timezone: str = "UTC"): # Validation allowed fields allowed_fields = ["name", "description", "price", "stock", "category", "images"] - for field in updates.keys(): + for field in list(updates.keys()): if field not in allowed_fields: raise HTTPException(status_code=400, detail=f"Invalid field: {field}") @@ -82,7 +82,11 @@ def update_product(self, product_id: int, updates: dict): if "stock" in updates and updates["stock"] < 0: raise HTTPException(status_code=400, detail="Stock cannot be negative") - return self.product_repo.update_product(product_id, updates) + images = updates.get("images") + if images and isinstance(images, list): + updates["images"] = self._process_images(images) + + return self.product_repo.update_product(product_id, updates, user_timezone) def delete_product(self, product_id): return self.product_repo.delete_product(product_id)