Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
TierInfinite,
UsageModel,
type UsagePriceConfig,
type UsageTier,
} from "@autumn/shared";
import { entsAreSame } from "@server/internal/products/entitlements/entitlementUtils";
import { pricesAreSame } from "@server/internal/products/prices/priceInitUtils";
Expand Down Expand Up @@ -241,7 +242,12 @@ const toFeatureAndPrice = ({
to: TierInfinite,
},
]
: (item.tiers as any),
: (item.tiers?.map((x) => {
return {
...x,
amount: x.amount ?? 0,
};
}) as UsageTier[]),
interval: itemToBillingInterval({ item }) as BillingInterval,
interval_count: item.interval_count || 1,
};
Expand Down
2 changes: 1 addition & 1 deletion server/src/utils/scriptUtils/constructItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const constructPrepaidItem = ({
}: {
featureId: string;
price?: number;
tiers?: { amount: number; to: number | "inf"; flat_amount?: number | null }[];
tiers?: { amount: number; to: number | "inf"; flat_amount?: number }[];
tierBehaviour?: TierBehavior;
billingUnits?: number;
includedUsage?: number;
Expand Down
4 changes: 2 additions & 2 deletions server/tests/utils/fixtures/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,15 @@ const volumePrepaidMessages = ({
}: {
includedUsage?: number;
billingUnits?: number;
tiers?: { to: number | "inf"; amount: number; flat_amount?: number | null }[];
tiers?: { to: number | "inf"; amount: number; flat_amount?: number }[];
config?: ProductItemConfig;
} = {}): LimitedItem =>
constructPrepaidItem({
featureId: TestFeature.Messages,
tiers: tiers as {
to: number;
amount: number;
flat_amount?: number | null;
flat_amount?: number;
}[],
tierBehaviour: TierBehavior.VolumeBased,
billingUnits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@ export enum TierBehavior {
VolumeBased = "volume",
}

export const UsageTierSchema = z.object({
to: z.number().or(z.literal(Infinite)),
amount: z.number(),
flat_amount: z.number().nullish(),
});
export const UsageTierSchema = z
.object({
to: z.number().or(z.literal(Infinite)),
amount: z.number().optional(),
flat_amount: z.number().optional(),
})
.refine(
(val) => val.amount !== undefined || val.flat_amount !== undefined,
{
message: "Either amount or flat_amount, or both must be defined",
path: ["amount", "flat_amount"],
},
)
.transform((val) => ({
...val,
amount: val.amount ?? 0,
}));

export type UsageTier = z.infer<typeof UsageTierSchema>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,32 @@ export enum ProductItemType {
Price = "price",
}

export const PriceTierSchema = z.object({
to: z.number().or(z.literal(TierInfinite)).meta({
description: "The maximum amount of usage for this tier.",
example: 100,
}),
amount: z.number().meta({
description: "The price of the product item for this tier.",
example: 10,
}),
flat_amount: z.number().nullish().meta({
description:
"A flat fee charged for this tier, in addition to the per-unit amount.",
}),
});
export const PriceTierSchema = z
.object({
to: z.number().or(z.literal(TierInfinite)).meta({
description: "The maximum amount of usage for this tier.",
example: 100,
}),
amount: z.number().optional().meta({
description: "The price of the product item for this tier.",
example: 10,
}),
flat_amount: z.number().optional().meta({
description:
"A flat fee charged for this tier, in addition to the per-unit amount.",
}),
})
.refine(
(val) => val.amount != null || val.flat_amount != null,
{
message: "Either amount or flat_amount, or both must be defined",
path: ["amount", "flat_amount"],
},
)
.transform((val) => ({
...val,
amount: val.amount ?? 0,
}));

export enum UsageModel {
Prepaid = "prepaid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const itemToPlanFeaturePrice = ({
tiers: item.tiers.map((tier) => ({
to: tier.to,
amount: tier.amount,
flat_amount: tier.flat_amount,
flat_amount: tier.flat_amount ?? undefined,
})),
included: includedUsage,
})
Expand Down
Loading