-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathpartner-profile.ts
More file actions
338 lines (310 loc) · 8.77 KB
/
Copy pathpartner-profile.ts
File metadata and controls
338 lines (310 loc) · 8.77 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import {
DATE_RANGE_INTERVAL_PRESETS,
DUB_PARTNERS_ANALYTICS_INTERVAL,
} from "@/lib/analytics/constants";
import { PARTNER_CUSTOMERS_MAX_PAGE_SIZE } from "@/lib/constants/partner-profile";
import {
CommissionType,
PartnerNetworkStatus,
PartnerPayoutMethod,
PartnerProfileType,
PartnerRole,
ProgramEnrollmentStatus,
SubmittedLeadStatus,
} from "@prisma/client";
import * as z from "zod/v4";
import { analyticsQuerySchema, eventsQuerySchema } from "./analytics";
import {
bountyPerformanceConditionSchema,
BountySchema,
BountySubmissionSchema,
} from "./bounties";
import {
CommissionSchema,
getCommissionsCountQuerySchema,
getCommissionsQuerySchema,
} from "./commissions";
import { customerActivityResponseSchema } from "./customer-activity";
import { CustomerEnrichedSchema } from "./customers";
import { LinkSchema } from "./links";
import { getPaginationQuerySchema } from "./misc";
import { payoutsQuerySchema } from "./payouts";
import { submittedLeadFormDataSchema } from "./submitted-lead-form";
import { centsSchema } from "./utils";
export const PartnerEarningsSchema = CommissionSchema.omit({
userId: true,
invoiceId: true,
}).extend({
customer: z
.object({
id: z.string(),
email: z.string(),
country: z.string().nullish(),
})
.nullable(),
link: LinkSchema.pick({
id: true,
shortLink: true,
url: true,
}).nullish(),
});
export const getPartnerEarningsQuerySchema = getCommissionsQuerySchema
.omit({
partnerId: true,
sortBy: true,
})
.extend({
interval: z
.enum(DATE_RANGE_INTERVAL_PRESETS)
.optional()
.default(DUB_PARTNERS_ANALYTICS_INTERVAL),
timezone: z.string().optional(),
type: z.enum(CommissionType).optional(),
linkId: z.string().optional(),
sortBy: z.enum(["createdAt", "amount", "earnings"]).default("createdAt"),
});
export const getPartnerEarningsCountQuerySchema = getCommissionsCountQuerySchema
.omit({
partnerId: true,
})
.extend({
interval: z
.enum(DATE_RANGE_INTERVAL_PRESETS)
.default(DUB_PARTNERS_ANALYTICS_INTERVAL),
timezone: z.string().optional(),
type: z.enum(CommissionType).optional(),
linkId: z.string().optional(),
groupBy: z.enum(["linkId", "customerId", "status", "type"]).optional(),
});
export const getPartnerEarningsTimeseriesSchema =
getPartnerEarningsCountQuerySchema.extend({
timezone: z.string().optional(),
});
export const PartnerProfileLinkSchema = LinkSchema.pick({
id: true,
domain: true,
key: true,
shortLink: true,
url: true,
clicks: true,
leads: true,
sales: true,
saleAmount: true,
comments: true,
}).extend({
createdAt: z.string().or(z.date()),
partnerGroupDefaultLinkId: z.string().nullish(),
discountCode: z.string().nullable().default(null),
discountCodeDisabledAt: z.coerce.date().nullable().default(null),
});
export const PartnerProfileCustomerSchema = CustomerEnrichedSchema.pick({
id: true,
email: true,
country: true,
createdAt: true,
firstSaleAt: true,
subscriptionCanceledAt: true,
}).extend({
activity: customerActivityResponseSchema,
});
export const partnerProfileAnalyticsQuerySchema = analyticsQuerySchema.omit({
externalId: true,
tenantId: true,
programId: true,
partnerId: true,
tagId: true,
tagIds: true,
folderId: true,
});
export const partnerProfileEventsQuerySchema = eventsQuerySchema.omit({
externalId: true,
tenantId: true,
programId: true,
partnerId: true,
tagId: true,
tagIds: true,
folderId: true,
});
export const partnerProfileProgramsQuerySchema = z.object({
includeRewardsDiscounts: z.coerce.boolean().optional(),
status: z.enum(ProgramEnrollmentStatus).optional(),
});
export const partnerProfileProgramsCountQuerySchema =
partnerProfileProgramsQuerySchema.pick({ status: true });
export const partnerNotificationTypes = z.enum([
"commissionCreated",
"applicationApproved",
"newMessageFromProgram",
"marketingCampaign",
"connectPayoutReminder",
"monthlyProgramSummary",
]);
export const partnerBountySubmissionSchema = BountySubmissionSchema.extend({
commission: PartnerEarningsSchema.pick({
id: true,
earnings: true,
status: true,
createdAt: true,
})
.nullable()
.default(null),
});
export const PartnerBountySchema = BountySchema.omit({
groups: true,
socialMetricsLastSyncedAt: true,
}).extend({
submissions: z.array(partnerBountySubmissionSchema),
performanceCondition: bountyPerformanceConditionSchema
.nullable()
.default(null),
partner: z.object({
totalClicks: z.number(),
totalLeads: z.number(),
totalConversions: z.number(),
totalSales: z.number(),
totalSaleAmount: centsSchema,
totalCommissions: centsSchema,
}),
});
export const invitePartnerUserSchema = z.object({
email: z.email({ error: "Please enter a valid email." }),
role: z.enum(PartnerRole),
});
export const getPartnerUsersQuerySchema = z.object({
search: z.string().optional(),
role: z.enum(PartnerRole).optional(),
});
export const partnerUserSchema = z.object({
id: z.string().nullable(),
name: z.string().nullable(),
email: z.string(),
role: z.enum(PartnerRole),
image: z.string().nullish(),
createdAt: z.date(),
});
export const partnerProfileChangeHistoryLogSchema = z.array(
z.union([
z.object({
field: z.literal("country"),
from: z.string().nullable(),
to: z.string(),
changedAt: z.coerce.date(),
}),
z.object({
field: z.literal("profileType"),
from: z.enum(PartnerProfileType),
to: z.enum(PartnerProfileType),
changedAt: z.coerce.date(),
}),
z.object({
field: z.literal("networkStatus"),
from: z.enum(PartnerNetworkStatus),
to: z.enum(PartnerNetworkStatus),
changedAt: z.coerce.date(),
}),
z.object({
field: z.literal("defaultPayoutMethod"),
from: z.enum(PartnerPayoutMethod).nullable(),
to: z.enum(PartnerPayoutMethod),
changedAt: z.coerce.date(),
}),
]),
);
export const partnerPayoutMethodSchema = z.object({
type: z.enum(PartnerPayoutMethod),
label: z.string(),
default: z.boolean(),
connected: z.boolean(),
identifier: z.string().nullable(),
});
export const setDefaultPayoutMethodSchema = z.object({
type: z.enum(PartnerPayoutMethod),
});
export const partnerProfilePayoutsQuerySchema = payoutsQuerySchema.extend({
programId: z.string().optional(),
sortBy: payoutsQuerySchema.shape.sortBy.default("initiatedAt"),
});
export const getPartnerCustomersQuerySchema = z
.object({
search: z
.string()
.optional()
.describe(
"A search query to filter customers by email or name. Only available if customer data sharing is enabled.",
),
country: z
.string()
.optional()
.describe(
"A filter on the list based on the customer's `country` field.",
),
linkId: z
.string()
.optional()
.describe(
"A filter on the list based on the customer's `linkId` field (the referral link ID).",
),
sortBy: z
.enum(["createdAt", "firstSaleAt", "subscriptionCanceledAt"])
.optional()
.default("createdAt")
.describe(
"The field to sort the customers by. The default is `createdAt`.",
),
sortOrder: z
.enum(["asc", "desc"])
.optional()
.default("desc")
.describe("The sort order. The default is `desc`."),
})
.extend(
getPaginationQuerySchema({ pageSize: PARTNER_CUSTOMERS_MAX_PAGE_SIZE }),
);
export const getPartnerCustomersCountQuerySchema =
getPartnerCustomersQuerySchema
.omit({
sortBy: true,
sortOrder: true,
page: true,
pageSize: true,
})
.extend({
groupBy: z.enum(["country", "linkId"]).optional(),
});
export const partnerProfileSubmittedLeadSchema = z.object({
id: z.string(),
name: z.string(),
email: z.email(),
company: z.string(),
status: z.enum(SubmittedLeadStatus),
customerId: z.string().nullable(),
formData: z.array(submittedLeadFormDataSchema).nullable().optional(),
createdAt: z.date(),
updatedAt: z.date(),
});
export type PartnerProfileSubmittedLead = z.infer<
typeof partnerProfileSubmittedLeadSchema
>;
export const getPartnerSubmittedLeadsQuerySchema = z
.object({
status: z.enum(SubmittedLeadStatus).optional(),
search: z.string().optional(),
})
.extend(getPaginationQuerySchema({ pageSize: 100 }));
export const getPartnerSubmittedLeadsCountQuerySchema =
getPartnerSubmittedLeadsQuerySchema
.omit({
page: true,
pageSize: true,
})
.extend({
groupBy: z.enum(["status"]).optional(),
});
export const partnerSubmittedLeadsCountByStatusSchema = z.object({
status: z.enum(SubmittedLeadStatus),
_count: z.number(),
});
export const partnerSubmittedLeadsCountResponseSchema = z.union([
z.array(partnerSubmittedLeadsCountByStatusSchema),
z.number(),
]);