-
Notifications
You must be signed in to change notification settings - Fork 30
Feat/implement scheduled email reminders #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
laryhills
wants to merge
6
commits into
AURORALAOrg:main
Choose a base branch
from
laryhills:feat/implement-scheduled-email-reminders
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
18dc621
feat: implement user reminder functionality with cron job settings anβ¦
laryhills f573a0d
refactor: streamline graceful shutdown handling in server and clean uβ¦
laryhills cd330b6
refactor: :recycle: applied suggested feedbacks
laryhills 7fa1aa9
Merge branch 'main' into feat/implement-scheduled-email-reminders
laryhills 80c6ba9
Fixing email send issue zeptomail <-> smtp
Gerson2102 5440caf
Merge branch 'main' into feat/implement-scheduled-email-reminders
laryhills File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +0,0 @@ | ||
| {"level":"error","message":"TypeError: Cannot read properties of undefined (reading 'id')\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\controllers\\question.controller.ts:8:39\n at Generator.next (<anonymous>)\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\controllers\\question.controller.ts:8:71\n at new Promise (<anonymous>)\n at __awaiter (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\controllers\\question.controller.ts:4:12)\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\controllers\\question.controller.ts:7:82\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\middlewares\\async.ts:6:19\n at Layer.handle [as handle_request] (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\express\\lib\\router\\layer.js:95:5)\n at next (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\express\\lib\\router\\route.js:149:13)\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\middlewares\\validator.ts:25:12"} | ||
| {"level":"error","message":"TypeError: Cannot read properties of undefined (reading 'id')\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\controllers\\question.controller.ts:8:39\n at Generator.next (<anonymous>)\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\controllers\\question.controller.ts:8:71\n at new Promise (<anonymous>)\n at __awaiter (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\controllers\\question.controller.ts:4:12)\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\controllers\\question.controller.ts:7:82\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\middlewares\\async.ts:6:19\n at Layer.handle [as handle_request] (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\express\\lib\\router\\layer.js:95:5)\n at next (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\express\\lib\\router\\route.js:149:13)\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\src\\middlewares\\validator.ts:25:12"} | ||
| {"level":"error","message":"SyntaxError: Unexpected token '`', ...\"entence\": `\"My daugh\"... is not valid JSON\n at JSON.parse (<anonymous>)\n at parse (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\body-parser\\lib\\types\\json.js:92:19)\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\body-parser\\lib\\read.js:128:18\n at AsyncResource.runInAsyncScope (node:async_hooks:214:14)\n at invokeCallback (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\raw-body\\index.js:238:16)\n at done (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\raw-body\\index.js:227:7)\n at IncomingMessage.onEnd (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\raw-body\\index.js:287:7)\n at IncomingMessage.emit (node:events:518:28)\n at IncomingMessage.emit (node:domain:489:12)\n at endReadableNT (node:internal/streams/readable:1698:12)"} | ||
| {"level":"error","message":"SyntaxError: Unexpected token '`', ...\"entence\": `\"My daugh\"... is not valid JSON\n at JSON.parse (<anonymous>)\n at parse (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\body-parser\\lib\\types\\json.js:92:19)\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\body-parser\\lib\\read.js:128:18\n at AsyncResource.runInAsyncScope (node:async_hooks:214:14)\n at invokeCallback (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\raw-body\\index.js:238:16)\n at done (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\raw-body\\index.js:227:7)\n at IncomingMessage.onEnd (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\raw-body\\index.js:287:7)\n at IncomingMessage.emit (node:events:518:28)\n at IncomingMessage.emit (node:domain:489:12)\n at endReadableNT (node:internal/streams/readable:1698:12)"} | ||
| {"level":"error","message":"SyntaxError: Unexpected token '`', ...\"entence\": `\"My daugh\"... is not valid JSON\n at JSON.parse (<anonymous>)\n at parse (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\body-parser\\lib\\types\\json.js:92:19)\n at C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\body-parser\\lib\\read.js:128:18\n at AsyncResource.runInAsyncScope (node:async_hooks:214:14)\n at invokeCallback (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\raw-body\\index.js:238:16)\n at done (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\raw-body\\index.js:227:7)\n at IncomingMessage.onEnd (C:\\projects\\others\\onlyDust\\AURORA\\AURORA-Backend\\backend\\node_modules\\raw-body\\index.js:287:7)\n at IncomingMessage.emit (node:events:518:28)\n at IncomingMessage.emit (node:domain:489:12)\n at endReadableNT (node:internal/streams/readable:1698:12)"} | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| # Please do not edit this file manually | ||
| # It should be added in your version-control system (e.g., Git) | ||
| provider = "postgresql" | ||
| provider = "postgresql" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import NotificationService from "../services/notification.service"; | ||
| import EmailNotifier from "../utils/service/emailNotifier"; | ||
| import UserService from "../services/user.service"; | ||
|
|
||
| interface JobResult { | ||
| success: boolean; | ||
| stats: { | ||
| candidatesFound: number; | ||
| emailsSent: number; | ||
| emailsFailed: number; | ||
| }; | ||
| errors: Array<{ email: string; error: string }>; | ||
| executionTime: number; | ||
| } | ||
|
|
||
| class UserReminderJob { | ||
| /** | ||
| * Main job function to check inactive users and send reminders | ||
| */ | ||
| public static async checkInactiveUsers(maxRetries: number = 2): Promise<JobResult> { | ||
| const startTime = Date.now(); | ||
| console.log("π Starting user reminder job..."); | ||
|
|
||
| try { | ||
| // Get reminder statistics for logging | ||
| const stats = await NotificationService.getReminderStats(); | ||
| console.log("π Reminder Stats:", { | ||
| totalInactive: stats.totalInactive, | ||
| candidatesFound: stats.candidatesFound, | ||
| breakdown: stats.breakdown, | ||
| }); | ||
|
|
||
| // Find users who need reminders | ||
| const candidates = await NotificationService.findReminderCandidates(); | ||
|
|
||
| if (candidates.length === 0) { | ||
| console.log("β No users found requiring reminders"); | ||
| return { | ||
| success: true, | ||
| stats: { | ||
| candidatesFound: 0, | ||
| emailsSent: 0, | ||
| emailsFailed: 0, | ||
| }, | ||
| errors: [], | ||
| executionTime: Date.now() - startTime, | ||
| }; | ||
| } | ||
|
|
||
| console.log(`π§ Sending reminders to ${candidates.length} users`); | ||
|
|
||
| // Prepare email data | ||
| const reminderEmails = candidates.map(candidate => ({ | ||
| email: candidate.email, | ||
| firstName: candidate.firstName || undefined, | ||
| reminderType: candidate.reminderType, | ||
| daysSinceActivity: candidate.daysSinceActivity, | ||
| })); | ||
|
|
||
| // Send emails with retry logic | ||
| const emailResults = await EmailNotifier.sendReminderEmails(reminderEmails, maxRetries); | ||
|
|
||
| // Update lastReminderSent for successfully sent emails | ||
| const successfulEmails = emailResults.successfulEmails || []; | ||
| const userIdsToUpdate = successfulEmails | ||
| .map(email => candidates.find(c => c.email === email)?.id) | ||
| .filter(id => id !== undefined) as string[]; | ||
|
|
||
| if (userIdsToUpdate.length > 0) { | ||
| await this.updateReminderSentTimestamps(userIdsToUpdate); | ||
| } | ||
|
|
||
| const executionTime = Date.now() - startTime; | ||
|
|
||
| console.log("β User reminder job completed:", { | ||
| candidatesFound: candidates.length, | ||
| emailsSent: emailResults.success, | ||
| emailsFailed: emailResults.failed, | ||
| executionTimeMs: executionTime, | ||
| }); | ||
|
|
||
| return { | ||
| success: true, | ||
| stats: { | ||
| candidatesFound: candidates.length, | ||
| emailsSent: emailResults.success, | ||
| emailsFailed: emailResults.failed, | ||
| }, | ||
| errors: emailResults.errors, | ||
| executionTime, | ||
| }; | ||
|
|
||
| } catch (error) { | ||
| const executionTime = Date.now() - startTime; | ||
| console.error("β User reminder job failed:", error); | ||
|
|
||
| return { | ||
| success: false, | ||
| stats: { | ||
| candidatesFound: 0, | ||
| emailsSent: 0, | ||
| emailsFailed: 0, | ||
| }, | ||
| errors: [{ email: "system", error: error instanceof Error ? error.message : "Unknown error" }], | ||
| executionTime, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update lastReminderSent timestamp for users who received reminders | ||
| */ | ||
| private static async updateReminderSentTimestamps(userIds: string[]): Promise<void> { | ||
| try { | ||
| await UserService.updateReminderSentBatch(userIds); | ||
| console.log(`β Updated reminder timestamps for ${userIds.length} users`); | ||
| } catch (error) { | ||
| console.error("β Failed to update reminder timestamps:", error); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Manual trigger for testing purposes | ||
| */ | ||
| public static async triggerManual(): Promise<JobResult> { | ||
| console.log("π§ Manual trigger of user reminder job"); | ||
| return this.checkInactiveUsers(); | ||
| } | ||
|
|
||
| /** | ||
| * Get job health status | ||
| */ | ||
| public static async getHealthStatus(): Promise<{ | ||
| isHealthy: boolean; | ||
| lastRun?: Date; | ||
| nextRun?: string; | ||
| }> { | ||
| try { | ||
| await NotificationService.getReminderStats(); | ||
|
|
||
| return { | ||
| isHealthy: true, | ||
| lastRun: new Date(), | ||
| nextRun: "Based on CRON schedule", | ||
| }; | ||
| } catch (error) { | ||
| return { | ||
| isHealthy: false, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default UserReminderJob; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.