ordeby submission list , remainder email for all quarter window , 14 days restiction for resubmittion : GWM

This commit is contained in:
Gowtham M 2026-03-11 15:05:08 +05:30
parent f91bfdada2
commit b47acde85d
4 changed files with 112 additions and 23 deletions

View File

@ -71,7 +71,7 @@ exports.getEstablishmentDashboard = async (req, res) => {
const submission_history = await Submission.findAll({
where: { establishment_id },
order: [["created_at", "DESC"]],
order: [["updated_at", "DESC"]],
// limit: 5,
attributes: {
include: [

View File

@ -363,6 +363,17 @@ exports.updateSubmission = async (req, res) => {
if (!submission)
return res.status(404).json({ status: "failed", message: "Submission not found" });
//if submission.status is rejected then only allow update the submission between rejected_at and next 14 days of rejected_at date
if (submission.status === 'Rejected') {
const rejectedAt = new Date(submission.rejected_at);
const next14Days = new Date(rejectedAt.getTime() + 14 * 24 * 60 * 60 * 1000);
const now = new Date();
if (now < rejectedAt || now > next14Days) {
return res.status(400).json({ status: "failed", message: "Submission can only be updated within 14 days of rejection" });
}
}
submissionData.updated_by = submissionData.updated_by || req.user.id;
submissionData.updated_at = submissionData.updated_at || new Date();
@ -516,7 +527,7 @@ exports.submissionHistory = async (req, res) => {
]
},
where: { establishment_id },
order: [["id", "DESC"]],
order: [["updated_at", "DESC"]],
});
data = data.map(item => ({
@ -953,7 +964,8 @@ exports.approveOrRejectSubmission = async (req, res) => {
reject_reason,
approve_or_reject_by,
updated_by: req.user.id,
updated_at: new Date()
updated_at: new Date(),
rejected_at: approve_reject_status == 0 ? new Date() : null,
});
const QuarterlyWindowsConfiguration = db.QuarterlyWindowsConfiguration;

View File

@ -59,6 +59,10 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.DATE,
allowNull: true,
},
rejected_at: {
type: DataTypes.DATE,
allowNull: true,
},
}, {
tableName: "submission",
timestamps: false,

View File

@ -392,38 +392,111 @@ class AutomatedSchedulerService {
console.log(`[${new Date().toISOString()}] Email reminders completed.`);
}
async getAllQuarterlyWindows() {
const connection = await this.pool.getConnection();
try {
const query = `
SELECT
survey_name,
year,
quarter,
end_date,
start_date
FROM quarterly_windows_configuration_master
WHERE ipi_calculated = 0
ORDER BY year DESC,
CASE quarter
WHEN 'Q1' THEN 1
WHEN 'Q2' THEN 2
WHEN 'Q3' THEN 3
WHEN 'Q4' THEN 4
END DESC
`;
// const query = `
// SELECT
// year,
// quarter,
// end_date,
// start_date
// FROM quarterly_windows_configuration_master
// ORDER BY id DESC
// LIMIT 1
// `;
const [rows] = await connection.query(query);
return rows || null;
} finally {
connection.release();
}
}
async runScheduledEmails() {
//find latest quarterly window configuration
const latestWindow = await this.getLatestQuarterlyWindow();
if (!latestWindow) {
// find all quarterly window data ordered by year desc and quarter desc
const latestWindow = await this.getAllQuarterlyWindows();
// check if data exists
if (!latestWindow || latestWindow.length === 0) {
console.log(`[${new Date().toISOString()}] No quarterly window configuration found for email reminders`);
return;
}
console.log(`[${new Date().toISOString()}] Latest window for email reminders: Year=${latestWindow.year}, Quarter=${latestWindow.quarter}, End Date=${latestWindow.end_date}`);
// loop through all windows
for (const window of latestWindow) {
console.log(
`[${new Date().toISOString()}] Processing window: Year=${window.year}, Quarter=${window.quarter}, End Date=${window.end_date}`
);
// Check if current date is greater than end_date
const currentDate = new Date();
const endDate = new Date(latestWindow.end_date);
let currentDateStr = new Date().toLocaleDateString("en-CA");
let endDateStr = new Date(window.end_date).toLocaleDateString("en-CA");
const currentDateStr = new Date().toLocaleDateString("en-CA");
const endDateStr = new Date(latestWindow.end_date).toLocaleDateString("en-CA");
console.log(`[${new Date().toISOString()}] Current Date: ${currentDateStr}`);
console.log(`[${new Date().toISOString()}] End Date: ${endDateStr}`);
console.log(`[${new Date().toISOString()}] Current Date: ${currentDateStr}`);
console.log(`[${new Date().toISOString()}] End Date: ${endDateStr}`);
if (currentDateStr === endDateStr) {
console.log(`[${new Date().toISOString()}] Today is the last day. Sending reminder emails.`);
await this.sendEmailReminders(latestWindow);
} else {
console.log(`[${new Date().toISOString()}] Not the last day. Skipping reminder emails.`);
// Check if today is the end date
if (currentDateStr === endDateStr) {
console.log(`[${new Date().toISOString()}] Today is the last day. Sending reminder emails.`);
await this.sendEmailReminders(window);
} else {
console.log(`[${new Date().toISOString()}] Not the last day. Skipping reminder emails.`);
}
}
}
// async runScheduledEmails() {
// //find latest quarterly window configuration
// const latestWindow = await this.getLatestQuarterlyWindow();
// if (!latestWindow) {
// console.log(`[${new Date().toISOString()}] No quarterly window configuration found for email reminders`);
// return;
// }
// console.log(`[${new Date().toISOString()}] Latest window for email reminders: Year=${latestWindow.year}, Quarter=${latestWindow.quarter}, End Date=${latestWindow.end_date}`);
// // Check if current date is greater than end_date
// const currentDate = new Date();
// const endDate = new Date(latestWindow.end_date);
// const currentDateStr = new Date().toLocaleDateString("en-CA");
// const endDateStr = new Date(latestWindow.end_date).toLocaleDateString("en-CA");
// console.log(`[${new Date().toISOString()}] Current Date: ${currentDateStr}`);
// console.log(`[${new Date().toISOString()}] End Date: ${endDateStr}`);
// if (currentDateStr === endDateStr) {
// console.log(`[${new Date().toISOString()}] Today is the last day. Sending reminder emails.`);
// await this.sendEmailReminders(latestWindow);
// } else {
// console.log(`[${new Date().toISOString()}] Not the last day. Skipping reminder emails.`);
// }
// }
/**
* Start the scheduler
* Runs every day at 6:00 AM (you can adjust the time)