-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathcreate-discount.ts
More file actions
159 lines (142 loc) · 4.27 KB
/
Copy pathcreate-discount.ts
File metadata and controls
159 lines (142 loc) · 4.27 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"use server";
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { createId } from "@/lib/api/create-id";
import { getGroupOrThrow } from "@/lib/api/groups/get-group-or-throw";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { qstash } from "@/lib/cron";
import { getDiscountProvider } from "@/lib/discounts/discount-provider";
import { prisma } from "@/lib/prisma";
import { DubDiscountAttributes } from "@/lib/stripe/coupon-discount-converter";
import { createDiscountSchema } from "@/lib/zod/schemas/discount";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { DiscountProvider } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { authActionClient } from "../safe-action";
import { throwIfNoPermission } from "../throw-if-no-permission";
export const createDiscountAction = authActionClient
.inputSchema(createDiscountSchema)
.action(async ({ parsedInput, ctx }) => {
const { workspace, user } = ctx;
const {
provider,
amount,
type,
maxDuration,
couponId,
couponTestId,
groupId,
autoProvision,
} = parsedInput;
throwIfNoPermission({
role: workspace.role,
requiredRoles: ["owner", "member"],
});
const programId = getDefaultProgramIdOrThrow(workspace);
const group = await getGroupOrThrow({
groupId,
programId,
});
if (group.discountId) {
throw new Error(
`You can't create a discount for this group because it already has a discount.`,
);
}
const discountProvider = getDiscountProvider(provider);
let coupon: (DubDiscountAttributes & { id: string }) | null = null;
// Fetch existing coupon if couponId is provided otherwise create a new coupon on the discount provider
if (provider === DiscountProvider.stripe) {
if (couponId) {
coupon = await discountProvider.getCoupon({
couponId,
workspace,
});
} else {
coupon = await discountProvider.createCoupon({
workspace,
group,
data: parsedInput,
});
}
} else if (provider === DiscountProvider.shopify) {
await discountProvider.assertDiscountIntegration({
workspace,
});
}
// Create the discount and update the group and program enrollment
const discount = await prisma.$transaction(async (tx) => {
const discount = await tx.discount.create({
data: {
id: createId({ prefix: "disc_" }),
programId,
amount,
type,
maxDuration,
provider,
couponId: coupon?.id || couponId || null,
...(couponTestId && { couponTestId }),
...(autoProvision && { autoProvisionEnabledAt: new Date() }),
},
});
await tx.partnerGroup.update({
where: {
id: groupId,
},
data: {
discountId: discount.id,
},
});
await tx.programEnrollment.updateMany({
where: {
groupId,
},
data: {
discountId: discount.id,
},
});
await tx.discountCode.updateMany({
where: {
programEnrollment: {
groupId,
},
},
data: {
discountId: discount.id,
},
});
return discount;
});
waitUntil(
Promise.allSettled([
qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/invalidate-for-discounts`,
body: {
groupId,
},
}),
recordAuditLog({
workspaceId: workspace.id,
programId,
action: "discount.created",
description: `Discount ${discount.id} created`,
actor: user,
targets: [
{
type: "discount",
id: discount.id,
metadata: discount,
},
],
}),
...(discount.autoProvisionEnabledAt
? [
qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/discount-codes/create/queue-batches`,
body: {
discountId: discount.id,
},
}),
]
: []),
]),
);
});