|
1 | 1 | 'use server'; |
2 | 2 |
|
| 3 | +import { sendNewPolicyEmail } from '@/jobs/tasks/email/new-policy-email'; |
3 | 4 | import { db, PolicyStatus } from '@db'; |
| 5 | +import { tasks } from '@trigger.dev/sdk'; |
4 | 6 | import { revalidatePath, revalidateTag } from 'next/cache'; |
5 | 7 | import { z } from 'zod'; |
6 | 8 | import { authActionClient } from '../safe-action'; |
@@ -92,34 +94,36 @@ export const acceptRequestedPolicyChangesAction = authActionClient |
92 | 94 | return roles.includes('employee'); |
93 | 95 | }); |
94 | 96 |
|
95 | | - // Call /api/send-policy-email to send emails to employees |
96 | | - |
97 | 97 | // Prepare the events array for the API |
98 | 98 | const events = employeeMembers |
99 | 99 | .filter((employee) => employee.user.email) |
100 | | - .map((employee) => ({ |
101 | | - subscriberId: `${employee.user.id}-${session.activeOrganizationId}`, |
102 | | - email: employee.user.email, |
103 | | - userName: employee.user.name || employee.user.email || 'Employee', |
104 | | - policyName: policy.name, |
105 | | - organizationName: policy.organization.name, |
106 | | - url: `${process.env.NEXT_PUBLIC_PORTAL_URL ?? 'https://portal.trycomp.ai'}/${session.activeOrganizationId}`, |
107 | | - description: `The "${policy.name}" policy has been ${isNewPolicy ? 'created' : 'updated'}.`, |
108 | | - })); |
| 100 | + .map((employee) => { |
| 101 | + let notificationType: 'new' | 're-acceptance' | 'updated'; |
| 102 | + const wasAlreadySigned = policy.signedBy.includes(employee.id); |
| 103 | + if (isNewPolicy) { |
| 104 | + notificationType = 'new'; |
| 105 | + } else if (wasAlreadySigned) { |
| 106 | + notificationType = 're-acceptance'; |
| 107 | + } else { |
| 108 | + notificationType = 'updated'; |
| 109 | + } |
| 110 | + |
| 111 | + return { |
| 112 | + email: employee.user.email, |
| 113 | + userName: employee.user.name || employee.user.email || 'Employee', |
| 114 | + policyName: policy.name, |
| 115 | + organizationId: session.activeOrganizationId || '', |
| 116 | + organizationName: policy.organization.name, |
| 117 | + notificationType, |
| 118 | + }; |
| 119 | + }); |
109 | 120 |
|
110 | 121 | // Call the API route to send the emails |
111 | | - try { |
112 | | - await fetch(`${process.env.BETTER_AUTH_URL ?? ''}/api/send-policy-email`, { |
113 | | - method: 'POST', |
114 | | - headers: { |
115 | | - 'Content-Type': 'application/json', |
116 | | - }, |
117 | | - body: JSON.stringify(events), |
118 | | - }); |
119 | | - } catch (error) { |
120 | | - console.error('Failed to call /api/send-policy-email:', error); |
121 | | - // Don't throw, just log |
122 | | - } |
| 122 | + await Promise.all( |
| 123 | + events.map((event) => |
| 124 | + tasks.trigger<typeof sendNewPolicyEmail>('send-new-policy-email', event), |
| 125 | + ), |
| 126 | + ); |
123 | 127 |
|
124 | 128 | // If a comment was provided, create a comment |
125 | 129 | if (comment && comment.trim() !== '') { |
|
0 commit comments