-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.subscription.schema.js
More file actions
69 lines (60 loc) · 1.6 KB
/
Copy pathbilling.subscription.schema.js
File metadata and controls
69 lines (60 loc) · 1.6 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
/**
* Module dependencies
*/
import { z } from 'zod';
import config from '../../../config/index.js';
/**
* Data Schema
*/
const objectIdRegex = /^[a-f\d]{24}$/i;
const optionalStripeId = z
.string()
.trim()
.optional()
.transform((val) => (val === '' ? undefined : val));
const baseShape = {
organization: z.string().trim().regex(objectIdRegex, 'organization must be a valid ObjectId'),
stripeCustomerId: optionalStripeId,
stripeSubscriptionId: optionalStripeId,
currentPeriodEnd: z.coerce.date().nullable().optional(),
};
const Subscription = z.object({
...baseShape,
plan: z.enum(config.billing.plans).default('free'),
status: z.enum(config.billing.statuses).default('active'),
cancelAtPeriodEnd: z.boolean().default(false),
});
/**
* Update schema without defaults to avoid populating unspecified fields during PATCH
*/
const SubscriptionCore = z.object({
...baseShape,
plan: z.enum(config.billing.plans),
status: z.enum(config.billing.statuses),
cancelAtPeriodEnd: z.boolean(),
});
const SubscriptionUpdate = SubscriptionCore.partial();
/**
* Checkout request body schema
*/
const CheckoutRequest = z
.object({
priceId: z.string().trim().min(1, 'priceId is required'),
successUrl: z.string().url('successUrl must be a valid URL'),
cancelUrl: z.string().url('cancelUrl must be a valid URL'),
})
.strict();
/**
* Portal request body schema
*/
const PortalRequest = z
.object({
returnUrl: z.string().url('returnUrl must be a valid URL').optional(),
})
.strict();
export default {
Subscription,
SubscriptionUpdate,
CheckoutRequest,
PortalRequest,
};