Make one-time lifetime access a per-plan option in admin.
Add an is_lifetime toggle for all subscription categories and stop hardcoding IELTS/Duolingo as lifetime-only. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
31b1ea5a81
commit
c323df9f47
|
|
@ -25,6 +25,7 @@ function normalizeSubscriptionPlan(raw: unknown): SubscriptionPlan | null {
|
|||
duration_unit: String(raw.duration_unit ?? "MONTH"),
|
||||
price: Number(raw.price ?? 0),
|
||||
currency: String(raw.currency ?? "ETB"),
|
||||
is_lifetime: Boolean(raw.is_lifetime ?? false),
|
||||
is_active: Boolean(raw.is_active ?? true),
|
||||
created_at: String(raw.created_at ?? ""),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -322,6 +322,7 @@ function normalizeUserSubscriptionRecord(raw: unknown): UserSubscriptionRecord |
|
|||
plan_id: planId,
|
||||
plan_name: String(raw.plan_name ?? ""),
|
||||
plan_category: String(raw.plan_category ?? ""),
|
||||
is_lifetime: raw.is_lifetime === true,
|
||||
starts_at: String(raw.starts_at ?? ""),
|
||||
expires_at: String(raw.expires_at ?? ""),
|
||||
status: String(raw.status ?? ""),
|
||||
|
|
|
|||
|
|
@ -14,17 +14,16 @@ export const SUBSCRIPTION_PLAN_CATEGORIES: {
|
|||
{ value: "DUOLINGO", label: "Duolingo" },
|
||||
]
|
||||
|
||||
export const LIFETIME_SUBSCRIPTION_CATEGORIES: SubscriptionPlanCategory[] = ["IELTS", "DUOLINGO"]
|
||||
|
||||
export const LIFETIME_DURATION_DEFAULT = {
|
||||
duration_value: 1,
|
||||
duration_unit: "YEAR" as SubscriptionPlanDurationUnit,
|
||||
}
|
||||
|
||||
export function isLifetimePlanCategory(category: string | null | undefined): boolean {
|
||||
if (!category) return false
|
||||
const normalized = category.trim().toUpperCase()
|
||||
return LIFETIME_SUBSCRIPTION_CATEGORIES.includes(normalized as SubscriptionPlanCategory)
|
||||
/** 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,
|
||||
): boolean {
|
||||
return plan?.is_lifetime === true
|
||||
}
|
||||
|
||||
/** True when expires_at is the far-future lifetime sentinel (year >= 9000). */
|
||||
|
|
@ -48,9 +47,12 @@ export const SUBSCRIPTION_DURATION_UNITS: {
|
|||
export const SUBSCRIPTION_CURRENCIES = ["ETB", "USD"] as const
|
||||
|
||||
export function formatPlanDuration(
|
||||
plan: Pick<SubscriptionPlan, "duration_value" | "duration_unit"> & { category?: string },
|
||||
plan: Pick<SubscriptionPlan, "duration_value" | "duration_unit"> & {
|
||||
category?: string
|
||||
is_lifetime?: boolean
|
||||
},
|
||||
): string {
|
||||
if (plan.category && isLifetimePlanCategory(plan.category)) return "One-time"
|
||||
if (isLifetimePlan(plan)) return "One-time"
|
||||
const v = plan.duration_value
|
||||
const u = String(plan.duration_unit).toUpperCase()
|
||||
const word =
|
||||
|
|
@ -96,6 +98,7 @@ export function planToUpdatePayload(
|
|||
duration_unit: plan.duration_unit,
|
||||
price: plan.price,
|
||||
currency: plan.currency,
|
||||
is_lifetime: plan.is_lifetime,
|
||||
is_active: plan.is_active,
|
||||
...overrides,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ import { SpinnerIcon } from "../../../components/ui/spinner-icon"
|
|||
import { Textarea } from "../../../components/ui/textarea"
|
||||
import { ToggleSwitch } from "../../../components/ui/toggle-switch"
|
||||
import {
|
||||
isLifetimePlanCategory,
|
||||
LIFETIME_DURATION_DEFAULT,
|
||||
SUBSCRIPTION_CURRENCIES,
|
||||
SUBSCRIPTION_DURATION_UNITS,
|
||||
|
|
@ -39,6 +38,7 @@ export interface CreateSubscriptionPlanDraft {
|
|||
duration_unit: SubscriptionPlanDurationUnit
|
||||
price: string
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_active: boolean
|
||||
}
|
||||
|
||||
|
|
@ -50,6 +50,7 @@ export const EMPTY_SUBSCRIPTION_PLAN_DRAFT: CreateSubscriptionPlanDraft = {
|
|||
duration_unit: "MONTH",
|
||||
price: "",
|
||||
currency: "ETB",
|
||||
is_lifetime: false,
|
||||
is_active: true,
|
||||
}
|
||||
|
||||
|
|
@ -57,11 +58,12 @@ function draftToPayload(draft: CreateSubscriptionPlanDraft): CreateSubscriptionP
|
|||
const name = draft.name.trim()
|
||||
const description = draft.description.trim()
|
||||
const price = Number(draft.price)
|
||||
const isLifetime = isLifetimePlanCategory(draft.category)
|
||||
const duration_value = isLifetime
|
||||
const duration_value = draft.is_lifetime
|
||||
? LIFETIME_DURATION_DEFAULT.duration_value
|
||||
: Number(draft.duration_value)
|
||||
const duration_unit = isLifetime ? LIFETIME_DURATION_DEFAULT.duration_unit : draft.duration_unit
|
||||
const duration_unit = draft.is_lifetime
|
||||
? LIFETIME_DURATION_DEFAULT.duration_unit
|
||||
: draft.duration_unit
|
||||
|
||||
if (!name) return null
|
||||
if (!description) return null
|
||||
|
|
@ -76,6 +78,7 @@ function draftToPayload(draft: CreateSubscriptionPlanDraft): CreateSubscriptionP
|
|||
duration_unit,
|
||||
price,
|
||||
currency: draft.currency,
|
||||
is_lifetime: draft.is_lifetime,
|
||||
is_active: draft.is_active,
|
||||
}
|
||||
}
|
||||
|
|
@ -101,8 +104,6 @@ export function CreateSubscriptionPlanDialog({
|
|||
}
|
||||
}, [open])
|
||||
|
||||
const isLifetime = isLifetimePlanCategory(draft.category)
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
const payload = draftToPayload(draft)
|
||||
|
|
@ -174,19 +175,12 @@ export function CreateSubscriptionPlanDialog({
|
|||
</label>
|
||||
<Select
|
||||
value={draft.category}
|
||||
onChange={(e) => {
|
||||
const category = e.target.value as SubscriptionPlanCategory
|
||||
onChange={(e) =>
|
||||
setDraft((d) => ({
|
||||
...d,
|
||||
category,
|
||||
...(isLifetimePlanCategory(category)
|
||||
? {
|
||||
duration_value: String(LIFETIME_DURATION_DEFAULT.duration_value),
|
||||
duration_unit: LIFETIME_DURATION_DEFAULT.duration_unit,
|
||||
}
|
||||
: {}),
|
||||
category: e.target.value as SubscriptionPlanCategory,
|
||||
}))
|
||||
}}
|
||||
}
|
||||
className="rounded-[6px]"
|
||||
>
|
||||
{SUBSCRIPTION_PLAN_CATEGORIES.map((opt) => (
|
||||
|
|
@ -197,14 +191,40 @@ export function CreateSubscriptionPlanDialog({
|
|||
</Select>
|
||||
</div>
|
||||
|
||||
{isLifetime ? (
|
||||
<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">One-time purchase</p>
|
||||
<p className="text-xs text-grayScale-500">
|
||||
Lifetime access with no expiration date
|
||||
</p>
|
||||
</div>
|
||||
<ToggleSwitch
|
||||
variant="plain"
|
||||
checked={draft.is_lifetime}
|
||||
aria-label="One-time purchase"
|
||||
onCheckedChange={() =>
|
||||
setDraft((d) => ({
|
||||
...d,
|
||||
is_lifetime: !d.is_lifetime,
|
||||
...(d.is_lifetime
|
||||
? {}
|
||||
: {
|
||||
duration_value: String(LIFETIME_DURATION_DEFAULT.duration_value),
|
||||
duration_unit: LIFETIME_DURATION_DEFAULT.duration_unit,
|
||||
}),
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
|
||||
{draft.is_lifetime ? (
|
||||
<div className="rounded-[8px] border border-grayScale-100 bg-grayScale-50/50 px-4 py-3">
|
||||
<p className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400">
|
||||
Duration
|
||||
</p>
|
||||
<p className="mt-1 text-sm font-medium text-grayScale-800">One-time · Never expires</p>
|
||||
<p className="mt-0.5 text-xs text-grayScale-500">
|
||||
IELTS and Duolingo packages are one-time purchases with lifetime access.
|
||||
Related subscriptions stay active permanently after purchase.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ import { Textarea } from "../../../components/ui/textarea"
|
|||
import { ToggleSwitch } from "../../../components/ui/toggle-switch"
|
||||
import {
|
||||
formatPlanCategory,
|
||||
isLifetimePlanCategory,
|
||||
LIFETIME_DURATION_DEFAULT,
|
||||
SUBSCRIPTION_CURRENCIES,
|
||||
SUBSCRIPTION_DURATION_UNITS,
|
||||
|
|
@ -38,6 +37,7 @@ interface EditDraft {
|
|||
duration_unit: SubscriptionPlanDurationUnit
|
||||
price: string
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_active: boolean
|
||||
}
|
||||
|
||||
|
|
@ -49,22 +49,21 @@ function planToDraft(plan: SubscriptionPlan): EditDraft {
|
|||
duration_unit: plan.duration_unit,
|
||||
price: String(plan.price),
|
||||
currency: plan.currency,
|
||||
is_lifetime: Boolean(plan.is_lifetime),
|
||||
is_active: plan.is_active,
|
||||
}
|
||||
}
|
||||
|
||||
function draftToPayload(
|
||||
draft: EditDraft,
|
||||
category: string,
|
||||
): UpdateSubscriptionPlanPayload | null {
|
||||
function draftToPayload(draft: EditDraft): UpdateSubscriptionPlanPayload | null {
|
||||
const name = draft.name.trim()
|
||||
const description = draft.description.trim()
|
||||
const price = Number(draft.price)
|
||||
const isLifetime = isLifetimePlanCategory(category)
|
||||
const duration_value = isLifetime
|
||||
const duration_value = draft.is_lifetime
|
||||
? LIFETIME_DURATION_DEFAULT.duration_value
|
||||
: Number(draft.duration_value)
|
||||
const duration_unit = isLifetime ? LIFETIME_DURATION_DEFAULT.duration_unit : draft.duration_unit
|
||||
const duration_unit = draft.is_lifetime
|
||||
? LIFETIME_DURATION_DEFAULT.duration_unit
|
||||
: draft.duration_unit
|
||||
|
||||
if (!name) return null
|
||||
if (!description) return null
|
||||
|
|
@ -78,6 +77,7 @@ function draftToPayload(
|
|||
duration_unit,
|
||||
price,
|
||||
currency: draft.currency,
|
||||
is_lifetime: draft.is_lifetime,
|
||||
is_active: draft.is_active,
|
||||
}
|
||||
}
|
||||
|
|
@ -120,7 +120,7 @@ export function EditSubscriptionPlanDialog({
|
|||
e.preventDefault()
|
||||
if (!plan || !draft) return
|
||||
|
||||
const payload = draftToPayload(draft, plan.category)
|
||||
const payload = draftToPayload(draft)
|
||||
if (!payload) {
|
||||
toast.error("Please fill in all required fields with valid values.")
|
||||
return
|
||||
|
|
@ -195,7 +195,37 @@ export function EditSubscriptionPlanDialog({
|
|||
/>
|
||||
</div>
|
||||
|
||||
{isLifetimePlanCategory(plan.category) ? (
|
||||
<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">One-time purchase</p>
|
||||
<p className="text-xs text-grayScale-500">
|
||||
Lifetime access with no expiration date
|
||||
</p>
|
||||
</div>
|
||||
<ToggleSwitch
|
||||
variant="plain"
|
||||
checked={draft.is_lifetime}
|
||||
aria-label="One-time purchase"
|
||||
onCheckedChange={() =>
|
||||
setDraft((d) =>
|
||||
d
|
||||
? {
|
||||
...d,
|
||||
is_lifetime: !d.is_lifetime,
|
||||
...(!d.is_lifetime
|
||||
? {
|
||||
duration_value: String(LIFETIME_DURATION_DEFAULT.duration_value),
|
||||
duration_unit: LIFETIME_DURATION_DEFAULT.duration_unit,
|
||||
}
|
||||
: {}),
|
||||
}
|
||||
: d,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
|
||||
{draft.is_lifetime ? (
|
||||
<div className="rounded-[8px] border border-grayScale-100 bg-grayScale-50/50 px-4 py-3">
|
||||
<p className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400">
|
||||
Duration
|
||||
|
|
@ -204,7 +234,7 @@ export function EditSubscriptionPlanDialog({
|
|||
One-time · Never expires
|
||||
</p>
|
||||
<p className="mt-0.5 text-xs text-grayScale-500">
|
||||
IELTS and Duolingo packages are one-time purchases with lifetime access.
|
||||
Related active subscriptions are kept non-expiring when this is enabled.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
|
|
|
|||
|
|
@ -19,17 +19,21 @@ import {
|
|||
isLearnerRole,
|
||||
SUBSCRIPTION_ADMIN_PERMISSIONS,
|
||||
} from "../../../lib/subscriptionAdminPermissions"
|
||||
import { formatPlanCategory, isLifetimeExpiry, isLifetimePlanCategory } from "../../../lib/subscriptionPlans"
|
||||
import { formatPlanCategory, isLifetimeExpiry } from "../../../lib/subscriptionPlans"
|
||||
import { cn } from "../../../lib/utils"
|
||||
import type { UserSubscriptionRecord, UserSubscriptionsData } from "../../../types/userAdmin.types"
|
||||
import { CancelSubscriptionDialog } from "./CancelSubscriptionDialog"
|
||||
import { ExtendSubscriptionDialog } from "./ExtendSubscriptionDialog"
|
||||
import { GrantSubscriptionDialog } from "./GrantSubscriptionDialog"
|
||||
|
||||
function isLifetimeSubscription(subscription: UserSubscriptionRecord): boolean {
|
||||
return subscription.is_lifetime === true || isLifetimeExpiry(subscription.expires_at)
|
||||
}
|
||||
|
||||
function formatPlanTitle(subscription: UserSubscriptionRecord): string {
|
||||
const name = subscription.plan_name?.trim()
|
||||
if (name) return name
|
||||
if (isLifetimePlanCategory(subscription.plan_category)) return "One-time"
|
||||
if (isLifetimeSubscription(subscription)) return "One-time"
|
||||
if (subscription.duration_value && subscription.duration_unit) {
|
||||
const unit = subscription.duration_unit.toLowerCase().replace(/s$/, "")
|
||||
const plural = subscription.duration_value === 1 ? unit : `${unit}s`
|
||||
|
|
@ -38,8 +42,9 @@ function formatPlanTitle(subscription: UserSubscriptionRecord): string {
|
|||
return NOT_ASSIGNED_LABEL
|
||||
}
|
||||
|
||||
function formatExpiryDate(value?: string | null, category?: string | null): string {
|
||||
if (isLifetimePlanCategory(category) || isLifetimeExpiry(value)) return "Never expires"
|
||||
function formatExpiryDate(subscription: UserSubscriptionRecord): string {
|
||||
if (isLifetimeSubscription(subscription)) return "Never expires"
|
||||
const value = subscription.expires_at
|
||||
if (!value?.trim()) return NOT_ASSIGNED_LABEL
|
||||
const parsed = new Date(value)
|
||||
if (Number.isNaN(parsed.getTime())) return NOT_ASSIGNED_LABEL
|
||||
|
|
@ -63,8 +68,9 @@ function formatDateTime(value?: string | null): string {
|
|||
})
|
||||
}
|
||||
|
||||
function getDaysLeft(expiresAt?: string | null, category?: string | null): number | null {
|
||||
if (isLifetimePlanCategory(category) || isLifetimeExpiry(expiresAt)) return null
|
||||
function getDaysLeft(subscription: UserSubscriptionRecord): number | null {
|
||||
if (isLifetimeSubscription(subscription)) return null
|
||||
const expiresAt = subscription.expires_at
|
||||
if (!expiresAt?.trim()) return null
|
||||
const expires = new Date(expiresAt)
|
||||
if (Number.isNaN(expires.getTime())) return null
|
||||
|
|
@ -73,7 +79,7 @@ function getDaysLeft(expiresAt?: string | null, category?: string | null): numbe
|
|||
}
|
||||
|
||||
function canExtendSubscription(subscription: UserSubscriptionRecord): boolean {
|
||||
return !isLifetimePlanCategory(subscription.plan_category) && !isLifetimeExpiry(subscription.expires_at)
|
||||
return !isLifetimeSubscription(subscription)
|
||||
}
|
||||
|
||||
function formatDaysLeftLabel(daysLeft: number): string {
|
||||
|
|
@ -145,7 +151,7 @@ function SubscriptionManageCard({
|
|||
onGrant: () => void
|
||||
onCancel: () => void
|
||||
}) {
|
||||
const daysLeft = getDaysLeft(subscription.expires_at, subscription.plan_category)
|
||||
const daysLeft = getDaysLeft(subscription)
|
||||
const statusUpper = subscription.status.toUpperCase()
|
||||
const isActive = subscription.is_currently_active || statusUpper === "ACTIVE"
|
||||
const statusTone = isActive ? "active" : statusUpper === "PENDING" ? "pending" : "inactive"
|
||||
|
|
@ -176,7 +182,7 @@ function SubscriptionManageCard({
|
|||
<p className="text-xs font-medium text-grayScale-400">Expires On</p>
|
||||
<div className="mt-1 flex flex-wrap items-center gap-2">
|
||||
<p className="text-lg font-bold tracking-tight text-grayScale-900">
|
||||
{formatExpiryDate(subscription.expires_at, subscription.plan_category)}
|
||||
{formatExpiryDate(subscription)}
|
||||
</p>
|
||||
{daysLeft != null ? (
|
||||
<span
|
||||
|
|
@ -477,7 +483,7 @@ export function UserSubscriptionsSection({
|
|||
</p>
|
||||
<p className="text-xs text-grayScale-500">
|
||||
{formatPlanCategory(subscription.plan_category)} ·{" "}
|
||||
{formatExpiryDate(subscription.expires_at, subscription.plan_category)}
|
||||
{formatExpiryDate(subscription)}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ export interface SubscriptionPlan {
|
|||
duration_unit: SubscriptionPlanDurationUnit
|
||||
price: number
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_active: boolean
|
||||
created_at: string
|
||||
}
|
||||
|
|
@ -23,6 +24,7 @@ export interface CreateSubscriptionPlanPayload {
|
|||
duration_unit: SubscriptionPlanDurationUnit
|
||||
price: number
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_active: boolean
|
||||
}
|
||||
|
||||
|
|
@ -33,6 +35,7 @@ export interface UpdateSubscriptionPlanPayload {
|
|||
duration_unit: SubscriptionPlanDurationUnit
|
||||
price: number
|
||||
currency: string
|
||||
is_lifetime: boolean
|
||||
is_active: boolean
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -98,6 +98,7 @@ export interface UserSubscriptionRecord {
|
|||
plan_id: number
|
||||
plan_name: string
|
||||
plan_category: string
|
||||
is_lifetime: boolean
|
||||
starts_at: string
|
||||
expires_at: string
|
||||
status: string
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user