mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-08-03 21:17:44 +00:00
feat(user): add admin topup functionality in user details page ✨
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 6m16s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 6m16s
This commit is contained in:
@ -24,7 +24,8 @@ export async function createPayment(data: NewPayment) {
|
|||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
console.log("data", data);
|
console.log("data", data);
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${process.env.SARLINK_API_BASE_URL // });
|
`${
|
||||||
|
process.env.SARLINK_API_BASE_URL // });
|
||||||
}/api/billing/payment/`,
|
}/api/billing/payment/`,
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
@ -224,3 +224,56 @@ export async function updateUserAgreement(
|
|||||||
message: "User agreement updated successfully",
|
message: "User agreement updated successfully",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type AddTopupFormState = {
|
||||||
|
status: boolean;
|
||||||
|
message: string;
|
||||||
|
fieldErrors?: {
|
||||||
|
amount?: string[];
|
||||||
|
};
|
||||||
|
payload?: FormData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function adminUserTopup(
|
||||||
|
_prevState: AddTopupFormState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<AddTopupFormState> {
|
||||||
|
const user_id = formData.get("user_id") as string;
|
||||||
|
const amount = formData.get("amount") as string;
|
||||||
|
const session = await getServerSession(authOptions);
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
`${process.env.SARLINK_API_BASE_URL}/api/billing/admin-topup/`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Token ${session?.apiToken}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
amount: Number.parseInt(amount),
|
||||||
|
user_id: Number.parseInt(user_id),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json();
|
||||||
|
return {
|
||||||
|
status: false,
|
||||||
|
message:
|
||||||
|
errorData.message ||
|
||||||
|
errorData.detail ||
|
||||||
|
"An error occurred while topping up the user.",
|
||||||
|
fieldErrors: {},
|
||||||
|
payload: formData,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/users/[userId]/topup", "page");
|
||||||
|
return {
|
||||||
|
status: true,
|
||||||
|
message: "User topped up successfully",
|
||||||
|
fieldErrors: {},
|
||||||
|
payload: formData,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@ import { EyeIcon, FileTextIcon, PencilIcon } from "lucide-react";
|
|||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
|
import AddTopupDialogForm from "@/components/admin/admin-topup-form";
|
||||||
import ClientErrorMessage from "@/components/client-error-message";
|
import ClientErrorMessage from "@/components/client-error-message";
|
||||||
import InputReadOnly from "@/components/input-read-only";
|
import InputReadOnly from "@/components/input-read-only";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
@ -49,6 +50,7 @@ export default async function VerifyUserPage({
|
|||||||
<div className="flex flex-wrap gap-2">
|
<div className="flex flex-wrap gap-2">
|
||||||
{dbUser && !dbUser?.verified && <UserVerifyDialog user={dbUser} />}
|
{dbUser && !dbUser?.verified && <UserVerifyDialog user={dbUser} />}
|
||||||
{dbUser && !dbUser?.verified && <UserRejectDialog user={dbUser} />}
|
{dbUser && !dbUser?.verified && <UserRejectDialog user={dbUser} />}
|
||||||
|
<AddTopupDialogForm user_id={userId} />
|
||||||
<Link href={"update"}>
|
<Link href={"update"}>
|
||||||
<Button className="hover:cursor-pointer">
|
<Button className="hover:cursor-pointer">
|
||||||
<PencilIcon />
|
<PencilIcon />
|
112
components/admin/admin-topup-form.tsx
Normal file
112
components/admin/admin-topup-form.tsx
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Loader2, PlusCircle } from "lucide-react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { useActionState, useEffect, useState } from "react"; // Import useActionState
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { adminUserTopup } from "@/actions/user-actions";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from "@/components/ui/dialog";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
|
||||||
|
export type AddTopupFormState = {
|
||||||
|
status: boolean;
|
||||||
|
message: string;
|
||||||
|
fieldErrors?: {
|
||||||
|
amount?: string[];
|
||||||
|
};
|
||||||
|
payload?: FormData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const initialState: AddTopupFormState = {
|
||||||
|
message: "",
|
||||||
|
fieldErrors: {},
|
||||||
|
status: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function AddTopupDialogForm({ user_id }: { user_id?: string }) {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const router = useRouter();
|
||||||
|
const [state, formAction, pending] = useActionState(
|
||||||
|
adminUserTopup,
|
||||||
|
initialState,
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (state.message && state !== initialState) {
|
||||||
|
if (state.fieldErrors && Object.keys(state.fieldErrors).length > 0) {
|
||||||
|
toast.error(state.message);
|
||||||
|
} else if (state.status) {
|
||||||
|
setOpen(false);
|
||||||
|
toast.success(state.message);
|
||||||
|
router.push("/user-topups?page=1");
|
||||||
|
} else {
|
||||||
|
toast.error(state.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [state, router]);
|
||||||
|
|
||||||
|
if (!user_id) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
|
<DialogTrigger asChild>
|
||||||
|
<Button
|
||||||
|
className="gap-2 items-center"
|
||||||
|
disabled={pending}
|
||||||
|
variant="default"
|
||||||
|
>
|
||||||
|
Add cash topup
|
||||||
|
<PlusCircle size={16} />
|
||||||
|
</Button>
|
||||||
|
</DialogTrigger>
|
||||||
|
<DialogContent className="sm:max-w-[425px]">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>New Manual Topup</DialogTitle>
|
||||||
|
<DialogDescription>
|
||||||
|
To add a new manual topup, enter the amount below. Click save when
|
||||||
|
you are done.
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<form action={formAction}>
|
||||||
|
<div className="grid gap-4 py-4">
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<div className="flex flex-col gap-3">
|
||||||
|
<Label htmlFor="device_name">Topup Amount</Label>
|
||||||
|
<input type="hidden" name="user_id" value={user_id} />
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
defaultValue={(state?.payload?.get("amount") || "") as string}
|
||||||
|
name="amount"
|
||||||
|
id="topup_amount"
|
||||||
|
className="col-span-3"
|
||||||
|
/>
|
||||||
|
{state.fieldErrors?.amount && (
|
||||||
|
<span className="text-red-500 text-sm">
|
||||||
|
{state.fieldErrors.amount[0]}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<DialogFooter>
|
||||||
|
<Button disabled={pending} type="submit">
|
||||||
|
{pending ? <Loader2 className="animate-spin" /> : "Save"}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</form>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
@ -40,17 +40,17 @@ type Categories = {
|
|||||||
id: string;
|
id: string;
|
||||||
children: (
|
children: (
|
||||||
| {
|
| {
|
||||||
title: string;
|
title: string;
|
||||||
link: string;
|
link: string;
|
||||||
perm_identifier: string;
|
perm_identifier: string;
|
||||||
icon: React.JSX.Element;
|
icon: React.JSX.Element;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
title: string;
|
title: string;
|
||||||
link: string;
|
link: string;
|
||||||
icon: React.JSX.Element;
|
icon: React.JSX.Element;
|
||||||
perm_identifier?: undefined;
|
perm_identifier?: undefined;
|
||||||
}
|
}
|
||||||
)[];
|
)[];
|
||||||
}[];
|
}[];
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ export async function UsersTable({
|
|||||||
|
|
||||||
<TableCell>{user.mobile}</TableCell>
|
<TableCell>{user.mobile}</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<Link href={`/users/${user.id}/verify`}>
|
<Link href={`/users/${user.id}/details`}>
|
||||||
<Button>Details</Button>
|
<Button>Details</Button>
|
||||||
</Link>
|
</Link>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
|
@ -2,14 +2,14 @@ import { Calendar } from "lucide-react";
|
|||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import {
|
import {
|
||||||
Table,
|
Table,
|
||||||
TableBody,
|
TableBody,
|
||||||
TableCaption,
|
TableCaption,
|
||||||
TableCell,
|
TableCell,
|
||||||
TableFooter,
|
TableFooter,
|
||||||
TableHead,
|
TableHead,
|
||||||
TableHeader,
|
TableHeader,
|
||||||
TableRow,
|
TableRow,
|
||||||
} from "@/components/ui/table";
|
} from "@/components/ui/table";
|
||||||
import type { WalletTransaction } from "@/lib/backend-types";
|
import type { WalletTransaction } from "@/lib/backend-types";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
@ -20,232 +20,224 @@ import { Badge } from "./ui/badge";
|
|||||||
import { Button } from "./ui/button";
|
import { Button } from "./ui/button";
|
||||||
|
|
||||||
export async function WalletTransactionsTable({
|
export async function WalletTransactionsTable({
|
||||||
searchParams,
|
searchParams,
|
||||||
}: {
|
}: {
|
||||||
searchParams: Promise<{
|
searchParams: Promise<{
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}>;
|
}>;
|
||||||
}) {
|
}) {
|
||||||
const resolvedParams = await searchParams;
|
const resolvedParams = await searchParams;
|
||||||
const page = Number.parseInt(resolvedParams.page as string) || 1;
|
const page = Number.parseInt(resolvedParams.page as string) || 1;
|
||||||
const limit = 10;
|
const limit = 10;
|
||||||
const offset = (page - 1) * limit;
|
const offset = (page - 1) * limit;
|
||||||
// Build params object
|
// Build params object
|
||||||
const apiParams: Record<string, string | number | undefined> = {};
|
const apiParams: Record<string, string | number | undefined> = {};
|
||||||
for (const [key, value] of Object.entries(resolvedParams)) {
|
for (const [key, value] of Object.entries(resolvedParams)) {
|
||||||
if (value !== undefined && value !== "") {
|
if (value !== undefined && value !== "") {
|
||||||
apiParams[key] = typeof value === "number" ? value : String(value);
|
apiParams[key] = typeof value === "number" ? value : String(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
apiParams.limit = limit;
|
apiParams.limit = limit;
|
||||||
apiParams.offset = offset;
|
apiParams.offset = offset;
|
||||||
const [error, transactions] = await tryCatch(
|
const [error, transactions] = await tryCatch(
|
||||||
getWaleltTransactions(apiParams),
|
getWaleltTransactions(apiParams),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
if (error.message.includes("Unauthorized")) {
|
if (error.message.includes("Unauthorized")) {
|
||||||
redirect("/auth/signin");
|
redirect("/auth/signin");
|
||||||
} else {
|
} else {
|
||||||
return <pre>{JSON.stringify(error, null, 2)}</pre>;
|
return <pre>{JSON.stringify(error, null, 2)}</pre>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const { data, meta } = transactions;
|
const { data, meta } = transactions;
|
||||||
const totalDebit = data.reduce(
|
const totalDebit = data.reduce(
|
||||||
(acc, trx) =>
|
(acc, trx) => acc + (trx.transaction_type === "DEBIT" ? trx.amount : 0),
|
||||||
acc + (trx.transaction_type === "DEBIT" ? trx.amount : 0),
|
0,
|
||||||
0,
|
);
|
||||||
);
|
const totalCredit = data.reduce(
|
||||||
const totalCredit = data.reduce(
|
(acc, trx) => acc + (trx.transaction_type === "TOPUP" ? trx.amount : 0),
|
||||||
(acc, trx) =>
|
0,
|
||||||
acc + (trx.transaction_type === "TOPUP" ? trx.amount : 0),
|
);
|
||||||
0,
|
return (
|
||||||
);
|
<div>
|
||||||
return (
|
{data?.length === 0 ? (
|
||||||
<div>
|
<div className="h-[calc(100svh-400px)] flex flex-col items-center justify-center my-4">
|
||||||
{data?.length === 0 ? (
|
<h3>No transactions yet.</h3>
|
||||||
<div className="h-[calc(100svh-400px)] flex flex-col items-center justify-center my-4">
|
</div>
|
||||||
<h3>No transactions yet.</h3>
|
) : (
|
||||||
</div>
|
<div>
|
||||||
) : (
|
<div className="flex gap-4 mb-4 w-full">
|
||||||
<div>
|
<div className="bg-red-400 w-full sm:w-fit dark:bg-red-950 dark:text-red-400 text-red-900 p-2 px-4 rounded-md mb-2">
|
||||||
<div className="flex gap-4 mb-4 w-full">
|
<h5 className="text-lg font-semibold">Total Debit</h5>
|
||||||
<div className="bg-red-400 w-full sm:w-fit dark:bg-red-950 dark:text-red-400 text-red-900 p-2 px-4 rounded-md mb-2">
|
<p>{totalDebit.toFixed(2)} MVR</p>
|
||||||
<h5 className="text-lg font-semibold">
|
</div>
|
||||||
Total Debit
|
<div className="bg-green-400 w-full sm:w-fit dark:bg-green-950 dark:text-green-400 text-green-900 p-2 px-4 rounded-md mb-2">
|
||||||
</h5>
|
<h5 className="text-lg font-semibold">Total Credit</h5>
|
||||||
<p>{totalDebit.toFixed(2)} MVR</p>
|
<p>{totalCredit.toFixed(2)} MVR</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="bg-green-400 w-full sm:w-fit dark:bg-green-950 dark:text-green-400 text-green-900 p-2 px-4 rounded-md mb-2">
|
</div>
|
||||||
<h5 className="text-lg font-semibold">
|
<div className="hidden sm:block">
|
||||||
Total Credit
|
<Table className="overflow-scroll">
|
||||||
</h5>
|
<TableCaption>Table of all transactions.</TableCaption>
|
||||||
<p>{totalCredit.toFixed(2)} MVR</p>
|
<TableHeader>
|
||||||
</div>
|
<TableRow>
|
||||||
</div>
|
<TableHead>Description</TableHead>
|
||||||
<div className="hidden sm:block">
|
<TableHead>Amount</TableHead>
|
||||||
|
<TableHead>Transaction Type</TableHead>
|
||||||
|
<TableHead>View Details</TableHead>
|
||||||
|
<TableHead>Created at</TableHead>
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody className="overflow-scroll">
|
||||||
|
{transactions?.data?.map((trx) => (
|
||||||
|
<TableRow
|
||||||
|
className={cn(
|
||||||
|
"items-start border rounded p-2",
|
||||||
|
trx?.transaction_type === "TOPUP"
|
||||||
|
? "credit-bg"
|
||||||
|
: "debit-bg",
|
||||||
|
)}
|
||||||
|
key={trx.id}
|
||||||
|
>
|
||||||
|
<TableCell>
|
||||||
|
<span className="text-muted-foreground">
|
||||||
|
{trx.description}
|
||||||
|
</span>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>{trx.amount.toFixed(2)} MVR</TableCell>
|
||||||
|
|
||||||
<Table className="overflow-scroll">
|
<TableCell>
|
||||||
<TableCaption>Table of all transactions.</TableCaption>
|
<span className="font-semibold pr-2">
|
||||||
<TableHeader>
|
{trx.transaction_type === "TOPUP" ? (
|
||||||
<TableRow>
|
<Badge className="bg-green-100 dark:bg-green-700">
|
||||||
<TableHead>Description</TableHead>
|
{trx.transaction_type}
|
||||||
<TableHead>Amount</TableHead>
|
</Badge>
|
||||||
<TableHead>Transaction Type</TableHead>
|
) : (
|
||||||
<TableHead>View Details</TableHead>
|
<Badge className="bg-red-500 dark:bg-red-700">
|
||||||
<TableHead>Created at</TableHead>
|
{trx.transaction_type}
|
||||||
</TableRow>
|
</Badge>
|
||||||
</TableHeader>
|
)}
|
||||||
<TableBody className="overflow-scroll">
|
</span>
|
||||||
{transactions?.data?.map((trx) => (
|
</TableCell>
|
||||||
<TableRow
|
|
||||||
className={cn(
|
|
||||||
"items-start border rounded p-2",
|
|
||||||
trx?.transaction_type === "TOPUP"
|
|
||||||
? "credit-bg"
|
|
||||||
: "debit-bg",
|
|
||||||
)}
|
|
||||||
key={trx.id}
|
|
||||||
>
|
|
||||||
<TableCell>
|
|
||||||
<span className="text-muted-foreground">
|
|
||||||
{trx.description}
|
|
||||||
</span>
|
|
||||||
</TableCell>
|
|
||||||
<TableCell>{trx.amount.toFixed(2)} MVR</TableCell>
|
|
||||||
|
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<span className="font-semibold pr-2">
|
<span className="">
|
||||||
{trx.transaction_type === "TOPUP" ? (
|
{new Date(trx.created_at).toLocaleDateString("en-US", {
|
||||||
<Badge className="bg-green-100 dark:bg-green-700">
|
month: "short",
|
||||||
{trx.transaction_type}
|
day: "2-digit",
|
||||||
</Badge>
|
year: "numeric",
|
||||||
) : (
|
minute: "2-digit",
|
||||||
<Badge className="bg-red-500 dark:bg-red-700">
|
hour: "2-digit",
|
||||||
{trx.transaction_type}
|
})}
|
||||||
</Badge>
|
</span>
|
||||||
)}
|
</TableCell>
|
||||||
</span>
|
<TableCell>
|
||||||
</TableCell>
|
<Button>
|
||||||
|
<Link
|
||||||
<TableCell>
|
className="font-medium "
|
||||||
<span className="">
|
href={
|
||||||
{new Date(trx.created_at).toLocaleDateString("en-US", {
|
trx.transaction_type === "TOPUP"
|
||||||
month: "short",
|
? `/top-ups/${trx.reference_id}`
|
||||||
day: "2-digit",
|
: `/payments/${trx.reference_id}`
|
||||||
year: "numeric",
|
}
|
||||||
minute: "2-digit",
|
>
|
||||||
hour: "2-digit",
|
View Details
|
||||||
})}
|
</Link>
|
||||||
</span>
|
</Button>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
</TableRow>
|
||||||
<Button>
|
))}
|
||||||
<Link
|
</TableBody>
|
||||||
className="font-medium "
|
<TableFooter>
|
||||||
href={
|
<TableRow>
|
||||||
trx.transaction_type === "TOPUP"
|
<TableCell colSpan={5} className="text-muted-foreground">
|
||||||
? `/top-ups/${trx.reference_id}`
|
{meta?.total === 1 ? (
|
||||||
: `/payments/${trx.reference_id}`
|
<p className="text-center">
|
||||||
}
|
Total {meta?.total} transaction.
|
||||||
>
|
</p>
|
||||||
View Details
|
) : (
|
||||||
</Link>
|
<p className="text-center">
|
||||||
</Button>
|
Total {meta?.total} transactions.
|
||||||
</TableCell>
|
</p>
|
||||||
</TableRow>
|
)}
|
||||||
))}
|
</TableCell>
|
||||||
</TableBody>
|
</TableRow>
|
||||||
<TableFooter>
|
</TableFooter>
|
||||||
<TableRow>
|
</Table>
|
||||||
<TableCell colSpan={5} className="text-muted-foreground">
|
</div>
|
||||||
{meta?.total === 1 ? (
|
<div className="sm:hidden block">
|
||||||
<p className="text-center">
|
{data.map((trx) => (
|
||||||
Total {meta?.total} transaction.
|
<MobileTransactionDetails key={trx.id} trx={trx} />
|
||||||
</p>
|
))}
|
||||||
) : (
|
</div>
|
||||||
<p className="text-center">
|
<Pagination
|
||||||
Total {meta?.total} transactions.
|
totalPages={meta?.last_page}
|
||||||
</p>
|
currentPage={meta?.current_page}
|
||||||
)}
|
/>
|
||||||
</TableCell>
|
</div>
|
||||||
</TableRow>
|
)}
|
||||||
</TableFooter>
|
</div>
|
||||||
</Table>
|
);
|
||||||
</div>
|
|
||||||
<div className="sm:hidden block">
|
|
||||||
{data.map((trx) => (
|
|
||||||
<MobileTransactionDetails key={trx.id} trx={trx} />
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
<Pagination
|
|
||||||
totalPages={meta?.last_page}
|
|
||||||
currentPage={meta?.current_page}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
</div >
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function MobileTransactionDetails({ trx }: { trx: WalletTransaction }) {
|
function MobileTransactionDetails({ trx }: { trx: WalletTransaction }) {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"flex flex-col items-start border rounded p-2 my-2",
|
"flex flex-col items-start border rounded p-2 my-2",
|
||||||
trx?.transaction_type === "TOPUP" ? "credit-bg" : "debit-bg",
|
trx?.transaction_type === "TOPUP" ? "credit-bg" : "debit-bg",
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div className="bg-white shadow dark:bg-black p-2 rounded w-full">
|
<div className="bg-white shadow dark:bg-black p-2 rounded w-full">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<Calendar size={16} opacity={0.5} />
|
<Calendar size={16} opacity={0.5} />
|
||||||
<span className="text-muted-foreground text-sm">
|
<span className="text-muted-foreground text-sm">
|
||||||
{new Date(trx.created_at).toLocaleDateString("en-US", {
|
{new Date(trx.created_at).toLocaleDateString("en-US", {
|
||||||
month: "short",
|
month: "short",
|
||||||
day: "2-digit",
|
day: "2-digit",
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
minute: "2-digit",
|
minute: "2-digit",
|
||||||
hour: "2-digit",
|
hour: "2-digit",
|
||||||
})}
|
})}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-sm text-muted-foreground py-4">{trx.description}</p>
|
<p className="text-sm text-muted-foreground py-4">{trx.description}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="bg-white dark:bg-black p-2 rounded mt-2 w-full border flex justify-between items-center">
|
<div className="bg-white dark:bg-black p-2 rounded mt-2 w-full border flex justify-between items-center">
|
||||||
<div className="block sm:hidden">
|
<div className="block sm:hidden">
|
||||||
<h3 className="text-sm font-medium">Amount</h3>
|
<h3 className="text-sm font-medium">Amount</h3>
|
||||||
<span className="text-sm text-muted-foreground">
|
<span className="text-sm text-muted-foreground">
|
||||||
{trx.amount.toFixed(2)} MVR
|
{trx.amount.toFixed(2)} MVR
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<span className="font-semibold pr-2">
|
<span className="font-semibold pr-2">
|
||||||
{trx.transaction_type === "TOPUP" ? (
|
{trx.transaction_type === "TOPUP" ? (
|
||||||
<Badge className="bg-green-100 dark:bg-green-700">
|
<Badge className="bg-green-100 dark:bg-green-700">
|
||||||
{trx.transaction_type}
|
{trx.transaction_type}
|
||||||
</Badge>
|
</Badge>
|
||||||
) : (
|
) : (
|
||||||
<Badge className="bg-red-500 dark:bg-red-700">
|
<Badge className="bg-red-500 dark:bg-red-700">
|
||||||
{trx.transaction_type}
|
{trx.transaction_type}
|
||||||
</Badge>
|
</Badge>
|
||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-2 mt-2 w-full">
|
<div className="flex items-center gap-2 mt-2 w-full">
|
||||||
<Link
|
<Link
|
||||||
className="font-medium hover:underline"
|
className="font-medium hover:underline"
|
||||||
href={
|
href={
|
||||||
trx.transaction_type === "TOPUP"
|
trx.transaction_type === "TOPUP"
|
||||||
? `/top-ups/${trx.reference_id}`
|
? `/top-ups/${trx.reference_id}`
|
||||||
: `/payments/${trx.reference_id}`
|
: `/payments/${trx.reference_id}`
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<Button size={"sm"} className="w-full">
|
<Button size={"sm"} className="w-full">
|
||||||
View Details
|
View Details
|
||||||
</Button>
|
</Button>
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,6 @@ export interface NewPayment {
|
|||||||
amount: number;
|
amount: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface WalletTransaction {
|
export interface WalletTransaction {
|
||||||
id: string;
|
id: string;
|
||||||
user: Pick<User, "id" | "id_card" | "mobile"> & {
|
user: Pick<User, "id" | "id_card" | "mobile"> & {
|
||||||
@ -109,4 +108,4 @@ export interface WalletTransaction {
|
|||||||
description: string;
|
description: string;
|
||||||
reference_id: string;
|
reference_id: string;
|
||||||
created_at: string;
|
created_at: string;
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { authOptions } from "@/app/auth";
|
import { authOptions } from "@/app/auth";
|
||||||
import { BlockDeviceFormState } from "@/components/block-device-dialog";
|
import type { BlockDeviceFormState } from "@/components/block-device-dialog";
|
||||||
import type {
|
import type {
|
||||||
AddDeviceFormState,
|
AddDeviceFormState,
|
||||||
initialState,
|
initialState,
|
||||||
@ -63,7 +63,7 @@ export async function getDevice({ deviceId }: { deviceId: string }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function addDeviceAction(
|
export async function addDeviceAction(
|
||||||
prevState: AddDeviceFormState,
|
_prevState: AddDeviceFormState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<AddDeviceFormState> {
|
): Promise<AddDeviceFormState> {
|
||||||
const name = formData.get("name") as string;
|
const name = formData.get("name") as string;
|
||||||
@ -135,7 +135,7 @@ export async function addDeviceAction(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function blockDeviceAction(
|
export async function blockDeviceAction(
|
||||||
prevState: BlockDeviceFormState,
|
_prevState: BlockDeviceFormState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<BlockDeviceFormState> {
|
): Promise<BlockDeviceFormState> {
|
||||||
const deviceId = formData.get("deviceId") as string;
|
const deviceId = formData.get("deviceId") as string;
|
||||||
@ -196,10 +196,7 @@ export async function blockDeviceAction(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const result = await handleApiResponse<Device>(
|
await handleApiResponse<Device>(response, "blockDeviceAction");
|
||||||
response,
|
|
||||||
"blockDeviceAction",
|
|
||||||
);
|
|
||||||
|
|
||||||
revalidatePath("/devices");
|
revalidatePath("/devices");
|
||||||
revalidatePath("/parental-control");
|
revalidatePath("/parental-control");
|
||||||
|
@ -8,10 +8,10 @@ import { handleApiResponse } from "@/utils/tryCatch";
|
|||||||
type ParamProps = {
|
type ParamProps = {
|
||||||
[key: string]: string | number | undefined;
|
[key: string]: string | number | undefined;
|
||||||
};
|
};
|
||||||
export async function getUsers(params: ParamProps) {
|
export async function getUsers(params?: ParamProps) {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
|
|
||||||
const query = Object.entries(params)
|
const query = Object.entries(params ?? {})
|
||||||
.filter(([_, value]) => value !== undefined && value !== "")
|
.filter(([_, value]) => value !== undefined && value !== "")
|
||||||
.map(
|
.map(
|
||||||
([key, value]) =>
|
([key, value]) =>
|
||||||
|
@ -1,44 +1,48 @@
|
|||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { authOptions } from "@/app/auth";
|
import { authOptions } from "@/app/auth";
|
||||||
import type { ApiError, ApiResponse, WalletTransaction } from "@/lib/backend-types";
|
import type {
|
||||||
|
ApiError,
|
||||||
|
ApiResponse,
|
||||||
|
WalletTransaction,
|
||||||
|
} from "@/lib/backend-types";
|
||||||
|
|
||||||
type GenericGetResponseProps = {
|
type GenericGetResponseProps = {
|
||||||
offset?: number;
|
offset?: number;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
page?: number;
|
page?: number;
|
||||||
[key: string]: string | number | undefined;
|
[key: string]: string | number | undefined;
|
||||||
};
|
};
|
||||||
export async function getWaleltTransactions(
|
export async function getWaleltTransactions(
|
||||||
params: GenericGetResponseProps,
|
params: GenericGetResponseProps,
|
||||||
allTransactions = false,
|
allTransactions = false,
|
||||||
) {
|
) {
|
||||||
// Build query string from all defined params
|
// Build query string from all defined params
|
||||||
const query = Object.entries(params)
|
const query = Object.entries(params)
|
||||||
.filter(([_, value]) => value !== undefined && value !== "")
|
.filter(([_, value]) => value !== undefined && value !== "")
|
||||||
.map(
|
.map(
|
||||||
([key, value]) =>
|
([key, value]) =>
|
||||||
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`,
|
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`,
|
||||||
)
|
)
|
||||||
.join("&");
|
.join("&");
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/wallet-transactions/?${query}&all_transactions=${allTransactions}`,
|
`${process.env.SARLINK_API_BASE_URL}/api/billing/wallet-transactions/?${query}&all_transactions=${allTransactions}`,
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Token ${session?.apiToken}`,
|
Authorization: `Token ${session?.apiToken}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = (await response.json()) as ApiError;
|
const errorData = (await response.json()) as ApiError;
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
errorData.message || errorData.detail || "An error occurred.";
|
errorData.message || errorData.detail || "An error occurred.";
|
||||||
const error = new Error(errorMessage);
|
const error = new Error(errorMessage);
|
||||||
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
const data = (await response.json()) as ApiResponse<WalletTransaction>;
|
const data = (await response.json()) as ApiResponse<WalletTransaction>;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user