Skip to content

Commit

Permalink
✨ feat: add helper methods for fetching hospital list and details
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed Aug 22, 2024
1 parent 3974e1b commit b2f9772
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
85 changes: 85 additions & 0 deletions app/lib/admin/getHospitals.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"use server";

import { getSessionToken } from "../sessions/sessionUtils";
import getBaseUrl from "@utils/getBaseUrl";

export async function getHospitalsList(
currentPage: number = 1,
pageSize: number = 10
) {
const session = getSessionToken();
const serverUrl = getBaseUrl();

const headers = {
Authorization: `Bearer ${session}`,
};

try {
const response = await fetch(
`${serverUrl}/api/admin/hospitals?page=${currentPage}&limit=${pageSize}`,
{
headers,
}
);

if (!response.ok) {
throw new Error("Failed to fetch hospitals list");
}

const data = await response.json();

return {
hospitals: data.hospitals,
pagination: {
currentPage: data.pagination.currentPage,
pageSize: data.pagination.pageSize,
totalPages: data.pagination.totalPages,
totalCount: data.pagination.totalCount,
},
};
} catch (error) {
console.error("An error occurred while fetching hospitals list:", error);
throw error;
}
}

export async function getHospitalDetails(
hospitalId: string,
currentPage: number = 1,
pageSize: number = 10
) {
const session = getSessionToken();
const serverUrl = getBaseUrl();

const headers = {
Authorization: `Bearer ${session}`,
};

try {
const response = await fetch(
`${serverUrl}/api/admin/hospitals/users?hospitalId=${hospitalId}&page=${currentPage}&limit=${pageSize}`,
{
headers,
}
);

if (!response.ok) {
throw new Error("Failed to fetch hospital users details");
}

const data = await response.json();

return {
users: data.users,
pagination: {
currentPage: data.pagination.currentPage,
pageSize: data.pagination.pageSize,
totalPages: data.pagination.totalPages,
totalCount: data.pagination.totalCount,
},
};
} catch (error) {
console.error("An error occurred while fetching hospital details:", error);
throw error;
}
}
3 changes: 3 additions & 0 deletions app/lib/admin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import addAdmin from "./addAdmin";
import getAdminData from "./getAdminData";
import { getHospitalsList, getHospitalDetails } from "./getHospitals";
import { getTilesData, getRecentUsersData } from "./getDashboardData";
import getTransactions from "./getTransactions";

export {
addAdmin,
getAdminData,
getHospitalsList,
getHospitalDetails,
getTilesData,
getRecentUsersData,
getTransactions,
Expand Down

0 comments on commit b2f9772

Please sign in to comment.