Treat IELTS and Duolingo subscription plans as one-time lifetime access.
Hide duration editing for those categories, show never-expires in user subscriptions, and disable extend for lifetime plans. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
827e20b4f1
commit
2180d64b06
|
|
@ -14,6 +14,27 @@ 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 expires_at is the far-future lifetime sentinel (year >= 9000). */
|
||||
export function isLifetimeExpiry(expiresAt: string | null | undefined): boolean {
|
||||
if (!expiresAt?.trim()) return false
|
||||
const d = new Date(expiresAt)
|
||||
if (Number.isNaN(d.getTime())) return false
|
||||
return d.getUTCFullYear() >= 9000
|
||||
}
|
||||
|
||||
export const SUBSCRIPTION_DURATION_UNITS: {
|
||||
value: SubscriptionPlanDurationUnit
|
||||
label: string
|
||||
|
|
@ -26,7 +47,10 @@ export const SUBSCRIPTION_DURATION_UNITS: {
|
|||
|
||||
export const SUBSCRIPTION_CURRENCIES = ["ETB", "USD"] as const
|
||||
|
||||
export function formatPlanDuration(plan: Pick<SubscriptionPlan, "duration_value" | "duration_unit">): string {
|
||||
export function formatPlanDuration(
|
||||
plan: Pick<SubscriptionPlan, "duration_value" | "duration_unit"> & { category?: string },
|
||||
): string {
|
||||
if (plan.category && isLifetimePlanCategory(plan.category)) return "One-time"
|
||||
const v = plan.duration_value
|
||||
const u = String(plan.duration_unit).toUpperCase()
|
||||
const word =
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ import { Select } from "../../../components/ui/select"
|
|||
import { SpinnerIcon } from "../../../components/ui/spinner-icon"
|
||||
import { Textarea } from "../../../components/ui/textarea"
|
||||
import { ToggleSwitch } from "../../../components/ui/toggle-switch"
|
||||
import { cn } from "../../../lib/utils"
|
||||
import {
|
||||
isLifetimePlanCategory,
|
||||
LIFETIME_DURATION_DEFAULT,
|
||||
SUBSCRIPTION_CURRENCIES,
|
||||
SUBSCRIPTION_DURATION_UNITS,
|
||||
SUBSCRIPTION_PLAN_CATEGORIES,
|
||||
|
|
@ -55,8 +56,12 @@ export const EMPTY_SUBSCRIPTION_PLAN_DRAFT: CreateSubscriptionPlanDraft = {
|
|||
function draftToPayload(draft: CreateSubscriptionPlanDraft): CreateSubscriptionPlanPayload | null {
|
||||
const name = draft.name.trim()
|
||||
const description = draft.description.trim()
|
||||
const duration_value = Number(draft.duration_value)
|
||||
const price = Number(draft.price)
|
||||
const isLifetime = isLifetimePlanCategory(draft.category)
|
||||
const duration_value = isLifetime
|
||||
? LIFETIME_DURATION_DEFAULT.duration_value
|
||||
: Number(draft.duration_value)
|
||||
const duration_unit = isLifetime ? LIFETIME_DURATION_DEFAULT.duration_unit : draft.duration_unit
|
||||
|
||||
if (!name) return null
|
||||
if (!description) return null
|
||||
|
|
@ -68,7 +73,7 @@ function draftToPayload(draft: CreateSubscriptionPlanDraft): CreateSubscriptionP
|
|||
description,
|
||||
category: draft.category,
|
||||
duration_value,
|
||||
duration_unit: draft.duration_unit,
|
||||
duration_unit,
|
||||
price,
|
||||
currency: draft.currency,
|
||||
is_active: draft.is_active,
|
||||
|
|
@ -96,6 +101,8 @@ export function CreateSubscriptionPlanDialog({
|
|||
}
|
||||
}, [open])
|
||||
|
||||
const isLifetime = isLifetimePlanCategory(draft.category)
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
const payload = draftToPayload(draft)
|
||||
|
|
@ -167,9 +174,19 @@ export function CreateSubscriptionPlanDialog({
|
|||
</label>
|
||||
<Select
|
||||
value={draft.category}
|
||||
onChange={(e) =>
|
||||
setDraft((d) => ({ ...d, category: e.target.value as SubscriptionPlanCategory }))
|
||||
}
|
||||
onChange={(e) => {
|
||||
const category = e.target.value as SubscriptionPlanCategory
|
||||
setDraft((d) => ({
|
||||
...d,
|
||||
category,
|
||||
...(isLifetimePlanCategory(category)
|
||||
? {
|
||||
duration_value: String(LIFETIME_DURATION_DEFAULT.duration_value),
|
||||
duration_unit: LIFETIME_DURATION_DEFAULT.duration_unit,
|
||||
}
|
||||
: {}),
|
||||
}))
|
||||
}}
|
||||
className="rounded-[6px]"
|
||||
>
|
||||
{SUBSCRIPTION_PLAN_CATEGORIES.map((opt) => (
|
||||
|
|
@ -180,43 +197,55 @@ export function CreateSubscriptionPlanDialog({
|
|||
</Select>
|
||||
</div>
|
||||
|
||||
<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">
|
||||
Duration <span className="text-destructive">*</span>
|
||||
</label>
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
step={1}
|
||||
value={draft.duration_value}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, duration_value: e.target.value }))}
|
||||
className="rounded-[6px]"
|
||||
required
|
||||
/>
|
||||
{isLifetime ? (
|
||||
<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.
|
||||
</p>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400">
|
||||
Duration unit
|
||||
</label>
|
||||
<Select
|
||||
value={draft.duration_unit}
|
||||
onChange={(e) =>
|
||||
setDraft((d) => ({
|
||||
...d,
|
||||
duration_unit: e.target.value as SubscriptionPlanDurationUnit,
|
||||
}))
|
||||
}
|
||||
className="rounded-[6px]"
|
||||
>
|
||||
{SUBSCRIPTION_DURATION_UNITS.map((opt) => (
|
||||
<option key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
) : (
|
||||
<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">
|
||||
Duration <span className="text-destructive">*</span>
|
||||
</label>
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
step={1}
|
||||
value={draft.duration_value}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, duration_value: e.target.value }))}
|
||||
className="rounded-[6px]"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400">
|
||||
Duration unit
|
||||
</label>
|
||||
<Select
|
||||
value={draft.duration_unit}
|
||||
onChange={(e) =>
|
||||
setDraft((d) => ({
|
||||
...d,
|
||||
duration_unit: e.target.value as SubscriptionPlanDurationUnit,
|
||||
}))
|
||||
}
|
||||
className="rounded-[6px]"
|
||||
>
|
||||
{SUBSCRIPTION_DURATION_UNITS.map((opt) => (
|
||||
<option key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-1.5">
|
||||
|
|
|
|||
|
|
@ -18,9 +18,10 @@ import { Select } from "../../../components/ui/select"
|
|||
import { SpinnerIcon } from "../../../components/ui/spinner-icon"
|
||||
import { Textarea } from "../../../components/ui/textarea"
|
||||
import { ToggleSwitch } from "../../../components/ui/toggle-switch"
|
||||
import { cn } from "../../../lib/utils"
|
||||
import {
|
||||
formatPlanCategory,
|
||||
isLifetimePlanCategory,
|
||||
LIFETIME_DURATION_DEFAULT,
|
||||
SUBSCRIPTION_CURRENCIES,
|
||||
SUBSCRIPTION_DURATION_UNITS,
|
||||
} from "../../../lib/subscriptionPlans"
|
||||
|
|
@ -52,11 +53,18 @@ function planToDraft(plan: SubscriptionPlan): EditDraft {
|
|||
}
|
||||
}
|
||||
|
||||
function draftToPayload(draft: EditDraft): UpdateSubscriptionPlanPayload | null {
|
||||
function draftToPayload(
|
||||
draft: EditDraft,
|
||||
category: string,
|
||||
): UpdateSubscriptionPlanPayload | null {
|
||||
const name = draft.name.trim()
|
||||
const description = draft.description.trim()
|
||||
const duration_value = Number(draft.duration_value)
|
||||
const price = Number(draft.price)
|
||||
const isLifetime = isLifetimePlanCategory(category)
|
||||
const duration_value = isLifetime
|
||||
? LIFETIME_DURATION_DEFAULT.duration_value
|
||||
: Number(draft.duration_value)
|
||||
const duration_unit = isLifetime ? LIFETIME_DURATION_DEFAULT.duration_unit : draft.duration_unit
|
||||
|
||||
if (!name) return null
|
||||
if (!description) return null
|
||||
|
|
@ -67,7 +75,7 @@ function draftToPayload(draft: EditDraft): UpdateSubscriptionPlanPayload | null
|
|||
name,
|
||||
description,
|
||||
duration_value,
|
||||
duration_unit: draft.duration_unit,
|
||||
duration_unit,
|
||||
price,
|
||||
currency: draft.currency,
|
||||
is_active: draft.is_active,
|
||||
|
|
@ -112,7 +120,7 @@ export function EditSubscriptionPlanDialog({
|
|||
e.preventDefault()
|
||||
if (!plan || !draft) return
|
||||
|
||||
const payload = draftToPayload(draft)
|
||||
const payload = draftToPayload(draft, plan.category)
|
||||
if (!payload) {
|
||||
toast.error("Please fill in all required fields with valid values.")
|
||||
return
|
||||
|
|
@ -187,49 +195,63 @@ export function EditSubscriptionPlanDialog({
|
|||
/>
|
||||
</div>
|
||||
|
||||
<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">
|
||||
Duration <span className="text-destructive">*</span>
|
||||
</label>
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
step={1}
|
||||
value={draft.duration_value}
|
||||
onChange={(e) =>
|
||||
setDraft((d) => d && { ...d, duration_value: e.target.value })
|
||||
}
|
||||
className="rounded-[6px]"
|
||||
required
|
||||
/>
|
||||
{isLifetimePlanCategory(plan.category) ? (
|
||||
<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.
|
||||
</p>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400">
|
||||
Duration unit
|
||||
</label>
|
||||
<Select
|
||||
value={draft.duration_unit}
|
||||
onChange={(e) =>
|
||||
setDraft((d) =>
|
||||
d
|
||||
? {
|
||||
...d,
|
||||
duration_unit: e.target.value as SubscriptionPlanDurationUnit,
|
||||
}
|
||||
: d,
|
||||
)
|
||||
}
|
||||
className="rounded-[6px]"
|
||||
>
|
||||
{SUBSCRIPTION_DURATION_UNITS.map((opt) => (
|
||||
<option key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
) : (
|
||||
<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">
|
||||
Duration <span className="text-destructive">*</span>
|
||||
</label>
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
step={1}
|
||||
value={draft.duration_value}
|
||||
onChange={(e) =>
|
||||
setDraft((d) => d && { ...d, duration_value: e.target.value })
|
||||
}
|
||||
className="rounded-[6px]"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400">
|
||||
Duration unit
|
||||
</label>
|
||||
<Select
|
||||
value={draft.duration_unit}
|
||||
onChange={(e) =>
|
||||
setDraft((d) =>
|
||||
d
|
||||
? {
|
||||
...d,
|
||||
duration_unit: e.target.value as SubscriptionPlanDurationUnit,
|
||||
}
|
||||
: d,
|
||||
)
|
||||
}
|
||||
className="rounded-[6px]"
|
||||
>
|
||||
{SUBSCRIPTION_DURATION_UNITS.map((opt) => (
|
||||
<option key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-1.5">
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import {
|
|||
isLearnerRole,
|
||||
SUBSCRIPTION_ADMIN_PERMISSIONS,
|
||||
} from "../../../lib/subscriptionAdminPermissions"
|
||||
import { formatPlanCategory } from "../../../lib/subscriptionPlans"
|
||||
import { formatPlanCategory, isLifetimeExpiry, isLifetimePlanCategory } from "../../../lib/subscriptionPlans"
|
||||
import { cn } from "../../../lib/utils"
|
||||
import type { UserSubscriptionRecord, UserSubscriptionsData } from "../../../types/userAdmin.types"
|
||||
import { CancelSubscriptionDialog } from "./CancelSubscriptionDialog"
|
||||
|
|
@ -29,6 +29,7 @@ import { GrantSubscriptionDialog } from "./GrantSubscriptionDialog"
|
|||
function formatPlanTitle(subscription: UserSubscriptionRecord): string {
|
||||
const name = subscription.plan_name?.trim()
|
||||
if (name) return name
|
||||
if (isLifetimePlanCategory(subscription.plan_category)) 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`
|
||||
|
|
@ -37,7 +38,8 @@ function formatPlanTitle(subscription: UserSubscriptionRecord): string {
|
|||
return NOT_ASSIGNED_LABEL
|
||||
}
|
||||
|
||||
function formatExpiryDate(value?: string | null): string {
|
||||
function formatExpiryDate(value?: string | null, category?: string | null): string {
|
||||
if (isLifetimePlanCategory(category) || isLifetimeExpiry(value)) return "Never expires"
|
||||
if (!value?.trim()) return NOT_ASSIGNED_LABEL
|
||||
const parsed = new Date(value)
|
||||
if (Number.isNaN(parsed.getTime())) return NOT_ASSIGNED_LABEL
|
||||
|
|
@ -61,7 +63,8 @@ function formatDateTime(value?: string | null): string {
|
|||
})
|
||||
}
|
||||
|
||||
function getDaysLeft(expiresAt?: string | null): number | null {
|
||||
function getDaysLeft(expiresAt?: string | null, category?: string | null): number | null {
|
||||
if (isLifetimePlanCategory(category) || isLifetimeExpiry(expiresAt)) return null
|
||||
if (!expiresAt?.trim()) return null
|
||||
const expires = new Date(expiresAt)
|
||||
if (Number.isNaN(expires.getTime())) return null
|
||||
|
|
@ -69,6 +72,10 @@ function getDaysLeft(expiresAt?: string | null): number | null {
|
|||
return Math.ceil(ms / 86_400_000)
|
||||
}
|
||||
|
||||
function canExtendSubscription(subscription: UserSubscriptionRecord): boolean {
|
||||
return !isLifetimePlanCategory(subscription.plan_category) && !isLifetimeExpiry(subscription.expires_at)
|
||||
}
|
||||
|
||||
function formatDaysLeftLabel(daysLeft: number): string {
|
||||
if (daysLeft <= 0) return "Expired"
|
||||
if (daysLeft === 1) return "1 day left"
|
||||
|
|
@ -138,11 +145,12 @@ function SubscriptionManageCard({
|
|||
onGrant: () => void
|
||||
onCancel: () => void
|
||||
}) {
|
||||
const daysLeft = getDaysLeft(subscription.expires_at)
|
||||
const daysLeft = getDaysLeft(subscription.expires_at, subscription.plan_category)
|
||||
const statusUpper = subscription.status.toUpperCase()
|
||||
const isActive = subscription.is_currently_active || statusUpper === "ACTIVE"
|
||||
const statusTone = isActive ? "active" : statusUpper === "PENDING" ? "pending" : "inactive"
|
||||
const extendEnabled = canExtend && hasActiveSubscription && isActive
|
||||
const isLifetime = !canExtendSubscription(subscription)
|
||||
const extendEnabled = canExtend && hasActiveSubscription && isActive && !isLifetime
|
||||
const cancelEnabled = canCancel && hasActiveSubscription && isActive && statusUpper !== "CANCELLED"
|
||||
const markAsPaidEnabled = canGrant && !hasActiveSubscription
|
||||
|
||||
|
|
@ -168,7 +176,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)}
|
||||
{formatExpiryDate(subscription.expires_at, subscription.plan_category)}
|
||||
</p>
|
||||
{daysLeft != null ? (
|
||||
<span
|
||||
|
|
@ -190,7 +198,7 @@ function SubscriptionManageCard({
|
|||
</div>
|
||||
|
||||
<div className="mt-6 flex flex-col gap-2">
|
||||
{canExtend ? (
|
||||
{canExtend && !isLifetime ? (
|
||||
<Button
|
||||
type="button"
|
||||
className="h-10 w-full rounded-xl bg-brand-500 text-sm font-semibold text-white hover:bg-brand-600 disabled:pointer-events-auto disabled:cursor-not-allowed disabled:opacity-50"
|
||||
|
|
@ -469,7 +477,7 @@ export function UserSubscriptionsSection({
|
|||
</p>
|
||||
<p className="text-xs text-grayScale-500">
|
||||
{formatPlanCategory(subscription.plan_category)} ·{" "}
|
||||
{formatExpiryDate(subscription.expires_at)}
|
||||
{formatExpiryDate(subscription.expires_at, subscription.plan_category)}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
|
|
@ -481,7 +489,7 @@ export function UserSubscriptionsSection({
|
|||
: "inactive"
|
||||
}
|
||||
/>
|
||||
{canExtend ? (
|
||||
{canExtend && canExtendSubscription(subscription) ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user