Refactor billing schema creation logic
Updated logic in createBillingSchema function in the create-billing-schema.ts file for clarity and efficiency. Adjusted line item schema refinement rules and implemented changes in plan types parsing. Removed unnecessary console logs and added a console log to output the parsed config.
This commit is contained in:
@@ -58,7 +58,7 @@ export default withBundleAnalyzer({
|
|||||||
|
|
||||||
function getRemotePatterns() {
|
function getRemotePatterns() {
|
||||||
/** @type {import('next').NextConfig['remotePatterns']} */
|
/** @type {import('next').NextConfig['remotePatterns']} */
|
||||||
// add here the remote patterns for your images
|
// add here the remote patterns for your images
|
||||||
const remotePatterns = [];
|
const remotePatterns = [];
|
||||||
|
|
||||||
if (SUPABASE_URL) {
|
if (SUPABASE_URL) {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ const RecurringLineItemSchema = z
|
|||||||
)
|
)
|
||||||
.refine(
|
.refine(
|
||||||
(schema) => {
|
(schema) => {
|
||||||
if (!schema.metered && !schema.usageType) {
|
if (schema.metered && !schema.usageType) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,13 +51,15 @@ const RecurringSchema = z
|
|||||||
costPerUnit: z.number().positive().optional(),
|
costPerUnit: z.number().positive().optional(),
|
||||||
perSeat: z.boolean().optional(),
|
perSeat: z.boolean().optional(),
|
||||||
usageType: LineItemUsageType.optional(),
|
usageType: LineItemUsageType.optional(),
|
||||||
addOns: z.array(RecurringLineItemSchema).default([]).optional(),
|
addOns: z.array(RecurringLineItemSchema).optional(),
|
||||||
})
|
})
|
||||||
.refine(
|
.refine(
|
||||||
(schema) => {
|
(schema) => {
|
||||||
if (schema.metered) {
|
if (schema.metered) {
|
||||||
return schema.costPerUnit !== undefined;
|
return schema.costPerUnit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: 'Metered plans must have a cost per unit',
|
message: 'Metered plans must have a cost per unit',
|
||||||
@@ -79,7 +81,11 @@ const RecurringSchema = z
|
|||||||
)
|
)
|
||||||
.refine(
|
.refine(
|
||||||
(schema) => {
|
(schema) => {
|
||||||
return schema.metered && schema.usageType;
|
if (schema.metered) {
|
||||||
|
return !!schema.usageType;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: 'Metered plans must have a usage type',
|
message: 'Metered plans must have a usage type',
|
||||||
@@ -91,8 +97,8 @@ export const RecurringPlanSchema = z.object({
|
|||||||
name: z.string().min(1).max(100),
|
name: z.string().min(1).max(100),
|
||||||
id: z.string().min(1),
|
id: z.string().min(1),
|
||||||
price: z.number().positive(),
|
price: z.number().positive(),
|
||||||
trialDays: z.number().positive().optional(),
|
|
||||||
recurring: RecurringSchema,
|
recurring: RecurringSchema,
|
||||||
|
trialDays: z.number().positive().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const OneTimePlanSchema = z.object({
|
export const OneTimePlanSchema = z.object({
|
||||||
@@ -107,10 +113,10 @@ export const ProductSchema = z
|
|||||||
name: z.string(),
|
name: z.string(),
|
||||||
description: z.string(),
|
description: z.string(),
|
||||||
currency: z.string().optional().default('USD'),
|
currency: z.string().optional().default('USD'),
|
||||||
plans: z.union([
|
plans: RecurringPlanSchema.strict()
|
||||||
RecurringPlanSchema.array().nonempty(),
|
.array()
|
||||||
OneTimePlanSchema.array().nonempty(),
|
.nonempty()
|
||||||
]),
|
.or(OneTimePlanSchema.strict().array().nonempty()),
|
||||||
paymentType: PaymentType,
|
paymentType: PaymentType,
|
||||||
features: z.array(z.string()),
|
features: z.array(z.string()),
|
||||||
badge: z.string().min(1).optional(),
|
badge: z.string().min(1).optional(),
|
||||||
@@ -119,8 +125,6 @@ export const ProductSchema = z
|
|||||||
})
|
})
|
||||||
.refine(
|
.refine(
|
||||||
(schema) => {
|
(schema) => {
|
||||||
console.log(schema);
|
|
||||||
|
|
||||||
const recurringPlans = schema.plans.filter((plan) => 'recurring' in plan);
|
const recurringPlans = schema.plans.filter((plan) => 'recurring' in plan);
|
||||||
|
|
||||||
if (recurringPlans.length && schema.paymentType === 'one-time') {
|
if (recurringPlans.length && schema.paymentType === 'one-time') {
|
||||||
@@ -193,6 +197,7 @@ export const BillingSchema = z
|
|||||||
* @param config The billing configuration
|
* @param config The billing configuration
|
||||||
*/
|
*/
|
||||||
export function createBillingSchema(config: z.infer<typeof BillingSchema>) {
|
export function createBillingSchema(config: z.infer<typeof BillingSchema>) {
|
||||||
|
console.log(JSON.stringify(config));
|
||||||
return BillingSchema.parse(config);
|
return BillingSchema.parse(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user