feat(marketing-pages): sendWelcomeEmail job + handler enqueues it

sendWelcomeEmailJob takes IMailerService, validates the typed input
(userId + email), and delegates to mailer.sendWelcome. The
onAuthUserSignedUpHandler now takes IJobQueue and enqueues
"marketing-pages.send-welcome-email" with the event payload.

Both binders bind a RecordingMailerService at the IMailerService
symbol (production placeholder until a real adapter ships) and pass
mailer / queue into the wrapped factories. Dev-seed additionally
queue.register()s the slug so the in-memory queue dispatches via
the wrapped job; production relies on the generated Payload event-task
to resolve the wrapped handler from the container.
This commit is contained in:
2026-05-08 16:35:45 +02:00
parent 6b57a34c0c
commit 7e844c646d
9 changed files with 177 additions and 22 deletions

View File

@@ -19,6 +19,9 @@ import type { IPagesRepository } from "../application/repositories/pages.reposit
import type { ISiteSettingsRepository } from "../application/repositories/site-settings.repository.interface.js";
import { userSignedUpEvent } from "@repo/auth";
import { onAuthUserSignedUpHandler } from "../events/handlers/on-auth-user-signed-up.handler.js";
import { sendWelcomeEmailJob, type ISendWelcomeEmailJob, type SendWelcomeEmailInput } from "../jobs/send-welcome-email.job.js";
import { RecordingMailerService } from "../infrastructure/services/recording-mailer.service.js";
import type { IMailerService } from "../application/services/mailer.service.interface.js";
/**
* Replace the default empty mocks with populated ones for dev mode + storybook.
@@ -129,10 +132,15 @@ export async function bindDevSeedMarketingPages(
),
),
);
// bus + queue are accept-and-forward in Phase 6; consumed by Phase 7 generator
// output at the <gen:event-handlers> / <gen:jobs> anchors below.
void bus;
void queue;
// Bind the dev-seed mailer (recording so e2e tests can inspect calls).
if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IMailerService)) {
marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.IMailerService);
}
const mailer = new RecordingMailerService();
marketingPagesContainer
.bind<IMailerService>(MARKETING_PAGES_SYMBOLS.IMailerService)
.toConstantValue(mailer);
// <gen:event-handlers>
// onAuthUserSignedUpHandler subscription — generated, edit the handler file (not this block) for behavior.
const wrappedAuthUserSignedUp = withSpan(
@@ -145,7 +153,7 @@ export async function bindDevSeedMarketingPages(
layer: "event-handler",
name: "marketing-pages.onAuthUserSignedUpHandler",
},
onAuthUserSignedUpHandler(),
onAuthUserSignedUpHandler(queue),
),
);
if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IOnAuthUserSignedUpHandler)) {
@@ -154,4 +162,35 @@ export async function bindDevSeedMarketingPages(
marketingPagesContainer.bind(MARKETING_PAGES_SYMBOLS.IOnAuthUserSignedUpHandler).toConstantValue(wrappedAuthUserSignedUp);
bus.subscribe(userSignedUpEvent, "marketing-pages", wrappedAuthUserSignedUp);
// <gen:jobs>
const wrappedSendWelcomeEmail = withSpan(
tracer,
{ name: "marketing-pages.sendWelcomeEmail", op: "job" },
withCapture(
logger,
{
feature: "marketing-pages",
layer: "job",
name: "marketing-pages.sendWelcomeEmail",
},
sendWelcomeEmailJob(mailer),
),
);
if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob)) {
marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob);
}
marketingPagesContainer.bind(MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob).toConstantValue(wrappedSendWelcomeEmail);
// Register the dev-seed in-memory queue handler so enqueue() actually fires
// the wrapped job. Production binding skips this — the Payload task does it.
if ("register" in queue && typeof (queue as { register?: unknown }).register === "function") {
(queue as { register: (slug: string, h: (input: unknown) => Promise<void>) => void }).register(
"marketing-pages.send-welcome-email",
async (input) => {
const wrapped = marketingPagesContainer.get<ISendWelcomeEmailJob>(
MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob,
);
await wrapped(input as SendWelcomeEmailInput);
},
);
}
}

View File

@@ -18,6 +18,9 @@ import { getSiteSettingsController } from "../interface-adapters/controllers/get
import { getPageBySlugController } from "../interface-adapters/controllers/get-page-by-slug.controller";
import { userSignedUpEvent } from "@repo/auth";
import { onAuthUserSignedUpHandler } from "../events/handlers/on-auth-user-signed-up.handler";
import { sendWelcomeEmailJob } from "../jobs/send-welcome-email.job";
import { RecordingMailerService } from "../infrastructure/services/recording-mailer.service";
import type { IMailerService } from "../application/services/mailer.service.interface";
export function bindProductionMarketingPages(
config: SanitizedConfig,
@@ -119,10 +122,17 @@ export function bindProductionMarketingPages(
),
),
);
// bus + queue are accept-and-forward in Phase 6; consumed by Phase 7 generator
// output at the <gen:event-handlers> / <gen:jobs> anchors below.
void bus;
void queue;
// Bind the production mailer. Until a real SMTP/SES adapter ships, this is
// a placeholder RecordingMailerService — production wiring is structurally
// identical to dev-seed but the impl is the seam where a real mailer plugs in.
if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IMailerService)) {
marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.IMailerService);
}
const mailer = new RecordingMailerService();
marketingPagesContainer
.bind<IMailerService>(MARKETING_PAGES_SYMBOLS.IMailerService)
.toConstantValue(mailer);
// <gen:event-handlers>
// onAuthUserSignedUpHandler subscription — generated, edit the handler file (not this block) for behavior.
const wrappedAuthUserSignedUp = withSpan(
@@ -135,7 +145,7 @@ export function bindProductionMarketingPages(
layer: "event-handler",
name: "marketing-pages.onAuthUserSignedUpHandler",
},
onAuthUserSignedUpHandler(),
onAuthUserSignedUpHandler(queue),
),
);
if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.IOnAuthUserSignedUpHandler)) {
@@ -144,4 +154,21 @@ export function bindProductionMarketingPages(
marketingPagesContainer.bind(MARKETING_PAGES_SYMBOLS.IOnAuthUserSignedUpHandler).toConstantValue(wrappedAuthUserSignedUp);
bus.subscribe(userSignedUpEvent, "marketing-pages", wrappedAuthUserSignedUp);
// <gen:jobs>
const wrappedSendWelcomeEmail = withSpan(
tracer,
{ name: "marketing-pages.sendWelcomeEmail", op: "job" },
withCapture(
logger,
{
feature: "marketing-pages",
layer: "job",
name: "marketing-pages.sendWelcomeEmail",
},
sendWelcomeEmailJob(mailer),
),
);
if (marketingPagesContainer.isBound(MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob)) {
marketingPagesContainer.unbind(MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob);
}
marketingPagesContainer.bind(MARKETING_PAGES_SYMBOLS.ISendWelcomeEmailJob).toConstantValue(wrappedSendWelcomeEmail);
}

View File

@@ -11,4 +11,5 @@ export const MARKETING_PAGES_SYMBOLS = {
// <gen:event-handler-symbols>
IOnAuthUserSignedUpHandler: Symbol.for("@repo/marketing-pages/onAuthUserSignedUp"),
// <gen:job-symbols>
ISendWelcomeEmailJob: Symbol.for("@repo/marketing-pages/sendWelcomeEmailJob"),
} as const;