2025-04-30 17:28:58 +01:00

356 lines
12 KiB
TypeScript

import type {
UserLoginNameAndPwd,
BaseRetData,
GetDoctorList,
GetWardList,
GetWardPatients,
Login,
UserInfo,
GetPatientList,
GetTeamList,
GetPatientBookingList,
GetTeamInfo,
GetAppointments,
GetDoctorAppointments,
GetTreatmentRecords
} from "./models"
import {dateToTimestamp} from "~/utils/utils.ts";
export const apiEndpoint = import.meta.env.VITE_API_ENDPOINT
export async function fetchAPI(path: string, method: string, body?: any, headers?: any, contentType: string | null = "application/json"): Promise<Response> {
const baseHeaders: { [key: string]: any } = {
"Authorization": `${localStorage.getItem("hms_token")}`,
}
if (contentType) {
baseHeaders["Content-Type"] = contentType
}
let reqHeaders: any
if (headers) {
reqHeaders = {...baseHeaders, ...headers}
}
else {
reqHeaders = baseHeaders
}
return new Promise((resolve, reject) => {
fetch(`${apiEndpoint}/${path}`, {
method,
credentials: "include",
headers: reqHeaders,
body: contentType === "application/json" ? (body ? JSON.stringify(body) : undefined) : body,
})
.then((v) => resolve(v))
.catch((e) => reject(e))
})
}
export async function apiLogin(userName: string, password: string): Promise<Login> {
const resp = await fetchAPI("login", "POST", {
username: userName,
password: password
})
return resp.json()
}
export async function apiChangeMyPassword(old_password: string, newPassword: string): Promise<BaseRetData> {
const resp = await fetchAPI("change_password", "POST", {
old_password: old_password,
new_password: newPassword
})
return resp.json()
}
export async function apiRegisterPatient(regData: {
username: string,
password: string,
name: string,
title: string,
birth_date: number,
gender: string,
phone: string,
email: string,
address: string,
postcode: string,
}): Promise<BaseRetData> {
const resp = await fetchAPI("register_patient", "POST", regData)
return resp.json()
}
export async function apiGetMyInfo(): Promise<UserInfo> {
const resp = await fetchAPI("get_my_info", "GET")
return resp.json()
}
export async function apiGetWardList(page: number, no_limit: boolean = false): Promise<GetWardList> {
const resp = await fetchAPI(`get_ward_list?page=${page}&no_limit=${no_limit}`, "GET")
return resp.json()
}
export async function apiEditWard(ward_id: number, ward_name: string, total_capacity: number, type: string): Promise<BaseRetData> {
const resp = await fetchAPI("admin/edit_ward", "POST", {
"ward_id": ward_id,
"ward_name": ward_name,
"total_capacity": total_capacity,
"type": type
})
return resp.json()
}
export async function apiDeleteWard(ward_id: number): Promise<BaseRetData> {
const resp = await fetchAPI("admin/delete_ward", "DELETE", {
"ward_id": ward_id,
})
return resp.json()
}
export async function apiCreateWard(ward_name: string, total_capacity: number, type: string): Promise<BaseRetData> {
const resp = await fetchAPI("admin/create_ward", "POST", {
"ward_name": ward_name,
"total_capacity": total_capacity,
"type": type
})
return resp.json()
}
export async function apiGetWardPatients(ward_id: number): Promise<GetWardPatients> {
const resp = await fetchAPI(`data/ward_patients?ward_id=${ward_id}`, "GET")
return resp.json()
}
export async function apiGetDoctorsList(page: number): Promise<GetDoctorList> {
// set page to -1 to get all doctors
const resp = await fetchAPI(`get_doctor_list?page=${page}`, "GET")
return resp.json()
}
export async function apiEditDoctorInfo(data: {
gender: string, phone: string, birth_date: number | Date, grade: number | string, name: string, title: string, email: string, is_admin: boolean,
doctor_id?: number, id?: number
}): Promise<BaseRetData> {
const pushData = structuredClone(data)
if (pushData.birth_date instanceof Date) {
pushData.birth_date = dateToTimestamp(pushData.birth_date)
}
if (typeof pushData.grade === "string") {
pushData.grade = Number(pushData.grade)
}
if (pushData.doctor_id === undefined) {
pushData.doctor_id = pushData.id
}
const resp = await fetchAPI("admin/edit_doctor_info", "POST", pushData)
return resp.json()
}
export async function apiSetDoctorResigned(doctor_id: number, is_resigned: boolean): Promise<BaseRetData> {
const resp = await fetchAPI(`admin/set_resign_doctor?`, "POST", {
"doctor_id": doctor_id,
"is_resigned": is_resigned
})
return resp.json()
}
export async function apiAdminRegister(data: {
reg_type: number | string, // 0: patient, 1: doctor, 99: receptionist
username: string,
gender: string, phone: string, birth_date: number | Date, name: string, title: string, email: string,
// team_id?: number, // doctor
grade?: number | string, // doctor or receptionist
is_admin?: boolean, // doctor or receptionist
address?: string, postcode?: string // patient
}): Promise<UserLoginNameAndPwd> {
const pushData = structuredClone(data)
if (pushData.birth_date instanceof Date) {
pushData.birth_date = dateToTimestamp(pushData.birth_date)
}
if (pushData.grade && typeof pushData.grade === "string") {
pushData.grade = Number(pushData.grade)
}
if (typeof pushData.reg_type === "string") {
pushData.reg_type = Number(pushData.reg_type)
}
const resp = await fetchAPI("admin/register", "POST", pushData)
return resp.json()
}
export async function apiResetPasswordFromRoleID(user_id: number, user_type: number): Promise<UserLoginNameAndPwd> {
const resp = await fetchAPI("admin/reset_user_password_from_role_id", "POST", {
"user_id": user_id,
"user_type": user_type
})
return resp.json()
}
export async function apiGetTeamList(page: number): Promise<GetTeamList> {
// set page to -1 to get all teams
const resp = await fetchAPI(`get_team_list?page=${page}`, "GET")
return resp.json()
}
export async function apiEditTeam(data: {
team_id: number,
department: string,
consultant_id: number,
is_admin_team: boolean,
team_members: number[] | null, // no change team members if null
}): Promise<BaseRetData> {
const resp = await fetchAPI("admin/edit_team_info", "POST", data)
return resp.json()
}
export async function apiCreateTeam(data: {
department: string,
consultant_id: number,
is_admin_team: boolean,
team_members: number[] | null,
}): Promise<BaseRetData> {
const resp = await fetchAPI("admin/create_doctor_team", "POST", data)
return resp.json()
}
export async function apiDeleteTeam(team_id: number): Promise<BaseRetData> {
const resp = await fetchAPI("admin/delete_doctor_team", "DELETE", {
"team_id": team_id,
})
return resp.json()
}
export async function get_team_info(team_id: number): Promise<GetTeamInfo> {
const resp = await fetchAPI(`get_team_info?team_id=${team_id}`, "GET")
return resp.json()
}
export async function apiGetPatientsList(page: number): Promise<GetPatientList> {
// set page to -1 to get all patients
const resp = await fetchAPI(`admin/get_patient_list?page=${page}`, "GET")
return resp.json()
}
export async function apiPatientBooking(data: {
appointment_time: number, // timestamp
category: string, // BookingCategory
description: string
}): Promise<BaseRetData> {
const resp = await fetchAPI("patient/book", "POST", data)
return resp.json()
}
export async function apiGetPatientBookingList(page: number): Promise<GetPatientBookingList> {
const resp = await fetchAPI(`patient/appointments?page=${page}`, "GET")
return resp.json()
}
export async function apiEditPatientBooking(data: {
appointment_id: number,
appointment_time: number, // timestamp
category: string, // BookingCategory
description: string,
}): Promise<BaseRetData> {
const resp = await fetchAPI("patient/edit_appointment", "POST", data)
return resp.json()
}
export async function apiDeletePatientBooking(appointment_id: number): Promise<BaseRetData> {
const resp = await fetchAPI("patient/cancel_appointment", "DELETE", {
"appointment_id": appointment_id,
})
return resp.json()
}
export async function apiGetAllAppointments(page: number, include_discharged: boolean): Promise<GetAppointments> {
const resp = await fetchAPI(`receptionist/appointments?page=${page}&include_discharged=${include_discharged}`, "GET")
return resp.json()
}
export async function apiProcessAppointment(appointment_id: number, approved: boolean, feedback: string, assigned_team: number | null): Promise<BaseRetData> {
const resp = await fetchAPI("receptionist/process_appointment", "POST", {
"appointment_id": appointment_id,
"approved": approved,
"feedback": feedback,
"assigned_team": assigned_team
})
return resp.json()
}
export async function apiPatientAdmission(appointment_id: number, ward_id: number): Promise<BaseRetData> {
const resp = await fetchAPI("receptionist/check_in", "POST", {
"appointment_id": appointment_id,
"ward_id": ward_id
})
return resp.json()
}
// export async function apiTransferWard(admission_id: number, target_ward_id: number): Promise<BaseRetData> {
// const resp = await fetchAPI("admin/transfer_ward", "POST", {
// "admission_id": admission_id,
// "target_ward_id": target_ward_id
// })
// return resp.json()
// }
export async function apiTransferWard(patient_id: number, target_ward_id: number): Promise<BaseRetData> {
const resp = await fetchAPI("admin/transfer_ward", "POST", {
"patient_id": patient_id,
"target_ward_id": target_ward_id
})
return resp.json()
}
export async function apiGetDoctorAppointments(target_team_id?: number): Promise<GetDoctorAppointments> {
const resp = await fetchAPI(`doctor/appointments?target_team=${target_team_id}`, "GET")
return resp.json()
}
export async function apiDoctorTreat(appointment_id: number, treat_info: string, treated_at: number): Promise<BaseRetData> {
const resp = await fetchAPI("doctor/treat", "POST", {
"appointment_id": appointment_id,
"treat_info": treat_info,
"treated_at": treated_at // timestamp
})
return resp.json()
}
export async function apiGetTreatmentRecords(page: number, patient_id: number | null, doctor_id: number | null,
team_id: number | null, all: boolean = false): Promise<GetTreatmentRecords> {
const resp = await fetchAPI(`data/treatment_doctors?patient_id=${patient_id}&doctor_id=${doctor_id}&team_id=${team_id}&page=${page}&all=${all}`, "GET")
return resp.json()
}
export async function apiEditPatientInfo(data: {
gender: string, phone: string, birth_date: number | Date, address: string, name: string, title: string, email: string, postcode: string,
patient_id?: number, id?: number
}): Promise<BaseRetData> {
const pushData = structuredClone(data)
if (pushData.birth_date instanceof Date) {
pushData.birth_date = dateToTimestamp(pushData.birth_date)
}
if (pushData.patient_id === undefined) {
pushData.patient_id = pushData.id
}
const resp = await fetchAPI("admin/edit_patient_info", "POST", pushData)
return resp.json()
}
export async function apiPatientDischarge(admission_id: number): Promise<BaseRetData> {
const resp = await fetchAPI("admin/patient_discharge", "POST", {
"admission_id": admission_id
})
return resp.json()
}
export async function apiPatientGetCurrentWard(): Promise<BaseRetData> {
const resp = await fetchAPI("patient/get_my_ward", "GET")
return resp.json()
}