mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-03 06:48:21 +00:00
feat: add age validation in signup and update payment verification logic
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 5m17s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 5m17s
This commit is contained in:
@ -115,20 +115,18 @@ export async function cancelPayment({ id }: { id: string }) {
|
||||
return { message: "Payment successfully canceled." };
|
||||
}
|
||||
|
||||
type UpdatePayment = Pick<
|
||||
Payment,
|
||||
"id" | "paid" | "paid_at" | "method" | "number_of_months"
|
||||
>;
|
||||
export async function updatePayment({
|
||||
id,
|
||||
method,
|
||||
paid,
|
||||
paid_at,
|
||||
number_of_months,
|
||||
}: UpdatePayment) {
|
||||
type UpdatePayment = {
|
||||
id: string;
|
||||
method: "TRANSFER" | "WALLET";
|
||||
benefName?: string;
|
||||
accountNo?: string;
|
||||
absAmount?: string;
|
||||
time?: string;
|
||||
};
|
||||
export async function verifyPayment({ id, method }: UpdatePayment) {
|
||||
const session = await getServerSession(authOptions);
|
||||
const response = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}/update/`,
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}/verify/`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
@ -137,58 +135,11 @@ export async function updatePayment({
|
||||
},
|
||||
body: JSON.stringify({
|
||||
method,
|
||||
paid,
|
||||
paid_at,
|
||||
number_of_months,
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
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;
|
||||
}
|
||||
const payment = (await response.json()) as Payment;
|
||||
return payment;
|
||||
}
|
||||
|
||||
type TUpdateWalletBalance = Pick<User, "id" | "wallet_balance">;
|
||||
export async function updateWalletBalance({
|
||||
id,
|
||||
wallet_balance,
|
||||
}: TUpdateWalletBalance) {
|
||||
const session = await getServerSession(authOptions);
|
||||
console.log("wallet bal in server action", wallet_balance);
|
||||
const response = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/auth/update-wallet/${id}/`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${session?.apiToken}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
wallet_balance: Number.parseFloat(wallet_balance?.toFixed(2) ?? "0"),
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
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;
|
||||
}
|
||||
const message = (await response.json()) as {
|
||||
message: "Wallet balance updated successfully.";
|
||||
};
|
||||
return message;
|
||||
revalidatePath("/payments/[paymentsId]");
|
||||
return handleApiResponse<Payment>(response, "updatePayment");
|
||||
}
|
||||
|
||||
type VerifyPaymentType = {
|
||||
@ -217,43 +168,6 @@ export async function getProfile() {
|
||||
return handleApiResponse<User>(response, "getProfile");
|
||||
}
|
||||
|
||||
export async function processWalletPayment({
|
||||
payment,
|
||||
amount,
|
||||
}: { payment: Payment | undefined; amount: number }) {
|
||||
await checkSession();
|
||||
const session = await getServerSession(authOptions);
|
||||
const walletBalance = session?.user?.wallet_balance ?? 0;
|
||||
console.log("processing wallet payment >>>", walletBalance);
|
||||
if (!payment) return;
|
||||
const [updatePaymentError, _] = await tryCatch(
|
||||
updatePayment({
|
||||
id: payment.id,
|
||||
method: "WALLET",
|
||||
paid: true,
|
||||
paid_at: new Date().toISOString(),
|
||||
number_of_months: payment.number_of_months,
|
||||
}),
|
||||
);
|
||||
if (updatePaymentError) {
|
||||
throw new Error(updatePaymentError.message);
|
||||
}
|
||||
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,
|
||||
wallet_balance: Number.parseFloat(updated_balance?.toFixed(2) ?? "0"),
|
||||
}),
|
||||
);
|
||||
if (walletUpdateError) {
|
||||
throw new Error(walletUpdateError.message);
|
||||
}
|
||||
revalidatePath("/payments/[paymentsId]", "page");
|
||||
return response;
|
||||
}
|
||||
|
||||
type VerifyPaymentResponse =
|
||||
| {
|
||||
success: boolean;
|
||||
|
Reference in New Issue
Block a user