{"id":21964602,"url":"https://github.com/apollo-level2-web-dev/batch-3-assignment-2","last_synced_at":"2025-10-08T07:21:33.516Z","repository":{"id":240672659,"uuid":"800553979","full_name":"Apollo-Level2-Web-Dev/batch-3-assignment-2","owner":"Apollo-Level2-Web-Dev","description":null,"archived":false,"fork":false,"pushed_at":"2024-05-21T03:47:16.000Z","size":58,"stargazers_count":6,"open_issues_count":0,"forks_count":7,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-28T01:16:58.959Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Apollo-Level2-Web-Dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-14T14:56:05.000Z","updated_at":"2024-06-11T09:12:48.000Z","dependencies_parsed_at":"2024-05-20T13:53:53.783Z","dependency_job_id":"c7469675-22b8-49e1-8cdb-adfa564a3310","html_url":"https://github.com/Apollo-Level2-Web-Dev/batch-3-assignment-2","commit_stats":null,"previous_names":["apollo-level2-web-dev/batch-3-assignment-2"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2Fbatch-3-assignment-2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2Fbatch-3-assignment-2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2Fbatch-3-assignment-2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2Fbatch-3-assignment-2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Apollo-Level2-Web-Dev","download_url":"https://codeload.github.com/Apollo-Level2-Web-Dev/batch-3-assignment-2/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245021690,"owners_count":20548388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-29T12:25:45.214Z","updated_at":"2025-10-08T07:21:33.433Z","avatar_url":"https://github.com/Apollo-Level2-Web-Dev.png","language":null,"readme":"# batch-3-assignment-2\n\n**Objective:** Develop a Express application with TypeScript as the programming language, integrating MongoDB with Mongoose for effective data management. Ensure data integrity through validation using Joi/Zod.\n\n### Set up the Project\n\n- Create a new Express project.\n- Set up a MongoDB database using Mongoose for storing product and order data.\n\n### Define Data Models\n\n- Create Mongoose models for **Product** data based on the provided data structure. (You can follow [sample-data.json](./sample-data.json) file for ideas).\n- Define appropriate data types, validations.\n\n### E-commerce Product Data Types\n\n- **name** (string):\n    - The name of the product.\n- **description** (string):\n    - A brief description of the product.\n- **price** (number):\n    - The price of the product.\n- **category** (string):\n    - The category to which the product belongs.\n- **tags** (array of strings):\n    - An array of tags or keywords associated with the product.\n- **variants** (array of objects):\n    - An array containing different variants or options of the product, such as size, color, or style.\n        - **type** (string): The type of variant (e.g., size, color).\n        - **value** (string): The specific value of the variant (e.g., \"Small\", \"Red\").\n- **inventory** (object):\n    - An object representing the product's inventory details.\n        - **quantity** (number): The available quantity of the product in stock.\n        - **inStock** (boolean): Indicates whether the product is currently in stock.\n\n\n\n## Main Section (50 Marks):\n\n\n## Product Management\n\n### **1. Create a New Product**\n\n- **Endpoint**: **`/api/products`**\n- **Method:** `POST`\n- **Sample Request Body**:\n    \n    ```json\n    {\n        \"name\": \"iPhone 13\",\n        \"description\": \"A sleek and powerful smartphone with cutting-edge features.\",\n        \"price\": 999,\n        \"category\": \"Electronics\",\n        \"tags\": [\"smartphone\", \"Apple\", \"iOS\"],\n        \"variants\": [\n            {\n                \"type\": \"Color\",\n                \"value\": \"Midnight Blue\"\n            },\n            {\n                \"type\": \"Storage Capacity\",\n                \"value\": \"256GB\"\n            }\n        ],\n        \"inventory\": {\n            \"quantity\": 50,\n            \"inStock\": true\n        }\n    }\n    ```\n    \n- **Sample Response**:\n    \n    ```json\n    {\n        \"success\": true,\n        \"message\": \"Product created successfully!\",\n        \"data\": {\n            \"name\": \"iPhone 13\",\n            \"description\": \"A sleek and powerful smartphone with cutting-edge features.\",\n            \"price\": 999,\n            \"category\": \"Electronics\",\n            \"tags\": [\"smartphone\", \"Apple\", \"iOS\"],\n            \"variants\": [\n                {\n                    \"type\": \"Color\",\n                    \"value\": \"Midnight Blue\"\n                },\n                {\n                    \"type\": \"Storage Capacity\",\n                    \"value\": \"256GB\"\n                }\n            ],\n            \"inventory\": {\n                \"quantity\": 50,\n                \"inStock\": true\n            }\n        }\n    }\n    ```\n    \n\n### **2. Retrieve a List of All Products**\n\n- **Endpoint**: **`/api/products`**\n- **Method:** `GET`\n- **Sample Response**:\n    \n    ```json\n    {\n        \"success\": true,\n        \"message\": \"Products fetched successfully!\",\n        \"data\": [\n            {\n                \"name\": \"iPhone 13\",\n                \"description\": \"A sleek and powerful smartphone with cutting-edge features.\",\n                \"price\": 999,\n                \"category\": \"Electronics\",\n                \"tags\": [\"smartphone\", \"Apple\", \"iOS\"],\n                \"variants\": [\n                    {\n                        \"type\": \"Color\",\n                        \"value\": \"Midnight Blue\"\n                    },\n                    {\n                        \"type\": \"Storage Capacity\",\n                        \"value\": \"256GB\"\n                    }\n                ],\n                \"inventory\": {\n                    \"quantity\": 50,\n                    \"inStock\": true\n                }\n            },\n            {\n                \"name\": \"Samsung Galaxy S21\",\n                \"description\": \"High-performance Android smartphone with advanced camera capabilities.\",\n                \"price\": 799,\n                \"category\": \"Electronics\",\n                \"tags\": [\"smartphone\", \"Samsung\", \"Android\"],\n                \"variants\": [\n                    {\n                        \"type\": \"Color\",\n                        \"value\": \"Phantom Black\"\n                    },\n                    {\n                        \"type\": \"Storage Capacity\",\n                        \"value\": \"128GB\"\n                    }\n                ],\n                \"inventory\": {\n                    \"quantity\": 30,\n                    \"inStock\": true\n                }\n            }\n            // Additional products can be added here...\n        ]\n    }\n    ```\n    \n\n### **3. Retrieve a Specific Product by ID**\n\n- **Endpoint**: **`/api/products/:productId`**\n- **Method: `GET`**\n- **Sample Response**:\n    \n    ```json\n    {\n        \"success\": true,\n        \"message\": \"Product fetched successfully!\",\n        \"data\": {\n            \"name\": \"iPhone 13\",\n            \"description\": \"A sleek and powerful smartphone with cutting-edge features.\",\n            \"price\": 999,\n            \"category\": \"Electronics\",\n            \"tags\": [\"smartphone\", \"Apple\", \"iOS\"],\n            \"variants\": [\n                {\n                    \"type\": \"Color\",\n                    \"value\": \"Midnight Blue\"\n                },\n                {\n                    \"type\": \"Storage Capacity\",\n                    \"value\": \"256GB\"\n                }\n            ],\n            \"inventory\": {\n                \"quantity\": 50,\n                \"inStock\": true\n            }\n        }\n    }\n    ```\n    \n\n### **4. Update Product Information**\n\n- **Endpoint**: **`/api/products/:productId`**\n- **Method: `PUT`**\n- **Sample Request Body**:\n    \n    ```json\n    {\n        \"name\": \"iPhone 13\",\n        \"description\": \"A sleek and powerful smartphone with cutting-edge features.\",\n        \"price\": 999,\n        \"category\": \"Electronics\",\n        \"tags\": [\"smartphone\", \"Apple\", \"iOS\"],\n        \"variants\": [\n            {\n                \"type\": \"Color\",\n                \"value\": \"Midnight Blue\"\n            },\n            {\n                \"type\": \"Storage Capacity\",\n                \"value\": \"256GB\"\n            }\n        ],\n        \"inventory\": {\n            \"quantity\": 50,\n            \"inStock\": true\n        }\n    }\n    ```\n    \n- **Sample Response**:\n    \n    ```json\n    {\n        \"success\": true,\n        \"message\": \"Product updated successfully!\",\n        \"data\": {\n            \"name\": \"iPhone 13\",\n            \"description\": \"A sleek and powerful smartphone with cutting-edge features.\",\n            \"price\": 999,\n            \"category\": \"Electronics\",\n            \"tags\": [\"smartphone\", \"Apple\", \"iOS\"],\n            \"variants\": [\n                {\n                    \"type\": \"Color\",\n                    \"value\": \"Midnight Blue\"\n                },\n                {\n                    \"type\": \"Storage Capacity\",\n                    \"value\": \"256GB\"\n                }\n            ],\n            \"inventory\": {\n                \"quantity\": 49,\n                \"inStock\": true\n            }\n        }\n    }\n    ```\n    \n\n### **5. Delete a Product**\n\n- **Endpoint**: **`/api/products/:productId`**\n- **Method: `DELETE`**\n- **Sample Response**:\n    \n    ```json\n    {\n        \"success\": true,\n        \"message\": \"Product deleted successfully!\",\n        \"data\": null\n     }\n     \n    // The product should be deleted from the database.\n    ```\n    \n\n### **6. Search a product**\n\n- **Endpoint**: `/api/products?searchTerm=iphone`\n- **Method: GET**\n- **Sample Response**:\n\n```jsx\n{\n    \"success\": true,\n    \"message\": \"Products matching search term 'iphone' fetched successfully!\",\n    \"data\": [\n        {\n            \"name\": \"iPhone 13 Pro\",\n            \"description\": \"The latest flagship iPhone model with advanced camera features.\",\n            \"price\": 999,\n            \"category\": \"Smartphones\",\n            \"tags\": [\"iPhone\", \"Apple\", \"Mobile\"],\n            \"variants\": [\n                {\n                    \"type\": \"Color\",\n                    \"value\": \"Graphite\"\n                },\n                {\n                    \"type\": \"Storage\",\n                    \"value\": \"256GB\"\n                }\n            ],\n            \"inventory\": {\n                \"quantity\": 50,\n                \"inStock\": true\n            }\n        },\n        {\n            \"name\": \"iPhone SE\",\n            \"description\": \"Compact and affordable iPhone model with powerful performance.\",\n            \"price\": 399,\n            \"category\": \"Smartphones\",\n            \"tags\": [\"iPhone\", \"Apple\", \"Mobile\"],\n            \"variants\": [\n                {\n                    \"type\": \"Color\",\n                    \"value\": \"White\"\n                },\n                {\n                    \"type\": \"Storage\",\n                    \"value\": \"128GB\"\n                }\n            ],\n            \"inventory\": {\n                \"quantity\": 20,\n                \"inStock\": true\n            }\n        }\n    ]\n}\n```\n\n## Order Management\n\n### **Order Management API Endpoints**\n\n### **1.Create a New Order**\n\n- **Endpoint**: **`/api/orders`**\n- **Method: `POST`**\n- **Request Body**:\n    \n    ```json\n    {\n        \"email\": \"level2@programming-hero.com\",\n        \"productId\": \"5fd67e890b60c903cd8544a3\",\n        \"price\": 999,\n        \"quantity\": 1\n    }\n    ```\n    \n- **Response**:\n    \n    ```json\n    {\n        \"success\": true,\n        \"message\": \"Order created successfully!\",\n        \"data\": {\n            \"email\": \"level2@programming-hero.com\",\n            \"productId\": \"5fd67e890b60c903cd8544a3\",\n            \"price\": 999,\n            \"quantity\": 1\n        }\n    }\n    \n    ```\n    \n\n### **2.Retrieve All Orders**\n\n- **Endpoint**: **`/api/orders`**\n- **Method: `GET`**\n- **Sample Response**:\n    \n    ```json\n    {\n        \"success\": true,\n        \"message\": \"Orders fetched successfully!\",\n        \"data\": [\n            {\n                \"email\": \"level2@programming-hero.com\",\n                \"productId\": \"5fd67e890b60c903cd8544a3\",\n                \"price\": 999,\n                \"quantity\": 1\n            }\n            // more orders...\n        ]\n    }\n    ```\n    \n\n### **3. Retrieve Orders by User Email**\n\n- **Endpoint**: `/api/orders?email=level2@programming-hero.com`\n- **Method:** `GET`\n- **Sample Response**:\n    \n    ```json\n    {\n        \"success\": true,\n        \"message\": \"Orders fetched successfully for user email!\",\n        \"data\": [\n            {\n                \"email\": \"level2@programming-hero.com\",\n                \"productId\": \"5fd67e890b60c903cd8544a3\",\n                \"price\": 999,\n                \"quantity\": 1\n            }\n            // more orders for the user...\n        ]\n    }\n    ```\n    \n\n## **Bonus Section (10 Marks):**\n\n**Inventory Update**: \n\nWhen creating new order (**`/api/orders`** ) ,reduce the `quantity` of the ordered product in inventory and update the **`inStock`** property.\n\n### **Inventory Management Logic**\n\n- When a new order is created, the system should check the available quantity in inventory.\n- If the ordered quantity exceeds the available quantity, return an error response indicating insufficient stock.\n- Update the inventory quantity and **`inStock`** status based on the ordered quantity:\n    - If the inventory quantity reaches zero, set **`inStock`** to **`false`**.\n    - Otherwise, keep **`inStock`** as **`true`**.\n\n## **Error Handling:**\n\n### **Sample Error Responses**\n\n- Insufficient Quantity Error\n\n```jsx\n{\n    \"success\": false,\n    \"message\": \"Insufficient quantity available in inventory\"\n}\n```\n\n- Not Found Error\n\n```jsx\n{\n \"success\": false,\n \"message\": \"Order not found\"\n}\n```\n\n- Not Found Route\n\n```jsx\n{\n \"success\": false,\n \"message\": \"Route not found\"\n}\n```\n\n### **Validation with Joi/Zod**\n\n- Use **Joi/Zod** to validate incoming data for product and order creation and updating operations.\n- Ensure that the data adheres to the structure defined in the models.\n- Handle validation errors gracefully and provide meaningful error messages in the API responses.\n\n## Instructions\n\n1. **Coding Quality:**\n    - Write clean, modular, and well-organized code.\n    - Follow consistent naming conventions for variables, functions, and routes.\n    - Use meaningful names that reflect the purpose of variables and functions.\n    - Ensure that the code is readable.\n2. **Comments:**\n    - Try to provide inline comments to explain complex sections of code or logic.\n3. **API Endpoint Adherence:**\n    - Strictly follow the provided API endpoint structure and naming conventions.\n    - Ensure that the request and response formats match the specifications outlined in the assignment.\n4. **Validation and Error Handling:**\n    - Implement validation using Joi/Zod for both user and order data.\n    - Handle validation errors gracefully and provide meaningful error messages in the API responses.\n    - Implement error handling for scenarios like user not found, validation errors.\n5. **Coding Tools and Libraries:**\n    - Avoid the use of AI tools or libraries for generating code. Write the code manually to demonstrate a clear understanding of the concepts.\n    - Utilize only the specified libraries like Express, Mongoose, Joi and avoid unnecessary dependencies.\n6. **Coding Style:**\n    - Consider using linting tools (e.g., **ESLint**) to enforce coding style and identify potential issues.\n    - Ensure there are at least 10 commits in your GitHub repository.\n\n***Not following the specified API endpoint structure, naming conventions, and other instructions will result in a deduction of marks.***\n\n### **Submission:**\n\n- Share the GitHub Repository link.\n- Share the Live Deployment link.\n- Include a README file with clear instructions on how to run the application locally.\n\n### **Submission Format for Our Submission Box: (Please use the following format for your submissions:)**\n\n- GitHub Repository URL (Server): https://github.com/Apollo-Level2-Web-Dev   ( There is no private repository for this assignment,you can use your repository)\n- Live Server Link: https://web.programming-hero.com/\n\n\n### **Deadline:**\n\n- 60 marks: May 22, 2024, 11:59 PM\n- 50 marks: May 23, 2023, 11:59 PM\n- 30 marks: After May 23, 11.59PM\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-level2-web-dev%2Fbatch-3-assignment-2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapollo-level2-web-dev%2Fbatch-3-assignment-2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-level2-web-dev%2Fbatch-3-assignment-2/lists"}