125 lines
4.4 KiB
TypeScript
125 lines
4.4 KiB
TypeScript
import type { Prisma } from "../generated/prisma/client/client.js";
|
|
|
|
import { getDbClient } from "./client.server.js";
|
|
import { publishOutboxEvent, type OutboxPublisherTransaction } from "./outbox.js";
|
|
|
|
export type CampaignBounceKind = "hart" | "weich";
|
|
|
|
export type MarkCampaignOutboundEmailBouncedInput = {
|
|
tenantId: string;
|
|
outboundEmailId: string;
|
|
bouncedAt: Date;
|
|
bounceKind: CampaignBounceKind;
|
|
providerEventType: string;
|
|
};
|
|
|
|
export type CampaignOutboundEmailBounceResult =
|
|
| { status: "bounced"; outboundEmailId: string; campaignId: string }
|
|
| { status: "already_bounced"; outboundEmailId: string; campaignId: string }
|
|
| { status: "unknown_delivery" };
|
|
|
|
type CampaignBounceRow = {
|
|
id: string;
|
|
bouncedAt: Date | null;
|
|
campaignId: string | null;
|
|
campaign: { id: string; name: string } | null;
|
|
};
|
|
|
|
export type CampaignBounceTransactionClient = {
|
|
outboundEmail: {
|
|
findFirst(args: {
|
|
where: { id: string; tenantId: string; campaignId: { not: null } };
|
|
select: {
|
|
id: true;
|
|
bouncedAt: true;
|
|
campaignId: true;
|
|
campaign: { select: { id: true; name: true } };
|
|
};
|
|
}): Promise<CampaignBounceRow | null>;
|
|
updateMany(args: { where: { id: string; tenantId: string; bouncedAt: null }; data: { bouncedAt: Date } }): Promise<{ count: number }>;
|
|
};
|
|
outboxEvent: OutboxPublisherTransaction["outboxEvent"];
|
|
};
|
|
|
|
export type CampaignBounceClient = CampaignBounceTransactionClient & {
|
|
$transaction<T>(runner: (tx: CampaignBounceTransactionClient) => Promise<T>): Promise<T>;
|
|
};
|
|
|
|
/**
|
|
* Reconciles a Mailjet campaign bounce and emits the matching automation event
|
|
* in the same transaction. The payload contains stable ids only, never the
|
|
* recipient address or the provider message id.
|
|
*/
|
|
export function markCampaignOutboundEmailBounced(
|
|
input: MarkCampaignOutboundEmailBouncedInput,
|
|
client: CampaignBounceClient = getDbClient() as unknown as CampaignBounceClient
|
|
): Promise<CampaignOutboundEmailBounceResult> {
|
|
return client.$transaction(async (tx) => {
|
|
const outbound = await tx.outboundEmail.findFirst({
|
|
where: { id: input.outboundEmailId, tenantId: input.tenantId, campaignId: { not: null } },
|
|
select: {
|
|
id: true,
|
|
bouncedAt: true,
|
|
campaignId: true,
|
|
campaign: { select: { id: true, name: true } },
|
|
},
|
|
});
|
|
if (!outbound?.campaignId || !outbound.campaign) {
|
|
return { status: "unknown_delivery" };
|
|
}
|
|
if (outbound.bouncedAt) {
|
|
return { status: "already_bounced", outboundEmailId: outbound.id, campaignId: outbound.campaignId };
|
|
}
|
|
|
|
const updated = await tx.outboundEmail.updateMany({
|
|
where: { id: outbound.id, tenantId: input.tenantId, bouncedAt: null },
|
|
data: { bouncedAt: input.bouncedAt },
|
|
});
|
|
if (updated.count !== 1) {
|
|
return { status: "already_bounced", outboundEmailId: outbound.id, campaignId: outbound.campaignId };
|
|
}
|
|
|
|
const campaignKey = toAutomationCampaignKey(outbound.campaign.name) || outbound.campaign.id;
|
|
await publishOutboxEvent(tx as unknown as OutboxPublisherTransaction, {
|
|
tenantId: input.tenantId,
|
|
type: "workflow.trigger.event",
|
|
payload: {
|
|
event: "email.campaign_bounced",
|
|
data: {
|
|
outboundEmailId: outbound.id,
|
|
campaignId: outbound.campaign.id,
|
|
campaignKey,
|
|
kampagne: campaignKey,
|
|
campaignName: outbound.campaign.name,
|
|
bounceKind: input.bounceKind,
|
|
bounceArt: input.bounceKind,
|
|
providerEventType: input.providerEventType,
|
|
bouncedAt: input.bouncedAt.toISOString(),
|
|
},
|
|
userId: null,
|
|
} satisfies Prisma.InputJsonValue,
|
|
dispatchKey: `workflow-trigger:email-bounce:${outbound.id}`,
|
|
});
|
|
|
|
return { status: "bounced", outboundEmailId: outbound.id, campaignId: outbound.campaignId };
|
|
});
|
|
}
|
|
|
|
export function toAutomationCampaignKey(name: string): string {
|
|
const words = name
|
|
.replace(/[Ää]/gu, (value) => (value === "Ä" ? "Ae" : "ae"))
|
|
.replace(/[Öö]/gu, (value) => (value === "Ö" ? "Oe" : "oe"))
|
|
.replace(/[Üü]/gu, (value) => (value === "Ü" ? "Ue" : "ue"))
|
|
.replace(/ß/gu, "ss")
|
|
.normalize("NFKD")
|
|
.replace(/[\u0300-\u036f]/gu, "")
|
|
.match(/[\p{L}\p{N}]+/gu);
|
|
if (!words?.length) return "";
|
|
return words
|
|
.map((word, index) => {
|
|
const lower = word.toLocaleLowerCase("de-DE");
|
|
return index === 0 ? lower : `${lower.charAt(0).toLocaleUpperCase("de-DE")}${lower.slice(1)}`;
|
|
})
|
|
.join("");
|
|
}
|