fix one-click unsub from the header (#195)
This commit is contained in:
@@ -24,6 +24,7 @@ export default function SesConfigurations() {
|
|||||||
<TableHeader className="">
|
<TableHeader className="">
|
||||||
<TableRow className=" bg-muted/30">
|
<TableRow className=" bg-muted/30">
|
||||||
<TableHead className="rounded-tl-xl">Region</TableHead>
|
<TableHead className="rounded-tl-xl">Region</TableHead>
|
||||||
|
<TableHead>Prefix Key</TableHead>
|
||||||
<TableHead>Callback URL</TableHead>
|
<TableHead>Callback URL</TableHead>
|
||||||
<TableHead>Callback status</TableHead>
|
<TableHead>Callback status</TableHead>
|
||||||
<TableHead>Created at</TableHead>
|
<TableHead>Created at</TableHead>
|
||||||
@@ -52,6 +53,7 @@ export default function SesConfigurations() {
|
|||||||
sesSettingsQuery.data?.map((sesSetting) => (
|
sesSettingsQuery.data?.map((sesSetting) => (
|
||||||
<TableRow key={sesSetting.id}>
|
<TableRow key={sesSetting.id}>
|
||||||
<TableCell>{sesSetting.region}</TableCell>
|
<TableCell>{sesSetting.region}</TableCell>
|
||||||
|
<TableCell>{sesSetting.idPrefix}</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<div className="w-[200px] overflow-hidden text-ellipsis">
|
<div className="w-[200px] overflow-hidden text-ellipsis">
|
||||||
<TextWithCopyButton
|
<TextWithCopyButton
|
||||||
|
49
apps/web/src/app/api/unsubscribe-oneclick/route.ts
Normal file
49
apps/web/src/app/api/unsubscribe-oneclick/route.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { unsubscribeContactFromLink } from "~/server/service/campaign-service";
|
||||||
|
import { logger } from "~/server/logger/log";
|
||||||
|
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const url = new URL(request.url);
|
||||||
|
const id = url.searchParams.get("id");
|
||||||
|
const hash = url.searchParams.get("hash");
|
||||||
|
|
||||||
|
if (!id || !hash) {
|
||||||
|
logger.warn(
|
||||||
|
`One-click unsubscribe: Missing id or hash id: ${id} hash: ${hash} url: ${request.url}`
|
||||||
|
);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Invalid unsubscribe link" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the unsubscribe using existing logic
|
||||||
|
const contact = await unsubscribeContactFromLink(id, hash);
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{ contactId: contact.id, campaignId: id.split("-")[1] },
|
||||||
|
"One-click unsubscribe successful"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Return success response for email clients
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
message: "Successfully unsubscribed",
|
||||||
|
},
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(
|
||||||
|
{ error: error instanceof Error ? error.message : error },
|
||||||
|
"One-click unsubscribe failed"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Return error response
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Failed to process unsubscribe request" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -99,6 +99,16 @@ export function createUnsubUrl(contactId: string, campaignId: string) {
|
|||||||
return `${env.NEXTAUTH_URL}/unsubscribe?id=${unsubId}&hash=${unsubHash}`;
|
return `${env.NEXTAUTH_URL}/unsubscribe?id=${unsubId}&hash=${unsubHash}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createOneClickUnsubUrl(contactId: string, campaignId: string) {
|
||||||
|
const unsubId = `${contactId}-${campaignId}`;
|
||||||
|
|
||||||
|
const unsubHash = createHash("sha256")
|
||||||
|
.update(`${unsubId}-${env.NEXTAUTH_SECRET}`)
|
||||||
|
.digest("hex");
|
||||||
|
|
||||||
|
return `${env.NEXTAUTH_URL}/api/unsubscribe-oneclick?id=${unsubId}&hash=${unsubHash}`;
|
||||||
|
}
|
||||||
|
|
||||||
export async function unsubscribeContactFromLink(id: string, hash: string) {
|
export async function unsubscribeContactFromLink(id: string, hash: string) {
|
||||||
const [contactId, campaignId] = id.split("-");
|
const [contactId, campaignId] = id.split("-");
|
||||||
|
|
||||||
@@ -253,6 +263,7 @@ async function processContactEmail(jobData: CampaignEmailJob) {
|
|||||||
const renderer = new EmailRenderer(jsonContent);
|
const renderer = new EmailRenderer(jsonContent);
|
||||||
|
|
||||||
const unsubscribeUrl = createUnsubUrl(contact.id, emailConfig.campaignId);
|
const unsubscribeUrl = createUnsubUrl(contact.id, emailConfig.campaignId);
|
||||||
|
const oneClickUnsubUrl = createOneClickUnsubUrl(contact.id, emailConfig.campaignId);
|
||||||
|
|
||||||
// Check for suppressed emails before processing
|
// Check for suppressed emails before processing
|
||||||
const toEmails = [contact.email];
|
const toEmails = [contact.email];
|
||||||
@@ -387,7 +398,7 @@ async function processContactEmail(jobData: CampaignEmailJob) {
|
|||||||
emailConfig.teamId,
|
emailConfig.teamId,
|
||||||
emailConfig.region,
|
emailConfig.region,
|
||||||
false,
|
false,
|
||||||
unsubscribeUrl
|
oneClickUnsubUrl
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user