From 7037adafd3e1dbcc516d400a4b82cbf9678e31a5 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 21 Nov 2025 12:15:40 +0530 Subject: [PATCH] Bug fixed: Create establishment without mapping products --- app/controllers/establishment.controller.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index daf5708..51f9321 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -86,6 +86,24 @@ exports.createEstablishment = async (req, res) => { }); } + if (!Array.isArray(establishment_products) || establishment_products.length === 0) { + return res.status(400).send({ + status: "failed", + message: "Please select at least one product." + }); + } + + for (const item of establishment_products) { + const id = Number(item.product_id); + + if (!id || isNaN(id) || id <= 1) { + return res.status(400).send({ + status: "failed", + message: "Invalid product_id. product_id must be a number greater than 1 and cannot be 0." + }); + } + } + // Check if establishment already exists const existingEstablishment = await Establishment.findOne({where: { establishment_code }, }); if (existingEstablishment) { @@ -167,7 +185,6 @@ exports.createEstablishment = async (req, res) => { // remove duplicates from request itself let uniqueProducts = [...new Set(establishment_products.map(x => x.product_id))]; - // now check which already exist for this establishment const existing = await EstablishmentProduct.findAll({ where: {