diff --git a/app/controllers/dashboard.controller.js b/app/controllers/dashboard.controller.js index accca30..b41e8ad 100644 --- a/app/controllers/dashboard.controller.js +++ b/app/controllers/dashboard.controller.js @@ -181,16 +181,18 @@ exports.adminDashboard = async (req, res) => { // only when query param not used → not started count let notStartedCount = 0; - if (!req.query.quarter && !req.query.year) { - const startedEstIds = await Submission.findAll({ - attributes: [[Sequelize.fn("DISTINCT", Sequelize.col("establishment_id")), "id"]], - raw: true, - }); - const startedIds = startedEstIds.map(e => e.id); - notStartedCount = await Establishment.count({ - where: { id: { [Op.notIn]: startedIds } }, - }); - } + + const startedEstIds = await Submission.findAll({ + where: whereCond, + attributes: [[Sequelize.fn("DISTINCT", Sequelize.col("establishment_id")), "id"]], + raw: true, + }); + + const startedIds = startedEstIds.map(e => e.id); + notStartedCount = await Establishment.count({ + where: { id: { [Op.notIn]: startedIds } }, + }); + const recentSubmissions = await Submission.findAll({ where: whereCond, diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index 08f73ff..73eb714 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -32,6 +32,7 @@ exports.createEstablishment = async (req, res) => { factory_name, permanent_factory_code, industry_code, + industry_code_production, license_number, isic_code, description, @@ -91,6 +92,7 @@ exports.createEstablishment = async (req, res) => { factory_name, permanent_factory_code, industry_code, + industry_code_production, license_number, isic_code, description, @@ -167,6 +169,17 @@ exports.createEstablishment = async (req, res) => { } catch (err) { + + if (err.name === "SequelizeUniqueConstraintError") { + + // extract exact field + const field = err.errors[0].path; // <-- this gives the column name + return res.status(400).json({ + status: "failed", + message: `${field} already exists` + }); + } + return res.status(500).send({status: "failed",message: err.message || "Internal server error",}); } }; @@ -275,14 +288,18 @@ exports.getAllEstablishments = async (req, res) => { worksheet.columns = [ { header: "ID", key: "id", width: 10 }, { header: "Establishment Name", key: "factory_name", width: 30 }, - { header: "Code", key: "establishment_code", width: 20 }, + { header: "Contact Name", key: "establishment_contact_person_name", width: 30 }, { header: "Emirate", key: "emirate_name", width: 20 }, - { header: "City/Town", key: "city_name", width: 20 }, - { header: "Contact Email", key: "contact_email", width: 30 }, - { header: "Corporate Email", key: "corporate_email", width: 30 }, - { header: "Status", key: "status", width: 15 }, + { header: "ISIC Code", key: "isic_code", width: 20 }, + { header: "Establishment ID", key: "establishment_code", width: 20 }, + { header: "Total Employees", key: "total_employees", width: 20 }, { header: "Created By", key: "created_user", width: 20 }, { header: "Created On", key: "created_at", width: 20 }, + { header: "Last Updated", key: "updated_at", width: 20 }, + { header: "Status", key: "status", width: 15 }, + // { header: "City/Town", key: "city_name", width: 20 }, + // { header: "Contact Email", key: "contact_email", width: 30 }, + // { header: "Corporate Email", key: "corporate_email", width: 30 }, ]; // 🧠 Format data @@ -290,14 +307,18 @@ exports.getAllEstablishments = async (req, res) => { worksheet.addRow({ id: item.id, factory_name: item.factory_name, - establishment_code: item.establishment_code, + establishment_contact_person_name: item.establishment_contact_person_name, emirate_name: item.establishment_emirate?.name || "-", - city_name: item.establishment_city?.name || "-", - contact_email: item.establishment_contact_email || "-", - corporate_email: item.corporate_email || "-", - status: item.is_active ? "Active" : "Inactive", + isic_code: item.isic_code || "-", + establishment_code: item.establishment_code, + total_employees: item.total_employees, created_user: item.created_user?.name || "-", created_at: new Date(item.created_at).toLocaleDateString(), + updated_at: new Date(item.updated_at).toLocaleDateString(), + status: item.is_active ? "Active" : "Inactive", + // city_name: item.establishment_city?.name || "-", + // contact_email: item.establishment_contact_email || "-", + // corporate_email: item.corporate_email || "-", }); }); diff --git a/app/models/establishment.model.js b/app/models/establishment.model.js index 602da9d..907a1d7 100644 --- a/app/models/establishment.model.js +++ b/app/models/establishment.model.js @@ -7,26 +7,14 @@ module.exports = (sequelize, DataTypes) => { autoIncrement: true, primaryKey: true, }, - establishment_code: { - type: DataTypes.STRING, - allowNull: false, - }, - factory_name: { - type: DataTypes.STRING, - allowNull: false, - }, - permanent_factory_code: { - type: DataTypes.STRING, - }, - industry_code: { - type: DataTypes.STRING, - }, - license_number: { - type: DataTypes.STRING, - }, - isic_code: { - type: DataTypes.STRING, - }, + establishment_code: { type: DataTypes.STRING, unique: true }, + factory_name: { type: DataTypes.STRING, unique: true }, + permanent_factory_code: { type: DataTypes.STRING, unique: true }, + industry_code: { type: DataTypes.STRING, unique: true }, + industry_code_production: { type: DataTypes.STRING, unique: true }, + license_number: { type: DataTypes.STRING, unique: true }, + isic_code: { type: DataTypes.STRING, unique: true }, + // Establishment Contact Details establishment_address: { type: DataTypes.STRING }, establishment_city_town_id: { type: DataTypes.INTEGER }, diff --git a/app/routes/routes.js b/app/routes/routes.js index c9895dd..aadf8b3 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -371,6 +371,7 @@ router.get("/testEmail", establishmentController.testEmail); * factory_name: { type: string } * permanent_factory_code: { type: string } * industry_code: { type: string } + * industry_code_production: { type: string } * license_number: { type: string } * isic_code: { type: string } * description: { type: string } @@ -530,6 +531,7 @@ router.get("/establishments/:id",[verifySignature, verifyToken], establishmentCo * factory_name: { type: string } * permanent_factory_code: { type: string } * industry_code: { type: string } + * industry_code_production: { type: string } * license_number: { type: string } * isic_code: { type: string } * description: { type: string }