|
| 1 | +import { logger } from 'common/src/util/logger' |
| 2 | +import { LoopsClient, APIError } from 'loops' // Import LoopsClient and APIError |
| 3 | + |
| 4 | +import type { LoopsEmailData, SendEmailResult } from './types' |
| 5 | + |
| 6 | +const ORGANIZATION_INVITATION_TRANSACTIONAL_ID = 'cmbikixxm15xo4a0iiemzkzw1' |
| 7 | +const BASIC_TRANSACTIONAL_ID = 'cmb8pafk92r820w0i7lkplkt2' |
| 8 | + |
| 9 | +// Initialize Loops client |
| 10 | +let loopsClient: LoopsClient | null = null |
| 11 | +if (process.env.LOOPS_API_KEY) { |
| 12 | + loopsClient = new LoopsClient(process.env.LOOPS_API_KEY) |
| 13 | +} |
| 14 | + |
| 15 | +async function sendTransactionalEmail( |
| 16 | + transactionalId: string, |
| 17 | + email: string, |
| 18 | + dataVariables: Record<string, any> = {} |
| 19 | +): Promise<SendEmailResult> { |
| 20 | + if (!loopsClient) { |
| 21 | + return { |
| 22 | + success: false, |
| 23 | + error: |
| 24 | + 'Loops SDK not initialized (LOOPS_API_KEY missing or SDK init failed).', |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + const response = await loopsClient.sendTransactionalEmail({ |
| 30 | + transactionalId, |
| 31 | + email, |
| 32 | + dataVariables, |
| 33 | + }) |
| 34 | + |
| 35 | + logger.info( |
| 36 | + { email, transactionalId, loopsId: (response as any)?.id }, |
| 37 | + 'Loops transactional email sent successfully via SDK' |
| 38 | + ) |
| 39 | + return { success: true, loopsId: (response as any)?.id } |
| 40 | + } catch (error) { |
| 41 | + let errorMessage = 'Unknown SDK error during transactional email' |
| 42 | + if (error instanceof APIError) { |
| 43 | + logger.error( |
| 44 | + { |
| 45 | + ...error, |
| 46 | + email, |
| 47 | + transactionalId, |
| 48 | + errorType: 'APIError', |
| 49 | + }, |
| 50 | + `Loops APIError sending transactional email: ${error.message}` |
| 51 | + ) |
| 52 | + errorMessage = `Loops APIError: ${error.message} (Status: ${error.statusCode})` |
| 53 | + } else { |
| 54 | + logger.error( |
| 55 | + { email, transactionalId, error }, |
| 56 | + 'An unexpected error occurred sending transactional email via Loops SDK' |
| 57 | + ) |
| 58 | + } |
| 59 | + return { success: false, error: errorMessage } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export async function sendSignupEventToLoops( |
| 64 | + userId: string, |
| 65 | + email: string | null, |
| 66 | + name: string | null |
| 67 | +): Promise<void> { |
| 68 | + if (!loopsClient) { |
| 69 | + logger.warn({ userId }, 'Loops SDK not initialized. Skipping signup event.') |
| 70 | + return |
| 71 | + } |
| 72 | + if (!email) { |
| 73 | + logger.warn( |
| 74 | + { userId }, |
| 75 | + 'User email missing, cannot send Loops signup event.' |
| 76 | + ) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + const response = await loopsClient.sendEvent({ |
| 82 | + eventName: 'signup', |
| 83 | + email, |
| 84 | + userId, |
| 85 | + contactProperties: { |
| 86 | + firstName: name?.split(' ')[0] ?? '', |
| 87 | + }, |
| 88 | + }) |
| 89 | + |
| 90 | + logger.info( |
| 91 | + { email, userId, eventName: 'signup', loopsId: (response as any)?.id }, |
| 92 | + 'Sent signup event to Loops via SDK' |
| 93 | + ) |
| 94 | + } catch (error) { |
| 95 | + if (error instanceof APIError) { |
| 96 | + logger.error( |
| 97 | + { |
| 98 | + ...error, |
| 99 | + email, |
| 100 | + userId, |
| 101 | + eventName: 'signup', |
| 102 | + errorType: 'APIError', |
| 103 | + }, |
| 104 | + `Loops APIError sending event: ${error.message}` |
| 105 | + ) |
| 106 | + } else { |
| 107 | + logger.error( |
| 108 | + { error, email, userId, eventName: 'signup' }, |
| 109 | + 'An unexpected error occurred sending signup event via Loops SDK' |
| 110 | + ) |
| 111 | + } |
| 112 | + // Original function did not return error status, just logged. |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export async function sendOrganizationInvitationEmail( |
| 117 | + data: LoopsEmailData |
| 118 | +): Promise<SendEmailResult> { |
| 119 | + return sendTransactionalEmail( |
| 120 | + ORGANIZATION_INVITATION_TRANSACTIONAL_ID, |
| 121 | + data.email, |
| 122 | + { |
| 123 | + firstName: data.firstName || '', |
| 124 | + organizationName: data.organizationName || '', |
| 125 | + inviterName: data.inviterName || '', |
| 126 | + invitationUrl: data.invitationUrl || '', |
| 127 | + role: data.role || 'member', |
| 128 | + } |
| 129 | + ) |
| 130 | +} |
| 131 | + |
| 132 | +export async function sendBasicEmail( |
| 133 | + email: string, |
| 134 | + data: { subject: string; message: string } |
| 135 | +): Promise<SendEmailResult> { |
| 136 | + return sendTransactionalEmail(BASIC_TRANSACTIONAL_ID, email, { |
| 137 | + subject: data.subject, |
| 138 | + message: data.message, |
| 139 | + }) |
| 140 | +} |
0 commit comments