-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathroute.ts
More file actions
105 lines (96 loc) · 2.99 KB
/
Copy pathroute.ts
File metadata and controls
105 lines (96 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { linkCache } from "@/lib/api/links/cache";
import { withCron } from "@/lib/cron/with-cron";
import { deleteDiscountCodes } from "@/lib/discounts/delete-discount-code";
import { prisma } from "@/lib/prisma";
import { sendBatchEmail } from "@dub/email";
import PartnerDeactivated from "@dub/email/templates/partner-deactivated";
import * as z from "zod/v4";
import { logAndRespond } from "../../utils";
const inputSchema = z.object({
programId: z.string(),
partnerIds: z.array(z.string()),
programDeactivated: z.boolean().optional().default(false),
});
// POST /api/cron/partners/deactivate - deactivate partners in a program
export const POST = withCron(async ({ rawBody }) => {
const { programId, partnerIds, programDeactivated } = inputSchema.parse(
JSON.parse(rawBody),
);
const programEnrollments = await prisma.programEnrollment.findMany({
where: {
programId,
partnerId: {
in: partnerIds,
},
},
include: {
partner: {
include: {
_count: {
select: {
users: true,
},
},
},
},
links: true,
discountCodes: {
include: {
discount: true,
},
},
},
});
// Expire all links in cache
const links = programEnrollments.flatMap(({ links }) => links);
await linkCache.expireMany(links);
console.log("[bulkDeactivatePartners] Expired links in cache.");
// Queue discount code deletions
const discountCodes = programEnrollments.flatMap(({ discountCodes }) =>
discountCodes.map((dc) => dc),
);
await deleteDiscountCodes(discountCodes, { isSoftDelete: true });
console.log("[bulkDeactivatePartners] Queued discount code deletions.");
// Find the program
const program = await prisma.program.findUniqueOrThrow({
where: {
id: programId,
},
select: {
name: true,
slug: true,
supportEmail: true,
},
});
// Send notification emails
const emailResponse = await sendBatchEmail(
programEnrollments
// only notify partners with user accounts (meaning they've signed up on partners.dub.co)
.filter(({ partner }) => partner._count.users > 0)
.map(({ partner }) => ({
variant: "notifications",
subject: programDeactivated
? `The ${program.name} program has been deactivated`
: `Your partnership with ${program.name} has been deactivated`,
to: partner.email!,
replyTo: program.supportEmail || "noreply",
react: PartnerDeactivated({
partner: {
name: partner.name,
email: partner.email!,
},
program: {
name: program.name,
slug: program.slug,
},
programDeactivated,
}),
})),
);
console.log("[bulkDeactivatePartners] Sent notification emails.", {
response: emailResponse,
});
return logAndRespond(
`[bulkDeactivatePartners] Deactivated ${partnerIds.length} partners for program ${programId}.`,
);
});