|
| 1 | +import { createTemplateComponentFromHtml } from "@/lib/email-rendering"; |
| 2 | +import { getEmailConfig, normalizeEmail, sendEmailToMany } from "@/lib/emails"; |
| 3 | +import { getNotificationCategoryByName } from "@/lib/notification-categories"; |
| 4 | +import { Tenancy } from "@/lib/tenancies"; |
| 5 | +import { UsersCrud } from "@stackframe/stack-shared/dist/interface/crud/users"; |
| 6 | +import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env"; |
| 7 | +import { throwErr, StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors"; |
| 8 | +import { escapeHtml } from "@stackframe/stack-shared/dist/utils/html"; |
| 9 | +import { urlString } from "@stackframe/stack-shared/dist/utils/urls"; |
| 10 | + |
| 11 | +const defaultRecipient = "team@stack-auth.com"; |
| 12 | +const transactionalCategoryId = getNotificationCategoryByName("Transactional")?.id ?? throwErr("Transactional notification category not found"); |
| 13 | + |
| 14 | +function formatTextForHtml(text: string): string { |
| 15 | + return escapeHtml(text).replace(/\n/g, "<br />"); |
| 16 | +} |
| 17 | + |
| 18 | +function sanitizeSubject(value: string): string { |
| 19 | + return value.replace(/\s+/g, " ").trim(); |
| 20 | +} |
| 21 | + |
| 22 | +export function getInternalFeedbackRecipients(): string[] { |
| 23 | + const rawRecipients = getEnvVariable("STACK_INTERNAL_FEEDBACK_RECIPIENTS", defaultRecipient); |
| 24 | + const recipients = rawRecipients.split(",").map((recipient) => recipient.trim()); |
| 25 | + |
| 26 | + if (recipients.some((recipient) => recipient.length === 0)) { |
| 27 | + throw new StackAssertionError("STACK_INTERNAL_FEEDBACK_RECIPIENTS contains an empty recipient", { |
| 28 | + rawRecipients, |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + return [...new Set(recipients.map((recipient) => normalizeEmail(recipient)))]; |
| 33 | +} |
| 34 | + |
| 35 | +async function sendInternalOperationsEmail(options: { |
| 36 | + tenancy: Tenancy, |
| 37 | + subject: string, |
| 38 | + htmlContent: string, |
| 39 | +}) { |
| 40 | + await getEmailConfig(options.tenancy); |
| 41 | + |
| 42 | + const recipients = getInternalFeedbackRecipients(); |
| 43 | + const tsxSource = createTemplateComponentFromHtml(options.htmlContent); |
| 44 | + |
| 45 | + await sendEmailToMany({ |
| 46 | + tenancy: options.tenancy, |
| 47 | + recipients: recipients.map((recipient) => ({ type: "custom-emails" as const, emails: [recipient] })), |
| 48 | + tsxSource, |
| 49 | + extraVariables: {}, |
| 50 | + themeId: null, |
| 51 | + isHighPriority: true, |
| 52 | + shouldSkipDeliverabilityCheck: true, |
| 53 | + scheduledAt: new Date(), |
| 54 | + createdWith: { type: "programmatic-call", templateId: null }, |
| 55 | + overrideSubject: sanitizeSubject(options.subject), |
| 56 | + overrideNotificationCategoryId: transactionalCategoryId, |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +export async function sendSupportFeedbackEmail(options: { |
| 61 | + tenancy: Tenancy, |
| 62 | + user: UsersCrud["Admin"]["Read"], |
| 63 | + name: string | null, |
| 64 | + email: string, |
| 65 | + message: string, |
| 66 | +}) { |
| 67 | + const displayName = options.name ?? options.user.display_name ?? "Not provided"; |
| 68 | + const htmlContent = ` |
| 69 | + <div style="font-family: Arial, sans-serif; max-width: 720px; margin: 0 auto; padding: 24px; color: #1f2937;"> |
| 70 | + <h2 style="margin: 0 0 20px;">Support feedback submission</h2> |
| 71 | + <p><strong>Sender name:</strong> ${formatTextForHtml(displayName)}</p> |
| 72 | + <p><strong>Sender email:</strong> ${formatTextForHtml(options.email)}</p> |
| 73 | + <p><strong>Stack Auth user ID:</strong> ${formatTextForHtml(options.user.id)}</p> |
| 74 | + <p><strong>Stack Auth display name:</strong> ${formatTextForHtml(options.user.display_name ?? "Not provided")}</p> |
| 75 | + <div style="margin-top: 24px;"> |
| 76 | + <p style="margin-bottom: 8px;"><strong>Message</strong></p> |
| 77 | + <div style="padding: 16px; border: 1px solid #d1d5db; border-radius: 8px; background: #f9fafb; white-space: normal;"> |
| 78 | + ${formatTextForHtml(options.message)} |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + `; |
| 83 | + |
| 84 | + await sendInternalOperationsEmail({ |
| 85 | + tenancy: options.tenancy, |
| 86 | + subject: `[Support] ${options.email}`, |
| 87 | + htmlContent, |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +export async function sendFeatureRequestNotificationEmail(options: { |
| 92 | + tenancy: Tenancy, |
| 93 | + user: UsersCrud["Admin"]["Read"], |
| 94 | + title: string, |
| 95 | + content: string | null, |
| 96 | + featureRequestId: string, |
| 97 | +}) { |
| 98 | + const featureRequestUrl = new URL(urlString`/p/${options.featureRequestId}`, "https://feedback.stack-auth.com").toString(); |
| 99 | + const htmlContent = ` |
| 100 | + <div style="font-family: Arial, sans-serif; max-width: 720px; margin: 0 auto; padding: 24px; color: #1f2937;"> |
| 101 | + <h2 style="margin: 0 0 20px;">New feature request submitted</h2> |
| 102 | + <p><strong>Title:</strong> ${formatTextForHtml(options.title)}</p> |
| 103 | + <p><strong>Featurebase post ID:</strong> ${formatTextForHtml(options.featureRequestId)}</p> |
| 104 | + <p><strong>Featurebase URL:</strong> <a href="${escapeHtml(featureRequestUrl)}">${escapeHtml(featureRequestUrl)}</a></p> |
| 105 | + <p><strong>Submitted by:</strong> ${formatTextForHtml(options.user.display_name ?? "Not provided")}</p> |
| 106 | + <p><strong>Submitted email:</strong> ${formatTextForHtml(options.user.primary_email ?? "Not provided")}</p> |
| 107 | + <p><strong>Stack Auth user ID:</strong> ${formatTextForHtml(options.user.id)}</p> |
| 108 | + <div style="margin-top: 24px;"> |
| 109 | + <p style="margin-bottom: 8px;"><strong>Details</strong></p> |
| 110 | + <div style="padding: 16px; border: 1px solid #d1d5db; border-radius: 8px; background: #f9fafb; white-space: normal;"> |
| 111 | + ${formatTextForHtml(options.content ?? "Not provided")} |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + `; |
| 116 | + |
| 117 | + await sendInternalOperationsEmail({ |
| 118 | + tenancy: options.tenancy, |
| 119 | + subject: `[Feature Request] ${options.title}`, |
| 120 | + htmlContent, |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +import.meta.vitest?.test("getInternalFeedbackRecipients()", ({ expect }) => { |
| 125 | + // eslint-disable-next-line no-restricted-syntax |
| 126 | + const previousValue = process.env.STACK_INTERNAL_FEEDBACK_RECIPIENTS; |
| 127 | + |
| 128 | + // eslint-disable-next-line no-restricted-syntax |
| 129 | + process.env.STACK_INTERNAL_FEEDBACK_RECIPIENTS = "TEAM@stack-auth.com, team@stack-auth.com , another@example.com"; |
| 130 | + expect(getInternalFeedbackRecipients()).toEqual([ |
| 131 | + "team@stack-auth.com", |
| 132 | + "another@example.com", |
| 133 | + ]); |
| 134 | + |
| 135 | + // eslint-disable-next-line no-restricted-syntax |
| 136 | + process.env.STACK_INTERNAL_FEEDBACK_RECIPIENTS = "valid@example.com, "; |
| 137 | + expect(() => getInternalFeedbackRecipients()).toThrow("empty recipient"); |
| 138 | + |
| 139 | + // eslint-disable-next-line no-restricted-syntax |
| 140 | + process.env.STACK_INTERNAL_FEEDBACK_RECIPIENTS = ", "; |
| 141 | + expect(() => getInternalFeedbackRecipients()).toThrow("empty recipient"); |
| 142 | + |
| 143 | + if (previousValue === undefined) { |
| 144 | + // eslint-disable-next-line no-restricted-syntax |
| 145 | + delete process.env.STACK_INTERNAL_FEEDBACK_RECIPIENTS; |
| 146 | + } else { |
| 147 | + // eslint-disable-next-line no-restricted-syntax |
| 148 | + process.env.STACK_INTERNAL_FEEDBACK_RECIPIENTS = previousValue; |
| 149 | + } |
| 150 | +}); |
0 commit comments