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 (
-
- Payment recorded: {formatPlanPrice(selectedPlan)}. -
) : null} ++ Payment * +
+{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 */