GWM : corn for reminder email
This commit is contained in:
parent
2e00784921
commit
03dfdd39cd
@ -2,6 +2,7 @@ const cron = require('node-cron');
|
||||
const mysql = require('mysql2/promise');
|
||||
const SubmissionAutoFillService = require('./auto_fill_missing_quarterly_submissions_service'); // Adjust path
|
||||
const IPICalculationService = require('./ipi_calculation_service');
|
||||
const { sendEmailService } = require("./email.service");
|
||||
|
||||
// Database configuration
|
||||
const dbConfig = {
|
||||
@ -36,6 +37,7 @@ class AutomatedSchedulerService {
|
||||
try {
|
||||
const query = `
|
||||
SELECT
|
||||
survey_name,
|
||||
year,
|
||||
quarter,
|
||||
end_date,
|
||||
@ -265,12 +267,39 @@ class AutomatedSchedulerService {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
async getEstablishmentsWithoutSubmission(year, quarter) {
|
||||
|
||||
const connection = await this.pool.getConnection();
|
||||
|
||||
try {
|
||||
@ -287,12 +316,10 @@ async getEstablishmentsWithoutSubmission(year, quarter) {
|
||||
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM submissions s
|
||||
FROM submission s
|
||||
WHERE s.establishment_id = e.id
|
||||
AND s.year = ?
|
||||
AND s.quarter = ?
|
||||
AND s.status != 'Draft'
|
||||
)
|
||||
AND s.quarter = ? ) AND e.is_active = 1
|
||||
`;
|
||||
|
||||
const [rows] = await connection.query(query, [year, quarter]);
|
||||
@ -307,7 +334,6 @@ async getEstablishmentsWithoutSubmission(year, quarter) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async sendReminderEmails(rows, latestWindow) {
|
||||
try {
|
||||
if (!rows.length) {
|
||||
@ -315,23 +341,22 @@ async sendReminderEmails(rows, latestWindow) {
|
||||
return;
|
||||
}
|
||||
|
||||
const surveyData = await this.getSurveyData(
|
||||
latestWindow.year,
|
||||
latestWindow.quarter
|
||||
);
|
||||
|
||||
const basePlaceholder = {
|
||||
survey_name: latestWindow.survey_name,
|
||||
quarter: latestWindow.quarter,
|
||||
year: latestWindow.year,
|
||||
survey_name: surveyData?.survey_name,
|
||||
opens_on: latestWindow.start_date,
|
||||
closes_on: latestWindow.end_date,
|
||||
portal_url: process.env.FE_BASE_URL,
|
||||
support_email: process.env.SUPPORT_EMAIL,
|
||||
support_phone: process.env.SUPPORT_PHONE,
|
||||
portal_url: process.env.FE_BASE_URL,
|
||||
logo_url: `${process.env.APP_BASE_URL}/assets/FCSCLogo.png`,
|
||||
logo_url: `${process.env.APP_BASE_URL}/assets/FCSCLogo.png`
|
||||
};
|
||||
|
||||
|
||||
|
||||
const promises = rows.map((r) =>
|
||||
sendEmailService(r.email, "submission_reminder_mail", {
|
||||
sendEmailService(r.email, "submission_last_day_reminder_mail", {
|
||||
...basePlaceholder,
|
||||
user_name: r.user_name,
|
||||
establishment_name: r.factory_name,
|
||||
@ -347,14 +372,12 @@ async sendReminderEmails(rows, latestWindow) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async sendEmailReminders(latestWindow) {
|
||||
console.log(
|
||||
`[${new Date().toISOString()}] Sending reminders for Year=${latestWindow.year}, Quarter=${latestWindow.quarter}`
|
||||
);
|
||||
|
||||
const establishments = await getEstablishmentsWithoutSubmission(
|
||||
const establishments = await this.getEstablishmentsWithoutSubmission(
|
||||
latestWindow.year,
|
||||
latestWindow.quarter
|
||||
);
|
||||
@ -364,12 +387,11 @@ async sendEmailReminders(latestWindow) {
|
||||
return;
|
||||
}
|
||||
|
||||
await sendReminderEmails(establishments, latestWindow);
|
||||
await this.sendReminderEmails(establishments, latestWindow);
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Email reminders completed.`);
|
||||
}
|
||||
|
||||
|
||||
async runScheduledEmails() {
|
||||
|
||||
//find latest quarterly window configuration
|
||||
@ -382,33 +404,32 @@ async sendEmailReminders(latestWindow) {
|
||||
|
||||
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);
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Current Date: ${currentDate.toISOString()}`);
|
||||
console.log(`[${new Date().toISOString()}] End Date: ${endDate.toISOString()}`);
|
||||
const currentDateStr = new Date().toLocaleDateString("en-CA");
|
||||
const endDateStr = new Date(latestWindow.end_date).toLocaleDateString("en-CA");
|
||||
|
||||
if (currentDate < endDate) {
|
||||
console.log(`[${new Date().toISOString()}] Current date is not greater than end_date. Skipping email reminders.`);
|
||||
} else {
|
||||
console.log(`[${new Date().toISOString()}] Current date is greater than end_date. Sending email reminders.`);
|
||||
// Call your email reminder service here, passing the latestWindow details if needed
|
||||
// Send email remainders for All esatblishmets which have not submitted their survey for the quarter which has just opened and also for thouse who have not submitted till end date (This is a last day reminder)
|
||||
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)
|
||||
*/
|
||||
startRemainderEmail() {
|
||||
console.log(`[${new Date().toISOString()}] Starting automated scheduler...`);
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Starting automated email reminder scheduler...`);
|
||||
// Run every day at 6:00 AM
|
||||
// Format: minute hour day month weekday
|
||||
// '0 6 * * *' = At 6:00 AM every day
|
||||
|
||||
@ -307,6 +307,7 @@ app.post("/deploy", deploymentController.deployment);
|
||||
*/
|
||||
const scheduler = new AutomatedSchedulerService();
|
||||
scheduler.start();
|
||||
scheduler.startRemainderEmail();
|
||||
// manual trigger endpoint
|
||||
app.post('/api/admin/trigger-scheduler', async (req, res) => {
|
||||
try {
|
||||
@ -328,5 +329,6 @@ app.post('/api/admin/trigger-scheduler', async (req, res) => {
|
||||
const PORT = process.env.PORT || 5000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`🚀 Server running on port ${PORT}`);
|
||||
console.log(`Automated scheduler is active and will run daily at 2:00 AM`);
|
||||
console.log(`Automated scheduler (Index Calculation) is active and will run daily at 2:00 AM`);
|
||||
console.log(`Automated scheduler (Reminder Emails) is active and will run daily at 6:00 AM`);
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user