diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index 4ffcb8d..6d8355c 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -119,7 +119,12 @@ exports.createEstablishment = async (req, res) => { message: "Missing required field: city/town", }); } - + if (!industry_code_production) { + return res.status(400).send({ + status: "failed", + message: "Missing required field: Industry Code (Current Production)", + }); + } if (!Array.isArray(establishment_products) || establishment_products.length === 0) { return res.status(400).send({ status: "failed", @@ -1358,7 +1363,7 @@ exports.establishmentBulkUpload = async (req, res) => { // ------------------------------------------------------- // REQUIRED HEADERS // ------------------------------------------------------- - const required = ["establishment_id", "factory_name", "user_name", "email", "emirate", "industry_code_current_production"]; + const required = ["establishment_id", "factory_name", "user_name", "email", "emirate", "industry_code_current_production", "city_town"]; const firstKeys = Object.keys(rows[0]); const missingHeaders = required.filter(h => !firstKeys.includes(h)); @@ -1450,6 +1455,7 @@ exports.establishmentBulkUpload = async (req, res) => { const email = r.email; const emirate = r.emirate; const industry_code_current_production = r.industry_code_current_production; + const city_town = r.city_town; const missing = []; if (!est) missing.push("Establishment ID"); @@ -1458,6 +1464,7 @@ exports.establishmentBulkUpload = async (req, res) => { if (!email) missing.push("Email"); if (!emirate) missing.push("Emirate"); if (!industry_code_current_production) missing.push("Industry Code Current Production"); + if (!city_town) missing.push("City/Town"); if (missing.length) { errors.push({ row: rowNum, error: `Missing required fields: ${missing.join(", ")}` }); diff --git a/app/controllers/submission.controller.js b/app/controllers/submission.controller.js index 2c744be..be58f6f 100644 --- a/app/controllers/submission.controller.js +++ b/app/controllers/submission.controller.js @@ -25,44 +25,17 @@ exports.createSubmission = async (req, res) => { try { const { products = [], establishment_id, quarter, year, ...submissionData } = req.body; - if (products.length > 10) return res.status(400).json({ status: "failed", message: "Max 10 products allowed" }); // check duplicate submission const exists = await Submission.findOne({ where: {establishment_id,quarter,year} }); - + if (exists) { return res.status(400).json({status: "failed", message: `Submission already exists for establishment_id: ${establishment_id}, quarter: ${quarter}, year: ${year}`}); } submissionData.created_by = submissionData.created_by || req.user.id; - - const submission = await Submission.create({establishment_id, quarter, year, ...submissionData}); - - // find config - const config = await QuarterlyWindowsConfiguration.findOne({where: { quarter, year }}); - if (config) { - const currentResponded = config.responded ?? 0; - const currentNotResponded = config.not_responded ?? 0; - // respond increase, not responded decrease - await QuarterlyWindowsConfiguration.update({responded: currentResponded + 1, not_responded: currentNotResponded - 1, updated_at: new Date() }, {where: { id: config.id }} ); - } - - const hasNegativeNumber = (obj, fields) => { - return fields.find((field) => { - const val = obj[field]; - if (val === null || val === undefined || val === "") return false; - - const num = Number(val); - return !isNaN(num) && num < 0; - }); - }; - - if (products.length) { - // products.forEach((p) => (p.submission_id = submission.id)); - // await SubmissionProduct.bulkCreate(products); - - const numericFields = [ + const numericFields = [ "annual_installed_capacity", "previous_quantity_period_one", "previous_quantity_period_two", @@ -85,8 +58,18 @@ exports.createSubmission = async (req, res) => { "forecast_cost_period_two", "forecast_cost_period_three", ]; + + const hasNegativeNumber = (obj, fields) => { + return fields.find((field) => { + const val = obj[field]; + if (val === null || val === undefined || val === "") return false; - for (let i = 0; i < products.length; i++) { + const num = Number(val); + return !isNaN(num) && num < 0; + }); + }; + + for (let i = 0; i < products.length; i++) { const invalidField = hasNegativeNumber(products[i], numericFields); if (invalidField) { @@ -97,6 +80,21 @@ exports.createSubmission = async (req, res) => { } } + const submission = await Submission.create({establishment_id, quarter, year, ...submissionData}); + + // find config + const config = await QuarterlyWindowsConfiguration.findOne({where: { quarter, year }}); + if (config) { + const currentResponded = config.responded ?? 0; + const currentNotResponded = config.not_responded ?? 0; + // respond increase, not responded decrease + await QuarterlyWindowsConfiguration.update({responded: currentResponded + 1, not_responded: currentNotResponded - 1, updated_at: new Date() }, {where: { id: config.id }} ); + } + + if (products.length) { + // products.forEach((p) => (p.submission_id = submission.id)); + // await SubmissionProduct.bulkCreate(products); + // Process products before insert const processedProducts = products.map((p) => { const num = (v) => (isNaN(parseFloat(v)) ? 0 : parseFloat(v)); // safe number conversion diff --git a/app/downloads_csv/company_profile_upload_sample.csv b/app/downloads_csv/company_profile_upload_sample.csv index d57d098..a100cb1 100644 --- a/app/downloads_csv/company_profile_upload_sample.csv +++ b/app/downloads_csv/company_profile_upload_sample.csv @@ -1 +1 @@ -Establishment Id * (Mandatory),Factory Name * (Mandatory),User Name * (Mandatory),Email * (Mandatory),Emirate * (Mandatory),Industry Code Current Production * (Mandatory),HS Code 1 * (Mandatory),HS Code 2,HS Code 3,HS Code 4,HS Code 5,Permanent Factory Code,Industry Code Business Register,Description,Industry Code Mismatch Remarks,Establishment Address,City/Town,Postal Code,PO Box,Makani Number,Contact Person Name,Contact Person Designation,Mobile Number,Website,Number of Emirati Male,Number of Emirati Female,Number of Non-Emirati Male,Number of Non-Emirati Female +Establishment Id * (Mandatory),Factory Name * (Mandatory),User Name * (Mandatory),Email * (Mandatory),Emirate * (Mandatory),City/Town * (Mandatory),Industry Code Current Production * (Mandatory),HS Code 1 * (Mandatory),HS Code 2,HS Code 3,HS Code 4,HS Code 5,Permanent Factory Code,Industry Code Business Register,Description,Industry Code Mismatch Remarks,Establishment Address,Postal Code,PO Box,Makani Number,Contact Person Name,Contact Person Designation,Mobile Number,Website,Number of Emirati Male,Number of Emirati Female,Number of Non-Emirati Male,Number of Non-Emirati Female diff --git a/app/models/submission.model.js b/app/models/submission.model.js index 091235f..65b312e 100644 --- a/app/models/submission.model.js +++ b/app/models/submission.model.js @@ -41,7 +41,7 @@ module.exports = (sequelize, DataTypes) => { }, status: { type: DataTypes.STRING(50), - defaultValue: 'Pending', + defaultValue: 'Submitted', }, created_by: { type: DataTypes.INTEGER,