-
Notifications
You must be signed in to change notification settings - Fork 514
added cron job to for daily failed email digest #714
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
11 commits
Select commit
Hold shift + click to select a range
c4ef137
added cron job to for daily failed email digest
BilalG1 6490b15
fixed failed emails digest tests, cron secret, and escaped html
BilalG1 37f6ddf
empty
BilalG1 8bd6bf4
Merge branch 'dev' into failed-emails-digest
N2D4 c627bcd
Use inline snapshot for failing test
N2D4 8269438
more
N2D4 f879279
fix
N2D4 edc971e
log
N2D4 989a848
fix?
N2D4 bf2369e
fix
N2D4 2dac486
Merge branch 'dev' into failed-emails-digest
N2D4 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
48 changes: 48 additions & 0 deletions
48
apps/backend/src/app/api/latest/internal/failed-emails-digest/crud.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,48 @@ | ||
| import { prismaClient } from "@/prisma-client"; | ||
|
|
||
| type FailedEmailsQueryResult = { | ||
| tenancyId: string, | ||
| projectId: string, | ||
| to: string[], | ||
| subject: string, | ||
| contactEmail: string, | ||
| } | ||
|
|
||
| type FailedEmailsByTenancyData = { | ||
| emails: Array<{ subject: string, to: string[] }>, | ||
| tenantOwnerEmail: string, | ||
| projectId: string, | ||
| } | ||
|
|
||
| export const getFailedEmailsByTenancy = async (after: Date) => { | ||
| const result = await prismaClient.$queryRaw<Array<FailedEmailsQueryResult>>` | ||
| SELECT | ||
| se."tenancyId", | ||
| t."projectId", | ||
| se."to", | ||
|
N2D4 marked this conversation as resolved.
|
||
| se."subject", | ||
| cc."value" as "contactEmail" | ||
| FROM "SentEmail" se | ||
| INNER JOIN "Tenancy" t ON se."tenancyId" = t.id | ||
| LEFT JOIN "ProjectUser" pu ON pu."mirroredProjectId" = 'internal' | ||
|
BilalG1 marked this conversation as resolved.
|
||
| AND pu."mirroredBranchId" = 'main' | ||
| AND pu."serverMetadata"->'managedProjectIds' ? t."projectId" | ||
| LEFT JOIN "ContactChannel" cc ON pu."projectUserId" = cc."projectUserId" | ||
| AND cc."isPrimary" = 'TRUE' | ||
| AND cc."type" = 'EMAIL' | ||
| WHERE se."error" IS NOT NULL | ||
| AND se."createdAt" >= ${after} | ||
| `; | ||
|
|
||
| const failedEmailsByTenancy = new Map<string, FailedEmailsByTenancyData>(); | ||
| for (const failedEmail of result) { | ||
| let failedEmails = failedEmailsByTenancy.get(failedEmail.tenancyId) ?? { | ||
| emails: [], | ||
| tenantOwnerEmail: failedEmail.contactEmail, | ||
| projectId: failedEmail.projectId | ||
| }; | ||
| failedEmails.emails.push({ subject: failedEmail.subject, to: failedEmail.to }); | ||
| failedEmailsByTenancy.set(failedEmail.tenancyId, failedEmails); | ||
| } | ||
| return failedEmailsByTenancy; | ||
| }; | ||
88 changes: 88 additions & 0 deletions
88
apps/backend/src/app/api/latest/internal/failed-emails-digest/route.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,88 @@ | ||
| import { getSharedEmailConfig, sendEmail } from "@/lib/emails"; | ||
| import { DEFAULT_BRANCH_ID, getSoleTenancyFromProjectBranch } from "@/lib/tenancies"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { yupArray, yupBoolean, yupNumber, yupObject, yupString, yupTuple } from "@stackframe/stack-shared/dist/schema-fields"; | ||
| import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env"; | ||
| import { StatusError } from "@stackframe/stack-shared/dist/utils/errors"; | ||
| import { escapeHtml } from "@stackframe/stack-shared/dist/utils/html"; | ||
| import { getFailedEmailsByTenancy } from "./crud"; | ||
|
|
||
| export const POST = createSmartRouteHandler({ | ||
| metadata: { | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| headers: yupObject({ | ||
| "authorization": yupTuple([yupString()]).defined(), | ||
| }), | ||
| method: yupString().oneOf(["POST"]).defined(), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200, 401]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| success: yupBoolean().defined(), | ||
| error_message: yupString().optional(), | ||
| failed_emails_by_tenancy: yupArray(yupObject({ | ||
| emails: yupArray(yupObject({ | ||
| subject: yupString().defined(), | ||
| to: yupArray(yupString().defined()).defined(), | ||
| })).defined(), | ||
| tenant_owner_email: yupString().defined(), | ||
| project_id: yupString().defined(), | ||
| tenancy_id: yupString().defined(), | ||
| })).optional(), | ||
| }).defined(), | ||
| }), | ||
| handler: async ({ headers }) => { | ||
| const authHeader = headers.authorization[0]; | ||
| if (authHeader !== `Bearer ${getEnvVariable('CRON_SECRET')}`) { | ||
| throw new StatusError(401, "Unauthorized"); | ||
| } | ||
|
|
||
| const failedEmailsByTenancy = await getFailedEmailsByTenancy(new Date(Date.now() - 1000 * 60 * 60 * 24)); | ||
| const internalTenancy = await getSoleTenancyFromProjectBranch("internal", DEFAULT_BRANCH_ID); | ||
| const emailConfig = await getSharedEmailConfig("Stack Auth"); | ||
| const dashboardUrl = getEnvVariable("NEXT_PUBLIC_STACK_DASHBOARD_URL", "https://app.stack-auth.com"); | ||
|
|
||
| for (const failedEmailsBatch of failedEmailsByTenancy.values()) { | ||
| const viewInStackAuth = `<a href="${dashboardUrl}/projects/${encodeURIComponent(failedEmailsBatch.projectId)}/emails">View all email logs on the Dashboard</a>`; | ||
| const emailHtml = ` | ||
| <p>Thank you for using Stack Auth!</p> | ||
| <p>We detected that, on your project, there have been ${failedEmailsBatch.emails.length} emails that failed to deliver in the last 24 hours. Please check your email server configuration.</p> | ||
| <p>${viewInStackAuth}</p> | ||
| <p>Last failing emails:</p> | ||
| ${failedEmailsBatch.emails.slice(-10).map((failedEmail) => { | ||
| const escapedSubject = escapeHtml(failedEmail.subject).replace(/\s+/g, ' ').slice(0, 50); | ||
| const escapedTo = failedEmail.to.map(to => escapeHtml(to)).join(", "); | ||
| return `<div><p>Subject: ${escapedSubject}<br />To: ${escapedTo}</p></div>`; | ||
| }).join("")} | ||
| ${failedEmailsBatch.emails.length > 10 ? `<div>...</div>` : ""} | ||
| `; | ||
| await sendEmail({ | ||
| tenancyId: internalTenancy.id, | ||
| emailConfig, | ||
| to: failedEmailsBatch.tenantOwnerEmail, | ||
| subject: "Failed emails digest", | ||
| html: emailHtml, | ||
| }); | ||
| } | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| bodyType: 'json', | ||
| body: { | ||
| success: true, | ||
| failed_emails_by_tenancy: Array.from(failedEmailsByTenancy.entries()).map(([tenancyId, batch]) => ( | ||
| { | ||
| emails: batch.emails, | ||
| tenant_owner_email: batch.tenantOwnerEmail, | ||
| project_id: batch.projectId, | ||
| tenancy_id: tenancyId, | ||
| } | ||
| ), | ||
| ) | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
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,8 @@ | ||
| { | ||
| "crons": [ | ||
| { | ||
| "path": "/api/latest/internal/failed-emails-digest", | ||
| "schedule": "0 0 * * *" | ||
| } | ||
| ] | ||
| } |
183 changes: 183 additions & 0 deletions
183
apps/e2e/tests/backend/endpoints/api/v1/internal/failed-emails-digest.test.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,183 @@ | ||
| import { describe } from "vitest"; | ||
| import { it } from "../../../../../helpers"; | ||
| import { Auth, backendContext, InternalProjectKeys, niceBackendFetch, Project } from "../../../../backend-helpers"; | ||
|
|
||
| describe("unauthorized requests", () => { | ||
| it("should return 401 when invalid authorization is provided", async ({ expect }) => { | ||
| const response = await niceBackendFetch( | ||
| "/api/v1/internal/failed-emails-digest", | ||
| { | ||
| method: "POST", | ||
| accessType: "server", | ||
| headers: { | ||
| "Authorization": "Bearer some_invalid_secret", | ||
| } | ||
| } | ||
| ); | ||
| expect(response).toMatchInlineSnapshot(` | ||
| NiceResponse { | ||
| "status": 401, | ||
| "body": "Unauthorized", | ||
| "headers": Headers { <some fields may have been hidden> }, | ||
| } | ||
| `); | ||
| }); | ||
|
|
||
| it("should return 400 when no authorization header is provided", async ({ expect }) => { | ||
| const response = await niceBackendFetch( | ||
| "/api/v1/internal/failed-emails-digest", | ||
| { | ||
| method: "POST", | ||
| accessType: "server", | ||
| } | ||
| ); | ||
| expect(response.status).toBe(400); | ||
| }); | ||
|
|
||
| it("should return 401 when authorization header is malformed", async ({ expect }) => { | ||
| const response = await niceBackendFetch( | ||
| "/api/v1/internal/failed-emails-digest", | ||
| { | ||
| method: "POST", | ||
| accessType: "server", | ||
| headers: { | ||
| "Authorization": "InvalidFormat", | ||
| } | ||
| } | ||
| ); | ||
| expect(response).toMatchInlineSnapshot(` | ||
| NiceResponse { | ||
| "status": 401, | ||
| "body": "Unauthorized", | ||
| "headers": Headers { <some fields may have been hidden> }, | ||
| } | ||
| `); | ||
| }); | ||
| }); | ||
|
|
||
| describe("with valid credentials", () => { | ||
|
BilalG1 marked this conversation as resolved.
|
||
| it("should return 200 and process failed emails digest", async ({ expect }) => { | ||
| backendContext.set({ | ||
| projectKeys: InternalProjectKeys, | ||
| userAuth: null, | ||
| }); | ||
| await Auth.Otp.signIn(); | ||
| const adminAccessToken = backendContext.value.userAuth?.accessToken; | ||
| const { projectId } = await Project.create({ | ||
| display_name: "Test Failed Emails Project", | ||
| config: { | ||
| email_config: { | ||
| type: "standard", | ||
| host: "invalid-smtp-host.example.com", | ||
| port: 587, | ||
| username: "invalid_user", | ||
| password: "invalid_password", | ||
| sender_name: "Test Project", | ||
| sender_email: "test@invalid-domain.example.com", | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| backendContext.set({ | ||
| projectKeys: { | ||
| projectId, | ||
| }, | ||
| userAuth: null, | ||
| }); | ||
|
|
||
| const testEmailResponse = await niceBackendFetch("/api/v1/internal/send-test-email", { | ||
| method: "POST", | ||
| accessType: "admin", | ||
| headers: { | ||
| "x-stack-admin-access-token": adminAccessToken, | ||
| }, | ||
| body: { | ||
| "recipient_email": "test-email-recipient@stackframe.co", | ||
| "email_config": { | ||
| "host": "this-is-not-a-valid-host.example.com", | ||
| "port": 123, | ||
| "username": "123", | ||
| "password": "123", | ||
| "sender_email": "123@g.co", | ||
| "sender_name": "123" | ||
| } | ||
| }, | ||
| }); | ||
| expect(testEmailResponse).toMatchInlineSnapshot(` | ||
| NiceResponse { | ||
| "status": 200, | ||
| "body": { | ||
| "error_message": "Failed to connect to the email host. Please make sure the email host configuration is correct.", | ||
| "success": false, | ||
| }, | ||
| "headers": Headers { <some fields may have been hidden> }, | ||
| } | ||
| `); | ||
|
|
||
| const response = await niceBackendFetch("/api/v1/internal/failed-emails-digest", { | ||
| method: "POST", | ||
| headers: { "Authorization": "Bearer mock_cron_secret" } | ||
| }); | ||
| expect(response.status).toBe(200); | ||
| console.log(response.body); | ||
|
|
||
| const failedEmailsByTenancy = response.body.failed_emails_by_tenancy; | ||
| const mockProjectFailedEmails = failedEmailsByTenancy.filter( | ||
| (batch: any) => batch.tenant_owner_email === backendContext.value.mailbox.emailAddress | ||
| ); | ||
|
N2D4 marked this conversation as resolved.
|
||
| expect(mockProjectFailedEmails).toMatchInlineSnapshot(` | ||
| [ | ||
| { | ||
| "emails": [ | ||
| { | ||
| "subject": "Test Email from Stack Auth", | ||
| "to": ["test-email-recipient@stackframe.co"], | ||
| }, | ||
| ], | ||
| "project_id": "<stripped UUID>", | ||
| "tenancy_id": "<stripped UUID>", | ||
| "tenant_owner_email": "default-mailbox--<stripped UUID>@stack-generated.example.com", | ||
| }, | ||
| ] | ||
| `); | ||
|
|
||
| const messages = await backendContext.value.mailbox.fetchMessages(); | ||
| const digestEmail = messages.find(msg => msg.subject === "Failed emails digest"); | ||
| expect(digestEmail).toBeDefined(); | ||
| expect(digestEmail!.from).toBe("Stack Auth <noreply@example.com>"); | ||
| }); | ||
|
|
||
| it("should return 200 and not send digest email when all emails are successful", async ({ expect }) => { | ||
| await Auth.Otp.signIn(); | ||
| const { projectId } = await Project.create({ | ||
| display_name: "Test Successful Emails Project", | ||
| config: { | ||
| email_config: { | ||
| type: "standard", | ||
| host: "localhost", | ||
| port: 2500, | ||
| username: "test", | ||
| password: "test", | ||
| sender_name: "Test Project", | ||
| sender_email: "test@example.com", | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const response = await niceBackendFetch("/api/v1/internal/failed-emails-digest", { | ||
| method: "POST", | ||
| headers: { "Authorization": "Bearer mock_cron_secret" } | ||
| }); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const failedEmailsByTenancy = response.body.failed_emails_by_tenancy; | ||
| const mockProjectFailedEmails = failedEmailsByTenancy.filter( | ||
| (batch: any) => batch.tenant_owner_email === backendContext.value.mailbox.emailAddress | ||
| ); | ||
| expect(mockProjectFailedEmails).toMatchInlineSnapshot(`[]`); | ||
|
|
||
| const messages = await backendContext.value.mailbox.fetchMessages(); | ||
| const digestEmail = messages.find(msg => msg.subject === "Failed emails digest"); | ||
| expect(digestEmail).toBeUndefined(); | ||
| }); | ||
|
N2D4 marked this conversation as resolved.
|
||
| }); | ||
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.