Merge pull request #11 from i701/feat/wallet-topups
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 5m15s

feat/wallet topups
This commit is contained in:
Abdulla Aidhaan
2025-07-06 20:02:16 +05:00
committed by GitHub
3 changed files with 21 additions and 25 deletions

View File

@ -164,24 +164,16 @@ export async function getTopup({ id }: { id: string }) {
export async function cancelTopup({ id }: { id: string }) { export async function cancelTopup({ id }: { id: string }) {
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/topup/${id}/delete/`, `${process.env.SARLINK_API_BASE_URL}/api/billing/topup/${id}/cancel/`,
{ {
method: "DELETE", method: "PATCH",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Token ${session?.apiToken}`, Authorization: `Token ${session?.apiToken}`,
}, },
}, },
); );
if (!response.ok) { return handleApiResponse<Topup>(response, "cancelTopup");
const errorData = (await response.json()) as ApiError;
const errorMessage =
errorData.message || errorData.detail || "An error occurred.";
const error = new Error(errorMessage);
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
throw error;
}
return { message: "Topup successfully canceled." };
} }
export async function cancelPayment({ id }: { id: string }) { export async function cancelPayment({ id }: { id: string }) {

View File

@ -17,13 +17,15 @@ export default function CancelTopupButton({
<Button <Button
onClick={async () => { onClick={async () => {
setLoading(true); setLoading(true);
const [error, x] = await tryCatch(cancelTopup({ id: topupId })); const [error, topup] = await tryCatch(cancelTopup({ id: topupId }));
console.log(x);
if (error) { if (error) {
toast.error(error.message); toast.error(error.message);
setLoading(false); setLoading(false);
} else { } else {
toast.success("Topup cancelled successfully!") toast.success("Topup cancelled successfully!", {
description: `Your topup of ${topup?.amount} MVR has been cancelled.`,
closeButton: true,
})
router.replace("/top-ups"); router.replace("/top-ups");
} }
}} }}

View File

@ -125,7 +125,7 @@ export async function TopupsTable({
{topup.paid ? ( {topup.paid ? (
<Badge className="bg-green-100 dark:bg-green-700" variant="outline">{topup.status}</Badge> <Badge className="bg-green-100 dark:bg-green-700" variant="outline">{topup.status}</Badge>
) : topup.is_expired ? ( ) : topup.is_expired ? (
<Badge className="text-black dark:text-white bg-yellow-100 dark:bg-yellow-700">Expired</Badge> <Badge>Expired</Badge>
) : ( ) : (
<Badge variant="outline">{topup.status}</Badge> <Badge variant="outline">{topup.status}</Badge>
)} )}
@ -203,6 +203,7 @@ function MobileTopupDetails({ topup }: { topup: Topup }) {
View Details View Details
</Button> </Button>
</Link> </Link>
{topup.status !== "CANCELLED" && (
<Badge <Badge
className={cn( className={cn(
topup?.paid topup?.paid
@ -213,6 +214,7 @@ function MobileTopupDetails({ topup }: { topup: Topup }) {
> >
{topup.paid ? "Paid" : "Unpaid"} {topup.paid ? "Paid" : "Unpaid"}
</Badge> </Badge>
)}
</div> </div>
<div className="bg-white dark:bg-black p-2 rounded mt-2 w-full border"> <div className="bg-white dark:bg-black p-2 rounded mt-2 w-full border">
<div className="block sm:hidden"> <div className="block sm:hidden">