120 lines
4.7 KiB
TypeScript
120 lines
4.7 KiB
TypeScript
/**
|
|
* Bridges form-submission webhook outbox events onto the existing
|
|
* `hivegrid.email` webhook delivery processor.
|
|
*/
|
|
|
|
import {
|
|
buildWebhookDeliveryJob,
|
|
type QueueEnqueueOptions,
|
|
type WebhookDeliveryJobDescriptor,
|
|
} from "@hivegrid/queue";
|
|
|
|
export const FORM_SUBMISSION_WEBHOOK_OUTBOX_QUEUE = "outbox.webhook" as const;
|
|
|
|
export interface FormSubmissionWebhookDeliveryJobData {
|
|
readonly outboxEventId?: string;
|
|
readonly tenantId: string;
|
|
readonly eventType: "webhook.form-submission.deliver";
|
|
readonly dispatchKey?: string | null;
|
|
readonly payload: {
|
|
readonly submissionId: string;
|
|
readonly formId: string;
|
|
readonly tenantId: string;
|
|
readonly webhookUrl: string;
|
|
readonly targetType?: string;
|
|
readonly targetId?: string | null;
|
|
readonly values?: Record<string, unknown>;
|
|
};
|
|
}
|
|
|
|
export interface FormSubmissionWebhookDeliveryDeps {
|
|
readonly enqueueWebhook: (
|
|
descriptor: WebhookDeliveryJobDescriptor,
|
|
options: QueueEnqueueOptions,
|
|
) => Promise<unknown>;
|
|
}
|
|
|
|
export class FormSubmissionWebhookDeliveryJobError extends Error {
|
|
override readonly name = "FormSubmissionWebhookDeliveryJobError";
|
|
readonly code: "INVALID_PAYLOAD" | "INVALID_EVENT_TYPE" | "TENANT_MISMATCH";
|
|
|
|
constructor(message: string, code: FormSubmissionWebhookDeliveryJobError["code"]) {
|
|
super(message);
|
|
this.code = code;
|
|
}
|
|
}
|
|
|
|
export function parseFormSubmissionWebhookDeliveryJob(data: unknown): FormSubmissionWebhookDeliveryJobData {
|
|
if (!data || typeof data !== "object") {
|
|
throw new FormSubmissionWebhookDeliveryJobError("form-submission webhook job missing envelope", "INVALID_PAYLOAD");
|
|
}
|
|
const envelope = data as Record<string, unknown>;
|
|
if (envelope.eventType !== "webhook.form-submission.deliver") {
|
|
throw new FormSubmissionWebhookDeliveryJobError(
|
|
`form-submission webhook rejected eventType ${JSON.stringify(envelope.eventType)}`,
|
|
"INVALID_EVENT_TYPE",
|
|
);
|
|
}
|
|
const tenantId = typeof envelope.tenantId === "string" ? envelope.tenantId : "";
|
|
if (tenantId.length === 0) {
|
|
throw new FormSubmissionWebhookDeliveryJobError("form-submission webhook job missing tenantId", "INVALID_PAYLOAD");
|
|
}
|
|
const rawPayload = envelope.payload;
|
|
if (!rawPayload || typeof rawPayload !== "object") {
|
|
throw new FormSubmissionWebhookDeliveryJobError("form-submission webhook job missing payload", "INVALID_PAYLOAD");
|
|
}
|
|
const payload = rawPayload as Record<string, unknown>;
|
|
const payloadTenantId = typeof payload.tenantId === "string" ? payload.tenantId : "";
|
|
if (payloadTenantId !== tenantId) {
|
|
throw new FormSubmissionWebhookDeliveryJobError("form-submission webhook tenantId mismatch", "TENANT_MISMATCH");
|
|
}
|
|
const submissionId = typeof payload.submissionId === "string" ? payload.submissionId : "";
|
|
const formId = typeof payload.formId === "string" ? payload.formId : "";
|
|
const webhookUrl = typeof payload.webhookUrl === "string" ? payload.webhookUrl.trim() : "";
|
|
if (!submissionId || !formId || !webhookUrl) {
|
|
throw new FormSubmissionWebhookDeliveryJobError("form-submission webhook payload missing required fields", "INVALID_PAYLOAD");
|
|
}
|
|
|
|
return {
|
|
outboxEventId: typeof envelope.outboxEventId === "string" ? envelope.outboxEventId : undefined,
|
|
tenantId,
|
|
eventType: "webhook.form-submission.deliver",
|
|
dispatchKey: typeof envelope.dispatchKey === "string" ? envelope.dispatchKey : null,
|
|
payload: {
|
|
submissionId,
|
|
formId,
|
|
tenantId: payloadTenantId,
|
|
webhookUrl,
|
|
targetType: typeof payload.targetType === "string" ? payload.targetType : undefined,
|
|
targetId: typeof payload.targetId === "string" ? payload.targetId : null,
|
|
values: payload.values && typeof payload.values === "object" ? (payload.values as Record<string, unknown>) : {},
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function runFormSubmissionWebhookDeliveryJob(
|
|
data: unknown,
|
|
deps: FormSubmissionWebhookDeliveryDeps,
|
|
): Promise<{ enqueued: true; queueName: WebhookDeliveryJobDescriptor["queueName"]; jobName: WebhookDeliveryJobDescriptor["jobName"] }> {
|
|
const job = parseFormSubmissionWebhookDeliveryJob(data);
|
|
const descriptor = buildWebhookDeliveryJob({
|
|
url: job.payload.webhookUrl,
|
|
method: "POST",
|
|
eventName: "form.submission",
|
|
tenantId: job.tenantId,
|
|
body: job.payload,
|
|
metadata: {
|
|
source: "form-submission",
|
|
...(job.outboxEventId ? { outboxEventId: job.outboxEventId } : {}),
|
|
...(job.dispatchKey ? { dispatchKey: job.dispatchKey } : {}),
|
|
},
|
|
});
|
|
|
|
await deps.enqueueWebhook(descriptor, {
|
|
tenantId: job.tenantId,
|
|
idempotencyKey: job.outboxEventId ?? job.dispatchKey ?? `form-submission-webhook:${job.payload.submissionId}`,
|
|
});
|
|
|
|
return { enqueued: true, queueName: descriptor.queueName, jobName: descriptor.jobName };
|
|
}
|