Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+52
@@ -0,0 +1,52 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseMailjetSuppressionSignal } from "./mailjet-suppression.js";
|
||||
|
||||
const tenantPayload = JSON.stringify({ t: "tenant-1" });
|
||||
|
||||
describe("parseMailjetSuppressionSignal", () => {
|
||||
it("suppresses a hard bounce with a JSON tenant payload", () => {
|
||||
const body = JSON.stringify({ event: "bounce", email: "Hard@Bounce.test", Payload: tenantPayload, error: "user unknown" });
|
||||
expect(parseMailjetSuppressionSignal(body)).toEqual({
|
||||
action: "suppress",
|
||||
tenantId: "tenant-1",
|
||||
emailAddress: "Hard@Bounce.test",
|
||||
suppressionReason: "bounce",
|
||||
detail: "bounce: user unknown",
|
||||
});
|
||||
});
|
||||
|
||||
it("classifies a spam event as a complaint", () => {
|
||||
const body = JSON.stringify({ event: "spam", email: "a@x.test", CustomID: "tenant-9" });
|
||||
expect(parseMailjetSuppressionSignal(body)).toMatchObject({ action: "suppress", tenantId: "tenant-9", suppressionReason: "complaint" });
|
||||
});
|
||||
|
||||
it("accepts a bare tenant id in the payload", () => {
|
||||
const body = JSON.stringify({ event: "blocked", email: "a@x.test", Payload: "tenant-7" });
|
||||
expect(parseMailjetSuppressionSignal(body)).toMatchObject({ action: "suppress", tenantId: "tenant-7", suppressionReason: "bounce" });
|
||||
});
|
||||
|
||||
it("folds a batched array to its first event", () => {
|
||||
const body = JSON.stringify([{ event: "bounce", email: "a@x.test", Payload: tenantPayload }]);
|
||||
expect(parseMailjetSuppressionSignal(body)).toMatchObject({ action: "suppress", tenantId: "tenant-1" });
|
||||
});
|
||||
|
||||
it("ignores non-suppression events", () => {
|
||||
expect(parseMailjetSuppressionSignal(JSON.stringify({ event: "open", email: "a@x.test", Payload: tenantPayload }))).toEqual({
|
||||
action: "ignore",
|
||||
reason: "non-suppression-event:open",
|
||||
});
|
||||
});
|
||||
|
||||
it("ignores a bounce whose tenant cannot be resolved", () => {
|
||||
expect(parseMailjetSuppressionSignal(JSON.stringify({ event: "bounce", email: "a@x.test" }))).toEqual({ action: "ignore", reason: "unresolved-tenant" });
|
||||
});
|
||||
|
||||
it("ignores a bounce with no recipient", () => {
|
||||
expect(parseMailjetSuppressionSignal(JSON.stringify({ event: "bounce", Payload: tenantPayload }))).toEqual({ action: "ignore", reason: "missing-email" });
|
||||
});
|
||||
|
||||
it("ignores invalid JSON", () => {
|
||||
expect(parseMailjetSuppressionSignal("not json")).toEqual({ action: "ignore", reason: "invalid-json" });
|
||||
});
|
||||
});
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
// AK-P14a · P14 Email — Mailjet event → suppression signal parser (pure).
|
||||
//
|
||||
// Independent of the email-sequence bounce reconciliation (provider-webhook-route):
|
||||
// that path keys off `providerMessageId` to fail a sequence run; THIS extracts the
|
||||
// recipient address + the owning tenant so the address can be added to the tenant
|
||||
// suppression list. The tenant is carried in Mailjet's custom `Payload` / `CustomID`
|
||||
// field, which the AK-P14b send path stamps as JSON `{ "t": "<tenantId>" }` (or a
|
||||
// bare tenant id). When the tenant cannot be resolved the signal is `ignore` —
|
||||
// suppression is best-effort and must never break the webhook.
|
||||
//
|
||||
// Kept pure + separately unit-tested so it adds zero risk to the existing
|
||||
// sequence-webhook tests.
|
||||
|
||||
export type MailjetSuppressionSignal =
|
||||
| { action: "ignore"; reason: string }
|
||||
| { action: "suppress"; tenantId: string; emailAddress: string; suppressionReason: "bounce" | "complaint"; detail: string | null };
|
||||
|
||||
// Mailjet `event` values that warrant a permanent suppression. `spam` is a
|
||||
// complaint; hard `bounce` / `blocked` are delivery failures.
|
||||
const SUPPRESSION_EVENT_TYPES = new Set(["bounce", "blocked", "spam"]);
|
||||
|
||||
export function parseMailjetSuppressionSignal(rawBody: string): MailjetSuppressionSignal {
|
||||
let payload: unknown;
|
||||
try {
|
||||
payload = JSON.parse(rawBody) as unknown;
|
||||
} catch {
|
||||
return { action: "ignore", reason: "invalid-json" };
|
||||
}
|
||||
if (Array.isArray(payload)) {
|
||||
payload = payload[0];
|
||||
}
|
||||
if (!isRecord(payload)) {
|
||||
return { action: "ignore", reason: "not-an-object" };
|
||||
}
|
||||
|
||||
const eventType = normalizeEventType(readString(payload, [["event"], ["type"], ["event_type"], ["data", "event"]]));
|
||||
if (!eventType || !SUPPRESSION_EVENT_TYPES.has(eventType)) {
|
||||
return { action: "ignore", reason: `non-suppression-event:${eventType ?? "unknown"}` };
|
||||
}
|
||||
|
||||
const emailAddress = readString(payload, [["email"], ["Email"], ["recipient"], ["data", "email"], ["data", "recipient"]]);
|
||||
if (!emailAddress) {
|
||||
return { action: "ignore", reason: "missing-email" };
|
||||
}
|
||||
|
||||
const tenantId = resolveTenantId(payload);
|
||||
if (!tenantId) {
|
||||
return { action: "ignore", reason: "unresolved-tenant" };
|
||||
}
|
||||
|
||||
const detail = readString(payload, [["error"], ["comment"], ["reason"], ["error_related_to"], ["data", "error"]]) ?? null;
|
||||
return {
|
||||
action: "suppress",
|
||||
tenantId,
|
||||
emailAddress,
|
||||
suppressionReason: eventType === "spam" ? "complaint" : "bounce",
|
||||
detail: detail ? `${eventType}: ${detail}` : eventType,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the owning tenant from Mailjet's custom send-time metadata. Accepts a
|
||||
* JSON object `{ "t": "<tenantId>" }` / `{ "tenantId": "<id>" }` or a bare string
|
||||
* in `Payload` / `CustomID`.
|
||||
*/
|
||||
function resolveTenantId(payload: Record<string, unknown>): string | null {
|
||||
const raw = readString(payload, [["Payload"], ["payload"], ["CustomID"], ["customId"], ["custom_id"], ["data", "Payload"], ["data", "CustomID"]]);
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as unknown;
|
||||
if (isRecord(parsed)) {
|
||||
const fromJson = firstString([parsed["t"], parsed["tenantId"], parsed["tenant_id"]]);
|
||||
if (fromJson) {
|
||||
return fromJson;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// not JSON — fall through to treating the whole value as a bare tenant id.
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
function firstString(values: unknown[]): string | null {
|
||||
for (const value of values) {
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
return value.trim();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function readString(record: Record<string, unknown>, paths: string[][]): string | undefined {
|
||||
for (const path of paths) {
|
||||
let current: unknown = record;
|
||||
for (const key of path) {
|
||||
if (!isRecord(current)) {
|
||||
current = undefined;
|
||||
break;
|
||||
}
|
||||
current = current[key];
|
||||
}
|
||||
if (typeof current === "string" && current.trim()) {
|
||||
return current.trim();
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function normalizeEventType(value: string | undefined): string | undefined {
|
||||
return value?.trim().toLowerCase().replace(/[\s-]+/g, "_");
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
Reference in new issue
Block a user