diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index 17bdf6b..184e192 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -320,24 +320,38 @@ exports.getAllEstablishments = async (req, res) => { } // Fetch Data - const data = await Establishment.findAll({ - where: whereClause, - attributes: { - include: [ - [ - Sequelize.literal(`(SELECT COUNT(*) FROM establishment_products ep WHERE ep.establishment_id = establishments.id)`),"product_count" ] + const data = await Establishment.findAll({ + where: whereClause, + attributes: { + include: [ + [ + Sequelize.literal(`( + SELECT COUNT(*) + FROM establishment_products ep + WHERE ep.establishment_id = establishments.id + )`), + "product_count" + ], + [ + Sequelize.literal(`( + SELECT name + FROM admin_users au + WHERE au.id = establishments.created_by + )`), + "created_by_name" + ] ] - }, - include: [ - { model: CityTown, as: "establishment_city", attributes: ["name"] }, - { model: Emirate, as: "establishment_emirate", attributes: ["name"] }, - { model: CityTown, as: "corporate_city", attributes: ["name"] }, - { model: Emirate, as: "corporate_emirate", attributes: ["name"] }, - { model: user, as: "created_user", attributes: ["name"] }, - ], - order: [[sort_by, sort_order]], - ...(exportType ? {} : { limit, offset }), // pagination only when not exporting - }); + }, + include: [ + { model: CityTown, as: "establishment_city", attributes: ["name"] }, + { model: Emirate, as: "establishment_emirate", attributes: ["name"] }, + { model: CityTown, as: "corporate_city", attributes: ["name"] }, + { model: Emirate, as: "corporate_emirate", attributes: ["name"] }, + { model: user, as: "created_user", attributes: ["name"] }, + ], + order: [[sort_by, sort_order]], + ...(exportType ? {} : { limit, offset }), + }); // If export = excel → generate file if (exportType && exportType.toLowerCase() === "excel") {