mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-08-04 03:27: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:
@ -3,7 +3,7 @@
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/app/auth";
|
||||
import { BlockDeviceFormState } from "@/components/block-device-dialog";
|
||||
import type { BlockDeviceFormState } from "@/components/block-device-dialog";
|
||||
import type {
|
||||
AddDeviceFormState,
|
||||
initialState,
|
||||
@ -63,7 +63,7 @@ export async function getDevice({ deviceId }: { deviceId: string }) {
|
||||
}
|
||||
|
||||
export async function addDeviceAction(
|
||||
prevState: AddDeviceFormState,
|
||||
_prevState: AddDeviceFormState,
|
||||
formData: FormData,
|
||||
): Promise<AddDeviceFormState> {
|
||||
const name = formData.get("name") as string;
|
||||
@ -135,7 +135,7 @@ export async function addDeviceAction(
|
||||
}
|
||||
|
||||
export async function blockDeviceAction(
|
||||
prevState: BlockDeviceFormState,
|
||||
_prevState: BlockDeviceFormState,
|
||||
formData: FormData,
|
||||
): Promise<BlockDeviceFormState> {
|
||||
const deviceId = formData.get("deviceId") as string;
|
||||
@ -196,10 +196,7 @@ export async function blockDeviceAction(
|
||||
},
|
||||
);
|
||||
|
||||
const result = await handleApiResponse<Device>(
|
||||
response,
|
||||
"blockDeviceAction",
|
||||
);
|
||||
await handleApiResponse<Device>(response, "blockDeviceAction");
|
||||
|
||||
revalidatePath("/devices");
|
||||
revalidatePath("/parental-control");
|
||||
|
@ -8,10 +8,10 @@ import { handleApiResponse } from "@/utils/tryCatch";
|
||||
type ParamProps = {
|
||||
[key: string]: string | number | undefined;
|
||||
};
|
||||
export async function getUsers(params: ParamProps) {
|
||||
export async function getUsers(params?: ParamProps) {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
const query = Object.entries(params)
|
||||
const query = Object.entries(params ?? {})
|
||||
.filter(([_, value]) => value !== undefined && value !== "")
|
||||
.map(
|
||||
([key, value]) =>
|
||||
|
@ -1,44 +1,48 @@
|
||||
import { getServerSession } from "next-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 = {
|
||||
offset?: number;
|
||||
limit?: number;
|
||||
page?: number;
|
||||
[key: string]: string | number | undefined;
|
||||
offset?: number;
|
||||
limit?: number;
|
||||
page?: number;
|
||||
[key: string]: string | number | undefined;
|
||||
};
|
||||
export async function getWaleltTransactions(
|
||||
params: GenericGetResponseProps,
|
||||
allTransactions = false,
|
||||
params: GenericGetResponseProps,
|
||||
allTransactions = false,
|
||||
) {
|
||||
// Build query string from all defined params
|
||||
const query = Object.entries(params)
|
||||
.filter(([_, value]) => value !== undefined && value !== "")
|
||||
.map(
|
||||
([key, value]) =>
|
||||
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`,
|
||||
)
|
||||
.join("&");
|
||||
const session = await getServerSession(authOptions);
|
||||
const response = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/wallet-transactions/?${query}&all_transactions=${allTransactions}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${session?.apiToken}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
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 data = (await response.json()) as ApiResponse<WalletTransaction>;
|
||||
return data;
|
||||
}
|
||||
// Build query string from all defined params
|
||||
const query = Object.entries(params)
|
||||
.filter(([_, value]) => value !== undefined && value !== "")
|
||||
.map(
|
||||
([key, value]) =>
|
||||
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`,
|
||||
)
|
||||
.join("&");
|
||||
const session = await getServerSession(authOptions);
|
||||
const response = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/wallet-transactions/?${query}&all_transactions=${allTransactions}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${session?.apiToken}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
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 data = (await response.json()) as ApiResponse<WalletTransaction>;
|
||||
return data;
|
||||
}
|
||||
|
Reference in New Issue
Block a user