-
Notifications
You must be signed in to change notification settings - Fork 319
feat: notifications for risk tasks #40
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a10d355
feat: notifications for risk tasks
carhartlewis 4c1f14b
Merge branch 'lewis/languine' of github.com:trycompai/comp into lewis…
carhartlewis cbdb84f
fix: coderabbit suggestions
carhartlewis 8e97414
fix: removed transaction
carhartlewis ecbae4b
fix: incorrect return statement in error handler
carhartlewis 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
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
95 changes: 95 additions & 0 deletions
95
apps/app/src/jobs/tasks/notifications/risk-task-notifications.ts
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,95 @@ | ||
| import { db } from "@bubba/db"; | ||
| import { | ||
| NotificationTypes, | ||
| TriggerEvents, | ||
| trigger, | ||
| } from "@bubba/notifications"; | ||
| import { logger, schedules } from "@trigger.dev/sdk/v3"; | ||
| import { formatDistance } from "date-fns"; | ||
|
|
||
| export const sendRiskTaskNotifications = schedules.task({ | ||
| id: "send-risk-task-notifications", | ||
| run: async () => { | ||
| const now = new Date(); | ||
| const upcomingThreshold = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000); | ||
|
|
||
| logger.info( | ||
| `Sending risk task notifications from now: ${now} to ${upcomingThreshold}`, | ||
| ); | ||
|
|
||
| const tasks = await db.riskMitigationTask.findMany({ | ||
| where: { | ||
| dueDate: { gte: now, lte: upcomingThreshold }, | ||
| status: { in: ["open", "pending"] }, | ||
| notifiedAt: null, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| dueDate: true, | ||
| notifiedAt: true, | ||
| riskId: true, | ||
| title: true, | ||
| owner: { | ||
| select: { | ||
| id: true, | ||
| email: true, | ||
| name: true, | ||
| image: true, | ||
| organizationId: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const notifiedTasks = []; | ||
|
|
||
| for (const task of tasks) { | ||
| const owner = task.owner; | ||
|
|
||
| const timeUntilDue = task.dueDate | ||
| ? formatDistance(task.dueDate, new Date(), { | ||
| addSuffix: true, | ||
| }) | ||
| : "soon"; | ||
|
|
||
| try { | ||
| if (!owner || !owner.email || !owner.organizationId) { | ||
| logger.warn(`Skipping task ${task.id} - owner ${owner?.id} missing email or organizationId`); | ||
| continue; | ||
| } | ||
|
|
||
| await db.riskMitigationTask.update({ | ||
| where: { id: task.id }, | ||
| data: { notifiedAt: new Date() }, | ||
| }); | ||
|
|
||
| await trigger({ | ||
| name: TriggerEvents.TaskReminderInApp, | ||
| user: { | ||
| subscriberId: `${owner.organizationId}_${owner.id}`, | ||
| email: owner.email, | ||
| fullName: owner.name, | ||
| image: owner.image, | ||
| organizationId: owner.organizationId, | ||
| }, | ||
| payload: { | ||
| description: `${task.title} is due ${timeUntilDue}`, | ||
| recordId: `/risk/${task.riskId}/tasks/${task.id}`, | ||
| type: NotificationTypes.Task, | ||
| }, | ||
| }); | ||
|
|
||
|
|
||
| notifiedTasks.push(task.id); | ||
| } catch (error) { | ||
| logger.error( | ||
| `Error processing task ${task.id} for ${owner?.email}: ${error}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (notifiedTasks.length) { | ||
| logger.info(`Sent notifications for tasks: ${notifiedTasks.join(", ")}`); | ||
| } | ||
| }, | ||
| }); | ||
49 changes: 49 additions & 0 deletions
49
apps/app/src/jobs/tasks/notifications/utils/task-email-notification.tsx
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,49 @@ | ||
| import TaskReminderEmail from "@bubba/email/emails/reminders/task-reminder"; | ||
| import { TriggerEvents, trigger } from "@bubba/notifications"; | ||
| import { render } from "@react-email/render"; | ||
|
|
||
| interface Props { | ||
| owner: { | ||
| id: string; | ||
| fullName?: string; | ||
| email: string; | ||
| organizationId: string; | ||
| }; | ||
| task: { | ||
| recordId: string; | ||
| dueDate: string; | ||
| }; | ||
| } | ||
|
|
||
| export async function sendTaskEmailNotification({ owner, task }: Props) { | ||
| try { | ||
| const html = await render( | ||
| <TaskReminderEmail | ||
| email={owner.email} | ||
| name={owner.fullName ?? "there"} | ||
| dueDate={task.dueDate} | ||
| recordId={task.recordId} | ||
| />, | ||
| ); | ||
|
|
||
| const triggerData = { | ||
| name: TriggerEvents.TaskReminderEmail, | ||
| payload: { | ||
| subject: "Task Reminder", | ||
| html, | ||
| }, | ||
| replyTo: owner.email, | ||
| user: { | ||
| subscriberId: `${owner.organizationId}_${owner.id}`, | ||
| organizationId: owner.organizationId, | ||
| email: owner.email, | ||
| fullName: owner.fullName, | ||
| }, | ||
| }; | ||
|
|
||
| await trigger(triggerData); | ||
| } catch (error) { | ||
| console.error("Failed to send task email notification: ", error); | ||
| throw error; | ||
| } | ||
| } |
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
Binary file not shown.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using a transaction for atomic updates.
The task update and notification trigger should be atomic to prevent duplicate notifications if the trigger fails.
try { + await db.$transaction(async (tx) => { - await db.riskMitigationTask.update({ + await tx.riskMitigationTask.update({ where: { id: task.id }, data: { notifiedAt: new Date() }, }); await trigger({ name: TriggerEvents.TaskReminderInApp, // ... rest of the trigger config }); + });Also applies to: 66-80