gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Description: Create a function api for delete product., accept product id as param,…
- User
Description: Create a function api for delete product., accept product id as param, return deleted count, recive a flag from user for bulk delete. scenario is user can delete product bulky or single., if it bulk delete flag is true system should check you have given array of id. in single case id string is needed, bulk id can recive from body, node: create as a functions not a router user Product schema to fetch products
Generate source code based on the description above and respond in the following JSON format ONLY: ```json { "name": "prompt_name", "schema": { "type": "string", "code": "Generated code here" } } ``` IMPORTANT: - Ensure the JSON response is error-free. - Include no additional text or explanations. Respond with the JSON object only. - Assistant
{ "name": "deleteProduct", "schema": { "type": "string", "code": "const Product = require('./models/Product');\n\nconst deleteProduct = async (id, isBulkDelete = false) => {\n try {\n let deletedCount;\n if (isBulkDelete) {\n if (!Array.isArray(id) || id.length === 0) {\n throw new Error('For bulk delete, an array of IDs must be provided.');\n }\n deletedCount = await Product.deleteMany({ _id: { $in: id } });\n } else {\n if (typeof id !== 'string') {\n throw new Error('For single delete, a single ID string must be provided.');\n }\n deletedCount = await Product.deleteOne({ _id: id });\n }\n return deletedCount.deletedCount;\n } catch (error) {\n throw new Error(`Error deleting product(s): ${error.message}`);\n }\n};\n\nmodule.exports = deleteProduct;" } }