-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathroute.ts
More file actions
144 lines (126 loc) · 3.59 KB
/
Copy pathroute.ts
File metadata and controls
144 lines (126 loc) · 3.59 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { CRON_BATCH_SIZE, qstash } from "@/lib/cron";
import { enqueueBatchJobs } from "@/lib/cron/enqueue-batch-jobs";
import { withCron } from "@/lib/cron/with-cron";
import { isNonRecoverableDiscountError } from "@/lib/discounts/discount-error";
import { getDiscountProvider } from "@/lib/discounts/discount-provider";
import { prisma } from "@/lib/prisma";
import { ACTIVE_ENROLLMENT_STATUSES } from "@/lib/zod/schemas/partners";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import * as z from "zod/v4";
import { logAndRespond } from "../../../utils";
export const dynamic = "force-dynamic";
const inputSchema = z.object({
discountId: z.string(),
startingAfter: z.string().optional(),
});
// POST /api/cron/discount-codes/create/queue-batches
export const POST = withCron(async ({ rawBody }) => {
const { discountId, startingAfter } = inputSchema.parse(JSON.parse(rawBody));
const discount = await prisma.discount.findUnique({
where: {
id: discountId,
},
include: {
program: {
select: {
id: true,
workspace: {
select: {
id: true,
stripeConnectId: true,
shopifyStoreId: true,
},
},
},
},
},
});
if (!discount) {
return logAndRespond(`Discount ${discountId} not found. Skipping...`);
}
if (!discount.autoProvisionEnabledAt) {
return logAndRespond(
`Discount ${discountId} does not have auto-provision enabled. Skipping...`,
);
}
const { program } = discount;
const discountProvider = getDiscountProvider(discount.provider);
try {
await discountProvider.assertDiscountIntegration({
workspace: program.workspace,
});
} catch (error) {
if (isNonRecoverableDiscountError(error)) {
return logAndRespond(error.message, { logLevel: "warn" });
}
throw error;
}
const programEnrollments = await prisma.programEnrollment.findMany({
where: {
programId: program.id,
discountId: discount.id,
status: {
in: ACTIVE_ENROLLMENT_STATUSES,
},
},
select: {
id: true,
partnerId: true,
discountId: true,
links: {
select: {
id: true,
},
where: {
discountCode: null,
partnerGroupDefaultLinkId: {
not: null,
},
},
},
},
...(startingAfter && {
skip: 1,
cursor: {
id: startingAfter,
},
}),
orderBy: {
id: "asc",
},
take: CRON_BATCH_SIZE,
});
if (programEnrollments.length === 0) {
return logAndRespond(
`No more program enrollments found for discount ${discountId}.`,
);
}
const links = programEnrollments.flatMap(({ links }) => links);
if (links.length > 0) {
await enqueueBatchJobs(
links.map((link) => ({
queueName: "create-discount-code",
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/discount-codes/create`,
deduplicationId: `${discountId}-${link.id}`,
body: {
linkId: link.id,
},
})),
);
}
if (programEnrollments.length === CRON_BATCH_SIZE) {
const startingAfter = programEnrollments[programEnrollments.length - 1].id;
await qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/discount-codes/create/queue-batches`,
method: "POST",
body: {
discountId,
startingAfter,
},
});
return logAndRespond(
`Queued next batch for discount ${discountId} (startingAfter: ${startingAfter}).`,
);
}
return logAndRespond(`Finished queuing jobs for discount ${discountId}.`);
});