From b47acde85d54f011980752535e87057ea23a92f1 Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Wed, 11 Mar 2026 15:05:08 +0530 Subject: [PATCH] ordeby submission list , remainder email for all quarter window , 14 days restiction for resubmittion : GWM --- app/controllers/dashboard.controller.js | 2 +- app/controllers/submission.controller.js | 16 +++- app/models/submission.model.js | 4 + app/services/scheduler.service.js | 113 +++++++++++++++++++---- 4 files changed, 112 insertions(+), 23 deletions(-) diff --git a/app/controllers/dashboard.controller.js b/app/controllers/dashboard.controller.js index 9143de2..b757245 100644 --- a/app/controllers/dashboard.controller.js +++ b/app/controllers/dashboard.controller.js @@ -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: [ diff --git a/app/controllers/submission.controller.js b/app/controllers/submission.controller.js index e640a87..d05d57e 100644 --- a/app/controllers/submission.controller.js +++ b/app/controllers/submission.controller.js @@ -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; diff --git a/app/models/submission.model.js b/app/models/submission.model.js index 65b312e..8dfb667 100644 --- a/app/models/submission.model.js +++ b/app/models/submission.model.js @@ -59,6 +59,10 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.DATE, allowNull: true, }, + rejected_at: { + type: DataTypes.DATE, + allowNull: true, + }, }, { tableName: "submission", timestamps: false, diff --git a/app/services/scheduler.service.js b/app/services/scheduler.service.js index 0a90d6a..894924e 100644 --- a/app/services/scheduler.service.js +++ b/app/services/scheduler.service.js @@ -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)