mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-03 06:48:21 +00:00
feat: add loading skeleton for devices table and improve payment processing logic
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m30s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m30s
This commit is contained in:
@ -8,6 +8,7 @@ import type {
|
||||
Payment,
|
||||
} from "@/lib/backend-types";
|
||||
import type { User } from "@/lib/types/user";
|
||||
import { checkSession } from "@/utils/session";
|
||||
import { tryCatch } from "@/utils/tryCatch";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { revalidatePath } from "next/cache";
|
||||
@ -200,27 +201,15 @@ type VerifyPaymentType = {
|
||||
type?: "TRANSFER" | "WALLET";
|
||||
};
|
||||
|
||||
class InsufficientFundsError extends Error {
|
||||
constructor() {
|
||||
super("Insufficient funds in wallet");
|
||||
this.name = "InsufficientFundsError";
|
||||
}
|
||||
}
|
||||
|
||||
export async function processWalletPayment({
|
||||
payment,
|
||||
amount,
|
||||
}: { payment: Payment | undefined; amount: number }) {
|
||||
await checkSession();
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.user || !payment) {
|
||||
throw new Error("User or payment not found");
|
||||
}
|
||||
|
||||
const walletBalance = session.user.wallet_balance ?? 0;
|
||||
if (walletBalance < amount) {
|
||||
throw new InsufficientFundsError();
|
||||
}
|
||||
|
||||
const walletBalance = session?.user?.wallet_balance ?? 0;
|
||||
console.log("processing wallet payment >>>", walletBalance);
|
||||
if (!payment) return;
|
||||
const [updatePaymentError, _] = await tryCatch(
|
||||
updatePayment({
|
||||
id: payment.id,
|
||||
@ -235,9 +224,10 @@ export async function processWalletPayment({
|
||||
}
|
||||
console.log("Wallet balance before update:", walletBalance);
|
||||
const updated_balance = walletBalance - amount;
|
||||
if (!session?.user?.id) return;
|
||||
const [walletUpdateError, response] = await tryCatch(
|
||||
updateWalletBalance({
|
||||
id: session.user.id,
|
||||
id: session?.user?.id,
|
||||
wallet_balance: Number.parseFloat(updated_balance?.toFixed(2) ?? "0"),
|
||||
}),
|
||||
);
|
||||
|
Reference in New Issue
Block a user