192 lines
5.9 KiB
JavaScript
192 lines
5.9 KiB
JavaScript
const db = require("../models");
|
|
const { Op } = require("sequelize");
|
|
const { Sequelize } = require("sequelize");
|
|
const QuarterlyWindowsConfiguration = db.QuarterlyWindowsConfiguration;
|
|
const Establishment = db.Establishment;
|
|
const EstablishmentUser = db.EstablishmentUser;
|
|
const { sendEmailService } = require("../services/email.service");
|
|
const sanitize = require("sanitize-html");
|
|
const logger = require("../services/logger");
|
|
|
|
// Create new configuration
|
|
|
|
exports.createConfig = async (req, res) => {
|
|
try {
|
|
|
|
const {survey_name, quarter, year, start_date, end_date } = req.body;
|
|
|
|
// find config
|
|
const exists = await QuarterlyWindowsConfiguration.findOne({where: { quarter, year }});
|
|
|
|
if (exists) {
|
|
return res.status(400).json({status: "failed", message: `Window already exists quarter: ${quarter}, year: ${year}`});
|
|
}
|
|
|
|
// get active establishment count
|
|
const assignedCount = await Establishment.count({
|
|
where: { is_active: true }
|
|
});
|
|
|
|
function sanitizeValue(value) {
|
|
if (typeof value === "string") {
|
|
return sanitize(value, {
|
|
allowedTags: [],
|
|
allowedAttributes: {},
|
|
});
|
|
}
|
|
if (Array.isArray(value)) {
|
|
return value.map(sanitizeValue);
|
|
}}
|
|
|
|
const payload = {
|
|
survey_name: sanitizeValue(req.body.survey_name),
|
|
quarter: sanitizeValue(req.body.quarter),
|
|
year: req.body.year,
|
|
start_date: req.body.start_date,
|
|
end_date: req.body.end_date,
|
|
grace_periods_days: req.body.grace_periods_days,
|
|
assigned: assignedCount,
|
|
responded: 0,
|
|
not_responded: assignedCount,
|
|
created_by: req.user.id,
|
|
};
|
|
|
|
const placeholder = {
|
|
survey_name: survey_name,
|
|
quarter: quarter,
|
|
year: year,
|
|
opens_on: start_date,
|
|
closes_on: end_date,
|
|
portal_url: process.env.FE_BASE_URL,
|
|
support_email: process.env.SUPPORT_EMAIL,
|
|
support_phone: process.env.SUPPORT_PHONE
|
|
};
|
|
|
|
const data = await QuarterlyWindowsConfiguration.create(payload);
|
|
res.status(201).send({
|
|
status: "success",
|
|
message: "Quarterly window configuration created successfully"
|
|
});
|
|
sendEmailsToEstablishments(placeholder);
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message: "Internal server error" });
|
|
}
|
|
};
|
|
|
|
async function sendEmailsToEstablishments(placeholder) {
|
|
try {
|
|
const users = await EstablishmentUser.findAll({
|
|
where: { is_active: 1 }
|
|
});
|
|
|
|
for (const user of users) {
|
|
try {
|
|
await sendEmailService( user.email, "quarterly_survey_created_to_establishment", placeholder );
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
} catch (emailErr) {
|
|
logger.error(emailErr.message);
|
|
logger.error(`Stack trace: ${emailErr.stack}`);
|
|
console.error("Email failed:", emailErr.message);
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error("Email background job failed:", err.message);
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message: "Internal server error" });
|
|
}
|
|
}
|
|
|
|
// Get all configurations
|
|
exports.getAllConfigs = async (req, res) => {
|
|
try {
|
|
const data = await QuarterlyWindowsConfiguration.findAll({
|
|
attributes: {
|
|
include: [
|
|
[
|
|
Sequelize.literal(`(
|
|
SELECT COUNT(*) FROM submission AS s
|
|
WHERE s.quarter = quarterly_windows_configuration_master.quarter AND s.year = quarterly_windows_configuration_master.year
|
|
)`),
|
|
"submission_count",
|
|
],
|
|
],
|
|
},
|
|
order: [[Sequelize.literal("COALESCE(updated_at, created_at)"), "DESC"]],
|
|
});
|
|
res
|
|
.status(200)
|
|
.send({ status: "success", message: "Fetched successfully", quarter_count: data.length, data });
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message: "Internal server error" });
|
|
}
|
|
};
|
|
|
|
// Get single configuration by ID
|
|
exports.getConfigById = async (req, res) => {
|
|
try {
|
|
const data = await QuarterlyWindowsConfiguration.findByPk(req.params.id);
|
|
if (!data)
|
|
return res
|
|
.status(404)
|
|
.send({ status: "failed", message: "Record not found" });
|
|
|
|
res
|
|
.status(200)
|
|
.send({ status: "success", message: "Fetched successfully", data });
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message: "Internal server error" });
|
|
}
|
|
};
|
|
|
|
// Update configuration
|
|
exports.updateConfig = async (req, res) => {
|
|
try {
|
|
req.body.updated_by = req.body.updated_by || req.user.id;
|
|
req.body.updated_at = req.body.updated_at || new Date();
|
|
const [updated] = await QuarterlyWindowsConfiguration.update(req.body, {
|
|
where: { id: req.params.id },
|
|
});
|
|
if (!updated)
|
|
return res
|
|
.status(404)
|
|
.send({ status: "failed", message: "Record not found" });
|
|
|
|
res
|
|
.status(200)
|
|
.send({ status: "success", message: "Updated successfully" });
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message: "Internal server error" });
|
|
}
|
|
};
|
|
|
|
// Delete configuration
|
|
exports.deleteConfig = async (req, res) => {
|
|
try {
|
|
const deleted = await QuarterlyWindowsConfiguration.destroy({
|
|
where: { id: req.params.id },
|
|
});
|
|
if (!deleted)
|
|
return res
|
|
.status(404)
|
|
.send({ status: "failed", message: "Record not found" });
|
|
|
|
res
|
|
.status(200)
|
|
.send({ status: "success", message: "Deleted successfully" });
|
|
} catch (err) {
|
|
logger.error(err.message);
|
|
logger.error(`Stack trace: ${err.stack}`);
|
|
res.status(500).send({ status: "failed", message: "Internal server error" });
|
|
}
|
|
};
|