158 lines
4.7 KiB
JavaScript
158 lines
4.7 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");
|
|
|
|
// 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 }
|
|
});
|
|
|
|
// set auto values before inserting
|
|
req.body.assigned = assignedCount;
|
|
req.body.responded = 0;
|
|
req.body.not_responded = assignedCount;
|
|
req.body.created_by = req.body.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(req.body);
|
|
res.status(201).send({
|
|
status: "success",
|
|
message: "Quarterly window configuration created successfully",
|
|
data,
|
|
});
|
|
sendEmailsToEstablishments(placeholder);
|
|
} catch (err) {
|
|
res.status(500).send({ status: "failed", message: err.message });
|
|
}
|
|
};
|
|
|
|
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) {
|
|
console.error("Email failed:", user.email, emailErr.message);
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error("Email background job failed:", err.message);
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
res.status(500).send({ status: "failed", message: err.message });
|
|
}
|
|
};
|
|
|
|
// 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) {
|
|
res.status(500).send({ status: "failed", message: err.message });
|
|
}
|
|
};
|
|
|
|
// 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) {
|
|
res.status(500).send({ status: "failed", message: err.message });
|
|
}
|
|
};
|
|
|
|
// 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) {
|
|
res.status(500).send({ status: "failed", message: err.message });
|
|
}
|
|
};
|