From de9766be7ba406a5fc9f92c90806e1d1c6364446 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Mon, 3 Aug 2026 10:34:02 -0700 Subject: [PATCH] Add optional payment choice for admin grant and extend subscriptions. Co-authored-by: Cursor --- src/api/admin-subscriptions.api.ts | 2 + .../components/ExtendSubscriptionDialog.tsx | 223 +++++++++++++++--- .../components/GrantSubscriptionDialog.tsx | 90 ++++++- .../components/UserSubscriptionsSection.tsx | 6 +- src/types/adminSubscription.types.ts | 5 + 5 files changed, 283 insertions(+), 43 deletions(-) diff --git a/src/api/admin-subscriptions.api.ts b/src/api/admin-subscriptions.api.ts index a62730a..0c4945e 100644 --- a/src/api/admin-subscriptions.api.ts +++ b/src/api/admin-subscriptions.api.ts @@ -55,6 +55,8 @@ function mutationResult(res: { data: unknown; status?: number }) { * POST /admin/users/:user_id/subscriptions/extend * Unified grant-or-extend: no active sub in plan category → grant (201); * active in category → extend one period of selected plan (200). + * Optional body.record_payment (default true) controls whether a SUCCESS + * payment at the plan price is recorded. */ export const adminApplySubscription = (userId: number, body: AdminApplySubscriptionBody) => http diff --git a/src/pages/user-management/components/ExtendSubscriptionDialog.tsx b/src/pages/user-management/components/ExtendSubscriptionDialog.tsx index c2ab4dc..82039b5 100644 --- a/src/pages/user-management/components/ExtendSubscriptionDialog.tsx +++ b/src/pages/user-management/components/ExtendSubscriptionDialog.tsx @@ -1,9 +1,20 @@ import { useEffect, useState } from "react" +import { RefreshCw } from "lucide-react" import { toast } from "sonner" import { adminApplySubscription } from "../../../api/admin-subscriptions.api" +import { Button } from "../../../components/ui/button" +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "../../../components/ui/dialog" import { notifyApiError } from "../../../lib/apiErrors" -import { formatPlanCategory } from "../../../lib/subscriptionPlans" +import { formatPlanCategory, formatPlanPrice } from "../../../lib/subscriptionPlans" import { TypeToConfirmDialog } from "../../../lib/typeToConfirm" +import { cn } from "../../../lib/utils" import type { UserSubscriptionRecord } from "../../../types/userAdmin.types" const CONFIRM_WORD = "EXTEND" @@ -36,12 +47,24 @@ export function ExtendSubscriptionDialog({ subscription, onExtended, }: ExtendSubscriptionDialogProps) { + const [recordPayment, setRecordPayment] = useState(true) + const [confirmOpen, setConfirmOpen] = useState(false) const [saving, setSaving] = useState(false) useEffect(() => { - if (!open) setSaving(false) + if (!open) { + setSaving(false) + setConfirmOpen(false) + setRecordPayment(true) + } }, [open, subscription?.id]) + const handleContinue = (e: React.FormEvent) => { + e.preventDefault() + if (!subscription) return + setConfirmOpen(true) + } + const handleConfirmExtend = async () => { if (!subscription) return @@ -49,6 +72,7 @@ export function ExtendSubscriptionDialog({ try { const res = await adminApplySubscription(userId, { plan_id: subscription.plan_id, + record_payment: recordPayment, }) const expiresLabel = res.data?.expires_at ? formatDateTime(res.data.expires_at) @@ -58,6 +82,7 @@ export function ExtendSubscriptionDialog({ ? `Subscription extended to ${expiresLabel}` : res.message || "Subscription extended", ) + setConfirmOpen(false) onExtended() onOpenChange(false) } catch (err) { @@ -67,33 +92,173 @@ export function ExtendSubscriptionDialog({ } } + const priceLabel = subscription + ? formatPlanPrice({ + price: subscription.price, + currency: subscription.currency || "ETB", + }) + : "the plan price" + return ( - { - if (saving) return - if (!next) onOpenChange(false) - }} - title="Are you sure?" - description={ - subscription ? ( - <> - Extend {subscription.plan_name} (# - {subscription.id}) by one plan period? Current expiry:{" "} - {formatDateTime(subscription.expires_at)} - {subscription.plan_category - ? ` · ${formatPlanCategory(subscription.plan_category)}` - : ""} - . - - ) : ( - "Extend this subscription by one plan period?" - ) - } - confirmWord={CONFIRM_WORD} - confirmLabel="Extend subscription" - confirming={saving} - onConfirm={handleConfirmExtend} - /> + <> + { + if (saving) return + if (!next) { + setConfirmOpen(false) + onOpenChange(false) + } + }} + > + +
+ + + + Extend subscription + + + Extend by one plan period. Choose whether to record a payment at the plan + price. + + + +
+ {subscription ? ( +
+

+ {subscription.plan_name} + {subscription.plan_category + ? ` · ${formatPlanCategory(subscription.plan_category)}` + : ""} +

+

+ Current expiry: {formatDateTime(subscription.expires_at)} + {" · Plan price: "} + {formatPlanPrice({ + price: subscription.price, + currency: subscription.currency || "ETB", + })} +

+
+ ) : null} + +
+

+ Payment * +

+
+ + +
+
+
+ + + + + +
+
+
+ + + Extend {subscription.plan_name} (#{subscription.id}) by one + plan period? Current expiry:{" "} + {formatDateTime(subscription.expires_at)} + {subscription.plan_category + ? ` · ${formatPlanCategory(subscription.plan_category)}` + : ""} + . + {recordPayment ? ( + <> + {" "} + A payment of {priceLabel} will be recorded. + + ) : ( + " No payment will be recorded." + )} + + ) : ( + "Extend this subscription by one plan period?" + ) + } + confirmWord={CONFIRM_WORD} + confirmLabel={recordPayment ? "Extend and record payment" : "Extend without payment"} + confirming={saving} + onConfirm={handleConfirmExtend} + /> + ) } diff --git a/src/pages/user-management/components/GrantSubscriptionDialog.tsx b/src/pages/user-management/components/GrantSubscriptionDialog.tsx index 1042ed1..32578cb 100644 --- a/src/pages/user-management/components/GrantSubscriptionDialog.tsx +++ b/src/pages/user-management/components/GrantSubscriptionDialog.tsx @@ -52,6 +52,7 @@ export function GrantSubscriptionDialog({ const [planId, setPlanId] = useState("") const [planSearch, setPlanSearch] = useState("") const [planMenuOpen, setPlanMenuOpen] = useState(false) + const [recordPayment, setRecordPayment] = useState(true) const [confirmOpen, setConfirmOpen] = useState(false) const [saving, setSaving] = useState(false) const [formError, setFormError] = useState(null) @@ -61,6 +62,7 @@ export function GrantSubscriptionDialog({ setPlanId("") setPlanSearch("") setPlanMenuOpen(false) + setRecordPayment(true) setConfirmOpen(false) setFormError(null) setSaving(false) @@ -131,7 +133,10 @@ export function GrantSubscriptionDialog({ setSaving(true) try { - const res = await adminApplySubscription(userId, { plan_id: id }) + const res = await adminApplySubscription(userId, { + plan_id: id, + record_payment: recordPayment, + }) toast.success(res.message || "Subscription granted") setConfirmOpen(false) onGranted() @@ -163,12 +168,12 @@ export function GrantSubscriptionDialog({ - Mark as paid + Grant subscription Grant plan access - {userName ? ` to ${userName}` : ""}. A payment is recorded at the - plan price. + {userName ? ` to ${userName}` : ""}. Choose whether to record a + payment at the plan price. @@ -270,13 +275,70 @@ export function GrantSubscriptionDialog({ An active {formatPlanCategory(selectedPlan.category)} subscription already exists. Extend or cancel it instead.

- ) : selectedPlan ? ( -

- Payment recorded: {formatPlanPrice(selectedPlan)}. -

) : null} +
+

+ Payment * +

+
+ + +
+
+ {formError ?

{formError}

: null} @@ -319,15 +381,21 @@ export function GrantSubscriptionDialog({ ) : ( " to this learner" )} - ? A payment of {formatPlanPrice(selectedPlan)} will be - recorded. + {recordPayment ? ( + <> + ? A payment of {formatPlanPrice(selectedPlan)} will be + recorded. + + ) : ( + "? No payment will be recorded." + )} ) : ( "Grant this subscription?" ) } confirmWord={CONFIRM_WORD} - confirmLabel="Mark as paid" + confirmLabel={recordPayment ? "Grant and record payment" : "Grant without payment"} confirming={saving} onConfirm={handleConfirmGrant} /> diff --git a/src/pages/user-management/components/UserSubscriptionsSection.tsx b/src/pages/user-management/components/UserSubscriptionsSection.tsx index 731080f..67a9f34 100644 --- a/src/pages/user-management/components/UserSubscriptionsSection.tsx +++ b/src/pages/user-management/components/UserSubscriptionsSection.tsx @@ -230,12 +230,12 @@ function SubscriptionManageCard({ disabled={!markAsPaidEnabled} title={ !markAsPaidEnabled - ? "Mark as paid is unavailable while this learner already has an active subscription." + ? "Grant is unavailable while this learner already has an active subscription." : undefined } onClick={onGrant} > - Mark as Paid + Grant Subscription ) : null} {canCancel ? ( @@ -310,7 +310,7 @@ function EmptySubscriptionCard({ className="h-10 min-w-0 flex-1 rounded-xl bg-brand-500 text-sm font-semibold text-white hover:bg-brand-600" onClick={onGrant} > - Mark as Paid + Grant Subscription ) : null} {canCancel ? ( diff --git a/src/types/adminSubscription.types.ts b/src/types/adminSubscription.types.ts index 3636c59..fe09d1b 100644 --- a/src/types/adminSubscription.types.ts +++ b/src/types/adminSubscription.types.ts @@ -21,6 +21,11 @@ export interface AdminSubscriptionRes { /** Body for unified grant-or-extend. */ export interface AdminApplySubscriptionBody { plan_id: number + /** + * When true (default if omitted), records a SUCCESS payment at the plan price. + * When false, grants/extends access with no payments row. + */ + record_payment?: boolean } /** @deprecated Prefer AdminApplySubscriptionBody */