Support free subscription plans and unique billing periods in admin.
Add is_free toggles with paid price validation, client-side period conflict checks, and Free price display aligned with backend subscription rules. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
1799488127
commit
07c651fd17
|
|
@ -26,6 +26,7 @@ function normalizeSubscriptionPlan(raw: unknown): SubscriptionPlan | null {
|
|||
price: Number(raw.price ?? 0),
|
||||
currency: String(raw.currency ?? "ETB"),
|
||||
is_lifetime: Boolean(raw.is_lifetime ?? false),
|
||||
is_free: Boolean(raw.is_free ?? false),
|
||||
is_active: Boolean(raw.is_active ?? true),
|
||||
created_at: String(raw.created_at ?? ""),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,64 @@ export const LIFETIME_DURATION_DEFAULT = {
|
|||
duration_unit: "YEAR" as SubscriptionPlanDurationUnit,
|
||||
}
|
||||
|
||||
export const DUPLICATE_SUBSCRIPTION_PLAN_PERIOD_MESSAGE =
|
||||
"Only one plan is allowed per category and billing period. A free plan and a paid plan cannot share the same period—edit the existing plan or change category or duration."
|
||||
|
||||
export type SubscriptionPlanPeriodCandidate = {
|
||||
category: string
|
||||
is_lifetime: boolean
|
||||
duration_value: number
|
||||
duration_unit: SubscriptionPlanDurationUnit
|
||||
}
|
||||
|
||||
function normalizedPeriod(
|
||||
candidate: SubscriptionPlanPeriodCandidate,
|
||||
): SubscriptionPlanPeriodCandidate {
|
||||
if (candidate.is_lifetime) {
|
||||
return {
|
||||
category: candidate.category,
|
||||
is_lifetime: true,
|
||||
duration_value: LIFETIME_DURATION_DEFAULT.duration_value,
|
||||
duration_unit: LIFETIME_DURATION_DEFAULT.duration_unit,
|
||||
}
|
||||
}
|
||||
return {
|
||||
...candidate,
|
||||
duration_unit: String(candidate.duration_unit).toUpperCase() as SubscriptionPlanDurationUnit,
|
||||
}
|
||||
}
|
||||
|
||||
export function sameSubscriptionPlanPeriod(
|
||||
a: SubscriptionPlanPeriodCandidate,
|
||||
b: SubscriptionPlanPeriodCandidate,
|
||||
): boolean {
|
||||
const left = normalizedPeriod(a)
|
||||
const right = normalizedPeriod(b)
|
||||
return (
|
||||
left.category === right.category &&
|
||||
left.is_lifetime === right.is_lifetime &&
|
||||
left.duration_value === right.duration_value &&
|
||||
left.duration_unit === right.duration_unit
|
||||
)
|
||||
}
|
||||
|
||||
export function findConflictingSubscriptionPlan(
|
||||
plans: SubscriptionPlan[],
|
||||
candidate: SubscriptionPlanPeriodCandidate,
|
||||
excludePlanId?: number,
|
||||
): SubscriptionPlan | undefined {
|
||||
return plans.find(
|
||||
(plan) =>
|
||||
plan.id !== excludePlanId &&
|
||||
sameSubscriptionPlanPeriod(candidate, {
|
||||
category: plan.category,
|
||||
is_lifetime: plan.is_lifetime,
|
||||
duration_value: plan.duration_value,
|
||||
duration_unit: plan.duration_unit,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
/** True when a plan is configured as one-time (never expires). */
|
||||
export function isLifetimePlan(
|
||||
plan: Pick<SubscriptionPlan, "is_lifetime"> | { is_lifetime?: boolean | null } | null | undefined,
|
||||
|
|
@ -64,7 +122,10 @@ export function formatPlanDuration(
|
|||
return `${v} ${word}`
|
||||
}
|
||||
|
||||
export function formatPlanPrice(plan: Pick<SubscriptionPlan, "price" | "currency">): string {
|
||||
export function formatPlanPrice(
|
||||
plan: Pick<SubscriptionPlan, "price" | "currency" | "is_free">,
|
||||
): string {
|
||||
if (plan.is_free) return "Free"
|
||||
const amount = Number(plan.price)
|
||||
const formatted = Number.isFinite(amount)
|
||||
? amount.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 2 })
|
||||
|
|
@ -94,6 +155,7 @@ export function planToUpdatePayload(
|
|||
price: plan.price,
|
||||
currency: plan.currency,
|
||||
is_lifetime: plan.is_lifetime,
|
||||
is_free: plan.is_free,
|
||||
is_active: plan.is_active,
|
||||
...overrides,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -362,6 +362,7 @@ export function SubscriptionPlansTab() {
|
|||
open={createOpen}
|
||||
onOpenChange={setCreateOpen}
|
||||
onCreated={handleCreated}
|
||||
existingPlans={plans}
|
||||
/>
|
||||
|
||||
<EditSubscriptionPlanDialog
|
||||
|
|
@ -371,6 +372,7 @@ export function SubscriptionPlansTab() {
|
|||
if (!open) setPlanToEdit(null)
|
||||
}}
|
||||
onUpdated={handleUpdated}
|
||||
existingPlans={plans}
|
||||
/>
|
||||
|
||||
<DeleteSubscriptionPlanDialog
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ import { SpinnerIcon } from "../../../components/ui/spinner-icon"
|
|||
import { Textarea } from "../../../components/ui/textarea"
|
||||
import { ToggleSwitch } from "../../../components/ui/toggle-switch"
|
||||
import {
|
||||
DUPLICATE_SUBSCRIPTION_PLAN_PERIOD_MESSAGE,
|
||||
findConflictingSubscriptionPlan,
|
||||
LIFETIME_DURATION_DEFAULT,
|
||||
SUBSCRIPTION_CURRENCIES,
|
||||
SUBSCRIPTION_DURATION_UNITS,
|
||||
|
|
@ -39,6 +41,7 @@ export interface CreateSubscriptionPlanDraft {
|
|||
price: string
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_free: boolean
|
||||
is_active: boolean
|
||||
}
|
||||
|
||||
|
|
@ -51,6 +54,7 @@ export const EMPTY_SUBSCRIPTION_PLAN_DRAFT: CreateSubscriptionPlanDraft = {
|
|||
price: "",
|
||||
currency: "ETB",
|
||||
is_lifetime: false,
|
||||
is_free: false,
|
||||
is_active: true,
|
||||
}
|
||||
|
||||
|
|
@ -68,7 +72,11 @@ function draftToPayload(draft: CreateSubscriptionPlanDraft): CreateSubscriptionP
|
|||
if (!name) return null
|
||||
if (!description) return null
|
||||
if (!Number.isFinite(duration_value) || duration_value < 1) return null
|
||||
if (!Number.isFinite(price) || price < 0) return null
|
||||
if (draft.is_free) {
|
||||
if (price !== 0) return null
|
||||
} else if (!Number.isFinite(price) || price <= 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
|
|
@ -76,9 +84,10 @@ function draftToPayload(draft: CreateSubscriptionPlanDraft): CreateSubscriptionP
|
|||
category: draft.category,
|
||||
duration_value,
|
||||
duration_unit,
|
||||
price,
|
||||
price: draft.is_free ? 0 : price,
|
||||
currency: draft.currency,
|
||||
is_lifetime: draft.is_lifetime,
|
||||
is_free: draft.is_free,
|
||||
is_active: draft.is_active,
|
||||
}
|
||||
}
|
||||
|
|
@ -87,12 +96,14 @@ type CreateSubscriptionPlanDialogProps = {
|
|||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
onCreated: (plan: SubscriptionPlan) => void
|
||||
existingPlans: SubscriptionPlan[]
|
||||
}
|
||||
|
||||
export function CreateSubscriptionPlanDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
onCreated,
|
||||
existingPlans,
|
||||
}: CreateSubscriptionPlanDialogProps) {
|
||||
const [draft, setDraft] = useState<CreateSubscriptionPlanDraft>(EMPTY_SUBSCRIPTION_PLAN_DRAFT)
|
||||
const [saving, setSaving] = useState(false)
|
||||
|
|
@ -112,6 +123,17 @@ export function CreateSubscriptionPlanDialog({
|
|||
return
|
||||
}
|
||||
|
||||
const conflict = findConflictingSubscriptionPlan(existingPlans, {
|
||||
category: payload.category,
|
||||
is_lifetime: payload.is_lifetime,
|
||||
duration_value: payload.duration_value,
|
||||
duration_unit: payload.duration_unit,
|
||||
})
|
||||
if (conflict) {
|
||||
toast.error(DUPLICATE_SUBSCRIPTION_PLAN_PERIOD_MESSAGE)
|
||||
return
|
||||
}
|
||||
|
||||
setSaving(true)
|
||||
try {
|
||||
const res = await createSubscriptionPlan(payload)
|
||||
|
|
@ -267,20 +289,42 @@ export function CreateSubscriptionPlanDialog({
|
|||
</div>
|
||||
)}
|
||||
|
||||
<label className="flex cursor-pointer items-center justify-between gap-4 rounded-[8px] border border-grayScale-100 bg-grayScale-50/50 px-4 py-3">
|
||||
<div>
|
||||
<p className="text-sm font-medium text-grayScale-800">Free subscription</p>
|
||||
<p className="text-xs text-grayScale-500">
|
||||
Learners activate instantly with no Chapa or payment gateway checkout
|
||||
</p>
|
||||
</div>
|
||||
<ToggleSwitch
|
||||
variant="plain"
|
||||
checked={draft.is_free}
|
||||
aria-label="Free subscription"
|
||||
onCheckedChange={() =>
|
||||
setDraft((d) => ({
|
||||
...d,
|
||||
is_free: !d.is_free,
|
||||
price: !d.is_free ? "0" : d.price,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400">
|
||||
Price <span className="text-destructive">*</span>
|
||||
Price {!draft.is_free && <span className="text-destructive">*</span>}
|
||||
</label>
|
||||
<Input
|
||||
type="number"
|
||||
min={0}
|
||||
min={draft.is_free ? 0 : 0.01}
|
||||
step="0.01"
|
||||
value={draft.price}
|
||||
value={draft.is_free ? "0" : draft.price}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, price: e.target.value }))}
|
||||
placeholder="5.00"
|
||||
className="rounded-[6px]"
|
||||
required
|
||||
required={!draft.is_free}
|
||||
disabled={draft.is_free}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@ import { SpinnerIcon } from "../../../components/ui/spinner-icon"
|
|||
import { Textarea } from "../../../components/ui/textarea"
|
||||
import { ToggleSwitch } from "../../../components/ui/toggle-switch"
|
||||
import {
|
||||
DUPLICATE_SUBSCRIPTION_PLAN_PERIOD_MESSAGE,
|
||||
findConflictingSubscriptionPlan,
|
||||
formatPlanCategory,
|
||||
LIFETIME_DURATION_DEFAULT,
|
||||
SUBSCRIPTION_CURRENCIES,
|
||||
|
|
@ -38,6 +40,7 @@ interface EditDraft {
|
|||
price: string
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_free: boolean
|
||||
is_active: boolean
|
||||
}
|
||||
|
||||
|
|
@ -50,6 +53,7 @@ function planToDraft(plan: SubscriptionPlan): EditDraft {
|
|||
price: String(plan.price),
|
||||
currency: plan.currency,
|
||||
is_lifetime: Boolean(plan.is_lifetime),
|
||||
is_free: Boolean(plan.is_free),
|
||||
is_active: plan.is_active,
|
||||
}
|
||||
}
|
||||
|
|
@ -68,16 +72,21 @@ function draftToPayload(draft: EditDraft): UpdateSubscriptionPlanPayload | null
|
|||
if (!name) return null
|
||||
if (!description) return null
|
||||
if (!Number.isFinite(duration_value) || duration_value < 1) return null
|
||||
if (!Number.isFinite(price) || price < 0) return null
|
||||
if (draft.is_free) {
|
||||
if (price !== 0) return null
|
||||
} else if (!Number.isFinite(price) || price <= 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
description,
|
||||
duration_value,
|
||||
duration_unit,
|
||||
price,
|
||||
price: draft.is_free ? 0 : price,
|
||||
currency: draft.currency,
|
||||
is_lifetime: draft.is_lifetime,
|
||||
is_free: draft.is_free,
|
||||
is_active: draft.is_active,
|
||||
}
|
||||
}
|
||||
|
|
@ -94,6 +103,7 @@ type EditSubscriptionPlanDialogProps = {
|
|||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
onUpdated: (plan: SubscriptionPlan) => void
|
||||
existingPlans: SubscriptionPlan[]
|
||||
}
|
||||
|
||||
export function EditSubscriptionPlanDialog({
|
||||
|
|
@ -101,6 +111,7 @@ export function EditSubscriptionPlanDialog({
|
|||
open,
|
||||
onOpenChange,
|
||||
onUpdated,
|
||||
existingPlans,
|
||||
}: EditSubscriptionPlanDialogProps) {
|
||||
const [draft, setDraft] = useState<EditDraft | null>(null)
|
||||
const [saving, setSaving] = useState(false)
|
||||
|
|
@ -126,6 +137,21 @@ export function EditSubscriptionPlanDialog({
|
|||
return
|
||||
}
|
||||
|
||||
const conflict = findConflictingSubscriptionPlan(
|
||||
existingPlans,
|
||||
{
|
||||
category: plan.category,
|
||||
is_lifetime: payload.is_lifetime,
|
||||
duration_value: payload.duration_value,
|
||||
duration_unit: payload.duration_unit,
|
||||
},
|
||||
plan.id,
|
||||
)
|
||||
if (conflict) {
|
||||
toast.error(DUPLICATE_SUBSCRIPTION_PLAN_PERIOD_MESSAGE)
|
||||
return
|
||||
}
|
||||
|
||||
setSaving(true)
|
||||
try {
|
||||
const res = await updateSubscriptionPlan(plan.id, payload)
|
||||
|
|
@ -283,19 +309,45 @@ export function EditSubscriptionPlanDialog({
|
|||
</div>
|
||||
)}
|
||||
|
||||
<label className="flex cursor-pointer items-center justify-between gap-4 rounded-[8px] border border-grayScale-100 bg-grayScale-50/50 px-4 py-3">
|
||||
<div>
|
||||
<p className="text-sm font-medium text-grayScale-800">Free subscription</p>
|
||||
<p className="text-xs text-grayScale-500">
|
||||
Learners activate instantly with no Chapa or payment gateway checkout
|
||||
</p>
|
||||
</div>
|
||||
<ToggleSwitch
|
||||
variant="plain"
|
||||
checked={draft.is_free}
|
||||
aria-label="Free subscription"
|
||||
onCheckedChange={() =>
|
||||
setDraft((d) =>
|
||||
d
|
||||
? {
|
||||
...d,
|
||||
is_free: !d.is_free,
|
||||
price: !d.is_free ? "0" : d.price,
|
||||
}
|
||||
: d,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400">
|
||||
Price <span className="text-destructive">*</span>
|
||||
Price {!draft.is_free && <span className="text-destructive">*</span>}
|
||||
</label>
|
||||
<Input
|
||||
type="number"
|
||||
min={0}
|
||||
min={draft.is_free ? 0 : 0.01}
|
||||
step="0.01"
|
||||
value={draft.price}
|
||||
value={draft.is_free ? "0" : draft.price}
|
||||
onChange={(e) => setDraft((d) => d && { ...d, price: e.target.value })}
|
||||
className="rounded-[6px]"
|
||||
required
|
||||
required={!draft.is_free}
|
||||
disabled={draft.is_free}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ export interface SubscriptionPlan {
|
|||
price: number
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_free: boolean
|
||||
is_active: boolean
|
||||
created_at: string
|
||||
}
|
||||
|
|
@ -25,6 +26,7 @@ export interface CreateSubscriptionPlanPayload {
|
|||
price: number
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_free: boolean
|
||||
is_active: boolean
|
||||
}
|
||||
|
||||
|
|
@ -36,6 +38,7 @@ export interface UpdateSubscriptionPlanPayload {
|
|||
price: number
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_free: boolean
|
||||
is_active: boolean
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user