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:
@ -224,3 +224,56 @@ export async function updateUserAgreement(
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user