diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index 156a1f3..e91832b 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -9,6 +9,7 @@ const Emirate = db.Emirate; const user = db.user; const User = db.user; const Product = db.Product; +const IsicMaster = db.IsicMaster; const ExcelJS = require("exceljs"); const { Op, Sequelize } = require("sequelize"); const { sendEmail } = require("../services/emailHelper"); @@ -59,7 +60,7 @@ exports.createEstablishment = async (req, res) => { "permanent_factory_code", "industry_code", "license_number", - "isic_code" + "isic_code", ].forEach(key => { if (req.body[key] === "") req.body[key] = null; }); @@ -107,7 +108,8 @@ exports.createEstablishment = async (req, res) => { total_employees, created_by, establishment_user, - establishment_products + establishment_products, + ERN } = req.body; // Basic Validation @@ -148,10 +150,10 @@ exports.createEstablishment = async (req, res) => { // }); // } if (!isic_code) { - return res.status(400).send({ - status: "failed", - message: "Missing required field: Industry Code (Current Production)", - }); + return res.status(400).send({ + status: "failed", + message: "Missing required field: Industry Code (Current Production)", + }); } if (!corporate_email) { return res.status(400).send({ @@ -240,6 +242,7 @@ exports.createEstablishment = async (req, res) => { non_emirati_female, total_emirati, total_employees, + ERN: sanitizeStringValue(ERN), created_by: req.body.created_by || req.user.id, created_at: new Date(), }); @@ -725,6 +728,8 @@ exports.updateEstablishment = async (req, res) => { estData.updated_at = estData.updated_at || new Date(); // update establishment await establishment.update(estData); + + if (req.body.is_active === true || req.body.is_active === false) { await EstablishmentUser.update( { @@ -2069,3 +2074,44 @@ exports.downloadCompanyProfileSample = async (req, res) => { } }; + +exports.searchIsicMaster = async (req, res) => { + + const { code, description } = req.query; + + try { + const whereClause = { is_active: true }; + + if (code || description) { + const orConditions = []; + + if (code) { + orConditions.push({ code: { [Op.like]: `%${code}%` } }); + } + + if (description) { + orConditions.push({ description: { [Op.like]: `%${description}%` } }); + } + + whereClause[Op.or] = orConditions; + } + + const rows = await IsicMaster.findAll({ where: whereClause }); + + return res.status(200).json({ + success: "success", + count: rows.length, + data: rows, + }); + + + } catch (error) { + return res.status(500).json({ + success: "failed", + message: "Internal server error", + error: error.message, + }); + } + +}; + diff --git a/app/models/IsicMaster.model.js b/app/models/IsicMaster.model.js new file mode 100644 index 0000000..56f9939 --- /dev/null +++ b/app/models/IsicMaster.model.js @@ -0,0 +1,31 @@ +// models/IsicMaster.js +module.exports = (sequelize, DataTypes) => { + const IsicMaster = sequelize.define( + "IsicMaster", + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + code: { + type: DataTypes.STRING(20), + allowNull: true, + }, + description: { + type: DataTypes.STRING(100), + allowNull: false, + }, + is_active: { + type: DataTypes.BOOLEAN, + defaultValue: true, + }, + }, + { + tableName: "isic_master", + } + ); + + return IsicMaster; + }; + \ No newline at end of file diff --git a/app/models/establishment.model.js b/app/models/establishment.model.js index eb8d5b2..577a47c 100644 --- a/app/models/establishment.model.js +++ b/app/models/establishment.model.js @@ -73,6 +73,7 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.INTEGER, allowNull: true, }, + ERN: { type: DataTypes.STRING }, }, { timestamps: false, diff --git a/app/models/index.js b/app/models/index.js index 052aa67..5c6d497 100644 --- a/app/models/index.js +++ b/app/models/index.js @@ -34,6 +34,7 @@ db.Isic2DigitIndices = require("./isic2DigitIndices.model.js")(sequelize, DataTy db.Isic3DigitIndices = require("./isic3DigitIndices.model.js")(sequelize, DataTypes); db.Isic4DigitIndices = require("./isic4DigitIndices.model.js")(sequelize, DataTypes); db.CalculationLog = require("./calculation_log.model.js")(sequelize, DataTypes); +db.IsicMaster = require("./IsicMaster.model.js")(sequelize, DataTypes); // Associations db.Establishment.hasMany(db.EstablishmentUser, { foreignKey: "establishment_id", diff --git a/app/routes/routes.js b/app/routes/routes.js index a133fca..d7d2396 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -591,6 +591,7 @@ router.get("/testEmail", establishmentController.testEmail); * non_emirati_female: { type: integer } * total_emirati: { type: integer } * total_employees: { type: integer } + * ERN: { type: string } * created_by: { type: integer } * establishment_user: * type: object @@ -767,6 +768,7 @@ router.get("/establishments/:id",[verifySignature, verifyToken], establishmentCo * non_emirati_female: { type: integer } * total_emirati: { type: integer } * total_employees: { type: integer } + * ERN: { type: string } * updated_by: { type: integer } * establishment_products: * type: array @@ -849,6 +851,31 @@ router.post("/establishments/uploadCSV",[verifySignature, verifyToken, upload.si +/** + * @swagger + * /api/searchIsicMaster: + * get: + * summary: Get all Isic Master data + * tags: [Establishments] + * security: + * - appSignature: [] + * - CSRF: [] + * cookieAuth: [] # or bearerAuth: [] if you use Authorization header + * parameters: + * - in: query + * name: code + * schema: { type: string } + * - in: query + * name: description + * schema: { type: string } + * responses: + * 200: + * description: Fetched successfully + * 500: + * description: Internal server error + */ +router.get("/searchIsicMaster",[verifySignature, verifyToken], establishmentController.searchIsicMaster); +