Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Marketing-Funnel Phase 3 unsubscribe route (Phase 05 #80).
|
||||
*
|
||||
* Accepts both GET (one-click via List-Unsubscribe header per RFC-8058)
|
||||
* and POST. Verifies the HMAC token and flips the EmailSequenceRun
|
||||
* status to `unsubscribed`. Idempotent: clicking the link twice is a
|
||||
* no-op on the second hit.
|
||||
*
|
||||
* Intentionally minimal — no session, no CSRF (token IS the auth).
|
||||
* Mail-clients (Gmail, Outlook) POST to this URL automatically when
|
||||
* the user hits the inbox "Unsubscribe" button.
|
||||
*/
|
||||
|
||||
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
||||
import { useLoaderData } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { verifyUnsubscribeToken } from "~/utils/email/unsubscribeToken.server";
|
||||
import { createScopedLogger } from "~/utils/logger.server";
|
||||
|
||||
const logger = createScopedLogger("marketing.unsubscribe");
|
||||
|
||||
type LoaderData = {
|
||||
status: "ok" | "invalid" | "already_unsubscribed" | "run_not_found";
|
||||
};
|
||||
|
||||
async function processUnsubscribe(token: string): Promise<LoaderData> {
|
||||
const payload = verifyUnsubscribeToken(token);
|
||||
if (!payload) {
|
||||
logger.warn({ op: "unsubscribe.invalid_token" }, "invalid unsubscribe token");
|
||||
return { status: "invalid" };
|
||||
}
|
||||
const run = await db.emailSequenceRun.findUnique({ where: { id: payload.runId } });
|
||||
if (!run) {
|
||||
logger.warn({ op: "unsubscribe.run_missing", runId: payload.runId }, "unsubscribe run not found");
|
||||
return { status: "run_not_found" };
|
||||
}
|
||||
if (run.status === "unsubscribed") {
|
||||
return { status: "already_unsubscribed" };
|
||||
}
|
||||
await db.emailSequenceRun.update({
|
||||
where: { id: payload.runId },
|
||||
data: { status: "unsubscribed", completedAt: new Date() },
|
||||
});
|
||||
logger.info(
|
||||
{ op: "unsubscribe.applied", runId: payload.runId, leadId: payload.leadId, tenantId: payload.tenantId },
|
||||
"sequence unsubscribed"
|
||||
);
|
||||
return { status: "ok" };
|
||||
}
|
||||
|
||||
export async function loader({ params }: LoaderFunctionArgs): Promise<LoaderData> {
|
||||
const token = params.token ?? "";
|
||||
return processUnsubscribe(token);
|
||||
}
|
||||
|
||||
export async function action({ params }: ActionFunctionArgs): Promise<LoaderData> {
|
||||
const token = params.token ?? "";
|
||||
return processUnsubscribe(token);
|
||||
}
|
||||
|
||||
const MESSAGES: Record<LoaderData["status"], { title: string; description: string }> = {
|
||||
ok: {
|
||||
title: "marketing.unsubscribe.ok.title",
|
||||
description: "marketing.unsubscribe.ok.description",
|
||||
},
|
||||
already_unsubscribed: {
|
||||
title: "marketing.unsubscribe.alreadyUnsubscribed.title",
|
||||
description: "marketing.unsubscribe.alreadyUnsubscribed.description",
|
||||
},
|
||||
invalid: {
|
||||
title: "marketing.unsubscribe.invalid.title",
|
||||
description: "marketing.unsubscribe.invalid.description",
|
||||
},
|
||||
run_not_found: {
|
||||
title: "marketing.unsubscribe.runNotFound.title",
|
||||
description: "marketing.unsubscribe.runNotFound.description",
|
||||
},
|
||||
};
|
||||
|
||||
export default function UnsubscribeRoute() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const { t } = useTranslation();
|
||||
const msg = MESSAGES[data.status];
|
||||
return (
|
||||
<main style={{ maxWidth: "600px", margin: "60px auto", padding: "0 20px", fontFamily: "system-ui, sans-serif" }}>
|
||||
<h1 style={{ fontSize: "24px" }}>{t(msg.title)}</h1>
|
||||
<p style={{ fontSize: "16px", lineHeight: "1.5", color: "#475569" }}>{t(msg.description)}</p>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user