Skip to content

Commit 56dca78

Browse files
fix(billing): deduplicate schema fields and fix update JSDoc return type
1 parent e002433 commit 56dca78

2 files changed

Lines changed: 23 additions & 35 deletions

File tree

modules/billing/models/billing.subscription.schema.js

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,36 @@ import { z } from 'zod';
88
*/
99
const objectIdRegex = /^[a-f\d]{24}$/i;
1010

11-
const Subscription = z.object({
12-
organization: z
13-
.string()
14-
.trim()
15-
.regex(objectIdRegex, 'organization must be a valid ObjectId'),
16-
stripeCustomerId: z
17-
.string()
18-
.trim()
19-
.optional()
20-
.transform((val) => (val === '' ? undefined : val)),
21-
stripeSubscriptionId: z
22-
.string()
23-
.trim()
24-
.optional()
25-
.transform((val) => (val === '' ? undefined : val)),
26-
plan: z.enum(['free', 'starter', 'pro']).default('free'),
27-
status: z.enum(['active', 'past_due', 'canceled', 'trialing', 'incomplete']).default('active'),
11+
const optionalStripeId = z
12+
.string()
13+
.trim()
14+
.optional()
15+
.transform((val) => (val === '' ? undefined : val));
16+
17+
const plans = ['free', 'starter', 'pro'];
18+
const statuses = ['active', 'past_due', 'canceled', 'trialing', 'incomplete'];
19+
20+
const baseShape = {
21+
organization: z.string().trim().regex(objectIdRegex, 'organization must be a valid ObjectId'),
22+
stripeCustomerId: optionalStripeId,
23+
stripeSubscriptionId: optionalStripeId,
2824
currentPeriodEnd: z.coerce.date().nullable().optional(),
25+
};
26+
27+
const Subscription = z.object({
28+
...baseShape,
29+
plan: z.enum(plans).default('free'),
30+
status: z.enum(statuses).default('active'),
2931
cancelAtPeriodEnd: z.boolean().default(false),
3032
});
3133

3234
/**
3335
* Update schema without defaults to avoid populating unspecified fields during PATCH
3436
*/
3537
const SubscriptionCore = z.object({
36-
organization: z
37-
.string()
38-
.trim()
39-
.regex(objectIdRegex, 'organization must be a valid ObjectId'),
40-
stripeCustomerId: z
41-
.string()
42-
.trim()
43-
.optional()
44-
.transform((val) => (val === '' ? undefined : val)),
45-
stripeSubscriptionId: z
46-
.string()
47-
.trim()
48-
.optional()
49-
.transform((val) => (val === '' ? undefined : val)),
50-
plan: z.enum(['free', 'starter', 'pro']),
51-
status: z.enum(['active', 'past_due', 'canceled', 'trialing', 'incomplete']),
52-
currentPeriodEnd: z.coerce.date().nullable().optional(),
38+
...baseShape,
39+
plan: z.enum(plans),
40+
status: z.enum(statuses),
5341
cancelAtPeriodEnd: z.boolean(),
5442
});
5543

modules/billing/repositories/billing.subscription.repository.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const get = (id) => {
5050
* @function update
5151
* @description Data access operation to update an existing subscription in the database.
5252
* @param {Object} subscription - The subscription object containing the updated details.
53-
* @returns {Promise<Object>} A promise resolving to the updated subscription.
53+
* @returns {Promise<Object>|null} A promise resolving to the updated subscription, or null if the ID is invalid.
5454
*/
5555
const update = (subscription) => {
5656
const id = resolveId(subscription);

0 commit comments

Comments
 (0)