bulk unsub on complaint and bounce
This commit is contained in:
@@ -112,18 +112,22 @@ export async function unsubscribeContactFromLink(id: string, hash: string) {
|
|||||||
throw new Error("Invalid unsubscribe link");
|
throw new Error("Invalid unsubscribe link");
|
||||||
}
|
}
|
||||||
|
|
||||||
return await unsubscribeContact(
|
return await unsubscribeContact({
|
||||||
contactId,
|
contactId,
|
||||||
campaignId,
|
campaignId,
|
||||||
UnsubscribeReason.UNSUBSCRIBED
|
reason: UnsubscribeReason.UNSUBSCRIBED,
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function unsubscribeContact(
|
export async function unsubscribeContact({
|
||||||
contactId: string,
|
contactId,
|
||||||
campaignId: string,
|
campaignId,
|
||||||
reason: UnsubscribeReason
|
reason,
|
||||||
) {
|
}: {
|
||||||
|
contactId: string;
|
||||||
|
campaignId?: string;
|
||||||
|
reason: UnsubscribeReason;
|
||||||
|
}) {
|
||||||
// Update the contact's subscription status
|
// Update the contact's subscription status
|
||||||
try {
|
try {
|
||||||
const contact = await db.contact.findUnique({
|
const contact = await db.contact.findUnique({
|
||||||
@@ -140,14 +144,16 @@ export async function unsubscribeContact(
|
|||||||
data: { subscribed: false, unsubscribeReason: reason },
|
data: { subscribed: false, unsubscribeReason: reason },
|
||||||
});
|
});
|
||||||
|
|
||||||
await db.campaign.update({
|
if (campaignId) {
|
||||||
where: { id: campaignId },
|
await db.campaign.update({
|
||||||
data: {
|
where: { id: campaignId },
|
||||||
unsubscribed: {
|
data: {
|
||||||
increment: 1,
|
unsubscribed: {
|
||||||
|
increment: 1,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return contact;
|
return contact;
|
||||||
|
@@ -103,7 +103,12 @@ export async function parseSesHook(data: SesEvent) {
|
|||||||
mailStatus !== "CLICKED" ||
|
mailStatus !== "CLICKED" ||
|
||||||
!(mailData as SesClick).link.startsWith(`${env.NEXTAUTH_URL}/unsubscribe`)
|
!(mailData as SesClick).link.startsWith(`${env.NEXTAUTH_URL}/unsubscribe`)
|
||||||
) {
|
) {
|
||||||
await checkUnsubscribe(email.campaignId, email.contactId!, mailStatus);
|
await checkUnsubscribe({
|
||||||
|
contactId: email.contactId!,
|
||||||
|
campaignId: email.campaignId,
|
||||||
|
teamId: email.teamId,
|
||||||
|
event: mailStatus,
|
||||||
|
});
|
||||||
|
|
||||||
const mailEvent = await db.emailEvent.findFirst({
|
const mailEvent = await db.emailEvent.findFirst({
|
||||||
where: {
|
where: {
|
||||||
@@ -129,19 +134,60 @@ export async function parseSesHook(data: SesEvent) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkUnsubscribe(
|
async function checkUnsubscribe({
|
||||||
campaignId: string,
|
contactId,
|
||||||
contactId: string,
|
campaignId,
|
||||||
event: EmailStatus
|
teamId,
|
||||||
) {
|
event,
|
||||||
|
}: {
|
||||||
|
contactId: string;
|
||||||
|
campaignId: string;
|
||||||
|
teamId: number;
|
||||||
|
event: EmailStatus;
|
||||||
|
}) {
|
||||||
if (event === EmailStatus.BOUNCED || event === EmailStatus.COMPLAINED) {
|
if (event === EmailStatus.BOUNCED || event === EmailStatus.COMPLAINED) {
|
||||||
return unsubscribeContact(
|
const contact = await db.contact.findUnique({
|
||||||
contactId,
|
where: {
|
||||||
campaignId,
|
id: contactId,
|
||||||
event === EmailStatus.BOUNCED
|
},
|
||||||
? UnsubscribeReason.BOUNCED
|
});
|
||||||
: UnsubscribeReason.COMPLAINED
|
|
||||||
);
|
if (!contact) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const allContacts = await db.contact.findMany({
|
||||||
|
where: {
|
||||||
|
email: contact.email,
|
||||||
|
contactBook: {
|
||||||
|
teamId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const allContactIds = allContacts
|
||||||
|
.map((c) => c.id)
|
||||||
|
.filter((c) => c !== contactId);
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
unsubscribeContact({
|
||||||
|
contactId,
|
||||||
|
campaignId,
|
||||||
|
reason:
|
||||||
|
event === EmailStatus.BOUNCED
|
||||||
|
? UnsubscribeReason.BOUNCED
|
||||||
|
: UnsubscribeReason.COMPLAINED,
|
||||||
|
}),
|
||||||
|
...allContactIds.map((c) =>
|
||||||
|
unsubscribeContact({
|
||||||
|
contactId: c,
|
||||||
|
reason:
|
||||||
|
event === EmailStatus.BOUNCED
|
||||||
|
? UnsubscribeReason.BOUNCED
|
||||||
|
: UnsubscribeReason.COMPLAINED,
|
||||||
|
})
|
||||||
|
),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user