-
-
Notifications
You must be signed in to change notification settings - Fork 367
feat: automate domain verification follow-ups #375
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 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fba4599
feat: automate domain verification follow-ups
KMKoushik 5d5d50e
fix: harden domain verification notifications
KMKoushik e82d2b5
fix: skip unchanged first-run domain status emails
KMKoushik 9a71973
fix: make domain cleanup resilient and status labels readable
KMKoushik 836dd67
fix: clarify domain verification notification messaging
KMKoushik 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
122 changes: 122 additions & 0 deletions
122
apps/web/src/server/email-templates/DomainVerificationStatusEmail.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,122 @@ | ||
| import React from "react"; | ||
| import { Container, Text } from "jsx-email"; | ||
| import { render } from "jsx-email"; | ||
| import { DomainStatus } from "@prisma/client"; | ||
| import { EmailButton } from "~/server/email-templates/components/EmailButton"; | ||
| import { EmailFooter } from "~/server/email-templates/components/EmailFooter"; | ||
| import { EmailHeader } from "~/server/email-templates/components/EmailHeader"; | ||
| import { EmailLayout } from "~/server/email-templates/components/EmailLayout"; | ||
|
|
||
| interface DomainVerificationStatusEmailProps { | ||
| domainName: string; | ||
| currentStatus: DomainStatus; | ||
| previousStatus: DomainStatus; | ||
| verificationError?: string | null; | ||
| domainUrl: string; | ||
| } | ||
|
|
||
| function getTitle(currentStatus: DomainStatus, previousStatus: DomainStatus) { | ||
| if (currentStatus === DomainStatus.SUCCESS) { | ||
| return previousStatus === DomainStatus.SUCCESS | ||
| ? "Domain verification checked" | ||
| : "Your domain is verified"; | ||
| } | ||
|
|
||
| if (previousStatus === DomainStatus.SUCCESS) { | ||
| return "Your domain status changed"; | ||
| } | ||
|
|
||
| return "Your domain verification needs attention"; | ||
| } | ||
|
|
||
| export function DomainVerificationStatusEmail({ | ||
| domainName, | ||
| currentStatus, | ||
| previousStatus, | ||
| verificationError, | ||
| domainUrl, | ||
| }: DomainVerificationStatusEmailProps) { | ||
| const preview = `${domainName} is now ${currentStatus.toLowerCase().replaceAll("_", " ")}`; | ||
|
|
||
| return ( | ||
| <EmailLayout preview={preview}> | ||
| <EmailHeader title={getTitle(currentStatus, previousStatus)} /> | ||
|
|
||
| <Container style={{ padding: "20px 0", textAlign: "left" as const }}> | ||
| <Text | ||
| style={{ | ||
| fontSize: "16px", | ||
| color: "#374151", | ||
| margin: "0 0 16px 0", | ||
| lineHeight: "1.6", | ||
| textAlign: "left" as const, | ||
| }} | ||
| > | ||
| {domainName} is currently <strong>{currentStatus}</strong>. | ||
| </Text> | ||
|
|
||
| {previousStatus !== currentStatus ? ( | ||
| <Text | ||
| style={{ | ||
| fontSize: "15px", | ||
| color: "#4b5563", | ||
| margin: "0 0 16px 0", | ||
| lineHeight: "1.6", | ||
| textAlign: "left" as const, | ||
| }} | ||
| > | ||
| Previous status: <strong>{previousStatus}</strong> | ||
| </Text> | ||
| ) : null} | ||
|
|
||
| {verificationError ? ( | ||
| <Container | ||
| style={{ | ||
| backgroundColor: "#fef2f2", | ||
| border: "1px solid #fecaca", | ||
| padding: "12px 16px", | ||
| margin: "0 0 24px 0", | ||
| borderRadius: "4px", | ||
| }} | ||
| > | ||
| <Text | ||
| style={{ | ||
| margin: 0, | ||
| color: "#991b1b", | ||
| fontSize: 14, | ||
| textAlign: "left" as const, | ||
| }} | ||
| > | ||
| Verification error: {verificationError} | ||
| </Text> | ||
| </Container> | ||
| ) : null} | ||
|
|
||
| <Text | ||
| style={{ | ||
| fontSize: "14px", | ||
| color: "#6b7280", | ||
| margin: "0 0 24px 0", | ||
| lineHeight: "1.6", | ||
| textAlign: "left" as const, | ||
| }} | ||
| > | ||
| Review the DNS records in useSend to make sure the domain stays ready | ||
| to send. | ||
| </Text> | ||
|
|
||
| <Container style={{ margin: "0 0 32px 0", textAlign: "left" as const }}> | ||
| <EmailButton href={domainUrl}>Open domain settings</EmailButton> | ||
| </Container> | ||
| </Container> | ||
|
|
||
| <EmailFooter /> | ||
| </EmailLayout> | ||
| ); | ||
| } | ||
|
|
||
| export async function renderDomainVerificationStatusEmail( | ||
| props: DomainVerificationStatusEmailProps, | ||
| ): Promise<string> { | ||
| return render(<DomainVerificationStatusEmail {...props} />); | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { Queue, Worker } from "bullmq"; | ||
| import { db } from "~/server/db"; | ||
| import { logger } from "~/server/logger/log"; | ||
| import { getRedis, BULL_PREFIX } from "~/server/redis"; | ||
| import { | ||
| DOMAIN_VERIFICATION_QUEUE, | ||
| DEFAULT_QUEUE_OPTIONS, | ||
| } from "~/server/queue/queue-constants"; | ||
| import { | ||
| isDomainVerificationDue, | ||
| refreshDomainVerification, | ||
| } from "~/server/service/domain-service"; | ||
|
|
||
| let initialized = false; | ||
|
|
||
| export async function runDueDomainVerifications() { | ||
| const domains = await db.domain.findMany({ | ||
| orderBy: { | ||
| createdAt: "asc", | ||
| }, | ||
| }); | ||
|
|
||
| for (const domain of domains) { | ||
| try { | ||
| const isDue = await isDomainVerificationDue(domain); | ||
| if (!isDue) { | ||
| continue; | ||
| } | ||
|
|
||
| await refreshDomainVerification(domain); | ||
| } catch (error) { | ||
| logger.error( | ||
| { err: error, domainId: domain.id }, | ||
| "[DomainVerificationJob]: Failed to refresh domain verification", | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function initDomainVerificationJob() { | ||
| if (initialized) { | ||
| return; | ||
| } | ||
|
|
||
| const connection = getRedis(); | ||
| const domainVerificationQueue = new Queue(DOMAIN_VERIFICATION_QUEUE, { | ||
| connection, | ||
| prefix: BULL_PREFIX, | ||
| skipVersionCheck: true, | ||
| }); | ||
|
|
||
| const worker = new Worker( | ||
| DOMAIN_VERIFICATION_QUEUE, | ||
| async () => { | ||
| await runDueDomainVerifications(); | ||
| }, | ||
| { | ||
| connection, | ||
| concurrency: 1, | ||
| prefix: BULL_PREFIX, | ||
| skipVersionCheck: true, | ||
| }, | ||
| ); | ||
|
|
||
| await domainVerificationQueue.upsertJobScheduler( | ||
| "domain-verification-hourly", | ||
| { | ||
| pattern: "0 * * * *", | ||
| tz: "UTC", | ||
| }, | ||
| { | ||
| opts: { | ||
| ...DEFAULT_QUEUE_OPTIONS, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| worker.on("completed", (job) => { | ||
| logger.info({ jobId: job.id }, "[DomainVerificationJob]: Job completed"); | ||
| }); | ||
|
|
||
| worker.on("failed", (job, err) => { | ||
| logger.error( | ||
| { err, jobId: job?.id }, | ||
| "[DomainVerificationJob]: Job failed", | ||
| ); | ||
| }); | ||
|
|
||
| initialized = true; | ||
| } |
123 changes: 123 additions & 0 deletions
123
apps/web/src/server/jobs/domain-verification-job.unit.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,123 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { DomainStatus, type Domain } from "@prisma/client"; | ||
|
|
||
| const { | ||
| mockFindMany, | ||
| mockIsDomainVerificationDue, | ||
| mockRefreshDomainVerification, | ||
| mockUpsertJobScheduler, | ||
| mockWorkerOn, | ||
| mockQueue, | ||
| mockWorker, | ||
| } = vi.hoisted(() => ({ | ||
| mockFindMany: vi.fn(), | ||
| mockIsDomainVerificationDue: vi.fn(), | ||
| mockRefreshDomainVerification: vi.fn(), | ||
| mockUpsertJobScheduler: vi.fn(), | ||
| mockWorkerOn: vi.fn(), | ||
| mockQueue: vi.fn().mockImplementation(() => ({ | ||
| upsertJobScheduler: mockUpsertJobScheduler, | ||
| })), | ||
| mockWorker: vi.fn().mockImplementation(() => ({ | ||
| on: mockWorkerOn, | ||
| })), | ||
| })); | ||
|
|
||
| vi.mock("bullmq", () => ({ | ||
| Queue: mockQueue, | ||
| Worker: mockWorker, | ||
| })); | ||
|
|
||
| vi.mock("~/server/db", () => ({ | ||
| db: { | ||
| domain: { | ||
| findMany: mockFindMany, | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("~/server/redis", () => ({ | ||
| BULL_PREFIX: "bull", | ||
| getRedis: vi.fn(() => ({})), | ||
| })); | ||
|
|
||
| vi.mock("~/server/logger/log", () => ({ | ||
| logger: { | ||
| error: vi.fn(), | ||
| info: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("~/server/service/domain-service", () => ({ | ||
| isDomainVerificationDue: mockIsDomainVerificationDue, | ||
| refreshDomainVerification: mockRefreshDomainVerification, | ||
| })); | ||
|
|
||
| import { | ||
| initDomainVerificationJob, | ||
| runDueDomainVerifications, | ||
| } from "~/server/jobs/domain-verification-job"; | ||
|
|
||
| function createDomain(id: number, status: DomainStatus): Domain { | ||
| return { | ||
| id, | ||
| name: `example-${id}.com`, | ||
| teamId: 7, | ||
| status, | ||
| region: "us-east-1", | ||
| clickTracking: false, | ||
| openTracking: false, | ||
| publicKey: "public-key", | ||
| dkimSelector: "usesend", | ||
| dkimStatus: DomainStatus.NOT_STARTED, | ||
| spfDetails: DomainStatus.NOT_STARTED, | ||
| dmarcAdded: false, | ||
| errorMessage: null, | ||
| subdomain: null, | ||
| sesTenantId: null, | ||
| isVerifying: status !== DomainStatus.SUCCESS, | ||
| createdAt: new Date("2026-03-01T00:00:00.000Z"), | ||
| updatedAt: new Date("2026-03-01T00:00:00.000Z"), | ||
| }; | ||
| } | ||
|
|
||
| describe("domain-verification-job", () => { | ||
| beforeEach(() => { | ||
| mockFindMany.mockReset(); | ||
| mockIsDomainVerificationDue.mockReset(); | ||
| mockRefreshDomainVerification.mockReset(); | ||
| mockUpsertJobScheduler.mockReset(); | ||
| mockWorkerOn.mockReset(); | ||
| mockQueue.mockReset(); | ||
| mockWorker.mockReset(); | ||
| mockQueue.mockImplementation(() => ({ | ||
| upsertJobScheduler: mockUpsertJobScheduler, | ||
| })); | ||
| mockWorker.mockImplementation(() => ({ | ||
| on: mockWorkerOn, | ||
| })); | ||
| }); | ||
|
|
||
| it("refreshes only domains that are due", async () => { | ||
| const firstDomain = createDomain(1, DomainStatus.PENDING); | ||
| const secondDomain = createDomain(2, DomainStatus.SUCCESS); | ||
| mockFindMany.mockResolvedValue([firstDomain, secondDomain]); | ||
| mockIsDomainVerificationDue | ||
| .mockResolvedValueOnce(true) | ||
| .mockResolvedValueOnce(false); | ||
|
|
||
| await runDueDomainVerifications(); | ||
|
|
||
| expect(mockRefreshDomainVerification).toHaveBeenCalledTimes(1); | ||
| expect(mockRefreshDomainVerification).toHaveBeenCalledWith(firstDomain); | ||
| }); | ||
|
|
||
| it("initializes the worker lazily", async () => { | ||
| await initDomainVerificationJob(); | ||
|
|
||
| expect(mockQueue).toHaveBeenCalledTimes(1); | ||
| expect(mockWorker).toHaveBeenCalledTimes(1); | ||
| expect(mockUpsertJobScheduler).toHaveBeenCalledTimes(1); | ||
| expect(mockWorkerOn).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.