128 lines
3.3 KiB
JavaScript
128 lines
3.3 KiB
JavaScript
import { getToken } from './auth';
|
|
|
|
const API_BASE = (import.meta.env.VITE_API_BASE || '').replace(/\/$/, '');
|
|
|
|
async function request(path, options = {}) {
|
|
const headers = {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
...(options.headers || {}),
|
|
};
|
|
const token = getToken();
|
|
if (token && !headers.Authorization) {
|
|
headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
|
|
let res;
|
|
try {
|
|
res = await fetch(`${API_BASE}${path}`, {
|
|
...options,
|
|
headers,
|
|
});
|
|
} catch (networkErr) {
|
|
const err = new Error(
|
|
`Cannot reach TGICS API at ${API_BASE || '(missing VITE_API_BASE)'}. Is the backend running, and is CORS allowing this origin?`
|
|
);
|
|
err.cause = networkErr;
|
|
throw err;
|
|
}
|
|
const data = await res.json().catch(() => ({}));
|
|
if (!res.ok) {
|
|
const err = new Error(data.message || `Request failed (${res.status})`);
|
|
err.status = res.status;
|
|
err.data = data;
|
|
throw err;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
export function fetchDoctor(userId, contactId) {
|
|
return request(`/api/doctors/${encodeURIComponent(userId)}/${encodeURIComponent(contactId)}`);
|
|
}
|
|
|
|
export function loginDoctorByPhone(userId, phone) {
|
|
return request(`/api/doctors/${encodeURIComponent(userId)}/login`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ phone }),
|
|
});
|
|
}
|
|
|
|
export function submitSurvey(payload) {
|
|
return request('/api/surveys', {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export function fetchPatients(contactId, limit = 100, offset = 0) {
|
|
const qs = new URLSearchParams({
|
|
contact_id: contactId,
|
|
limit: String(limit),
|
|
offset: String(offset),
|
|
});
|
|
return request(`/api/surveys?${qs.toString()}`);
|
|
}
|
|
|
|
export function login(email, password) {
|
|
return request('/api/auth/login', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ email, password }),
|
|
});
|
|
}
|
|
|
|
export function fetchMe() {
|
|
return request('/api/auth/me');
|
|
}
|
|
|
|
export function fetchDashboardStats() {
|
|
return request('/api/dashboard/stats');
|
|
}
|
|
|
|
export function fetchDashboardSurveys(limit = 50, offset = 0) {
|
|
return request(`/api/dashboard/surveys?limit=${limit}&offset=${offset}`);
|
|
}
|
|
|
|
export function fetchTmUsers(refresh = false) {
|
|
return request(`/api/dashboard/tm-users${refresh ? '?refresh=1' : ''}`);
|
|
}
|
|
|
|
export function fetchDashboardDoctors(refresh = false) {
|
|
return request(`/api/dashboard/doctors${refresh ? '?refresh=1' : ''}`);
|
|
}
|
|
|
|
export function updateDashboardDoctor(contactId, payload) {
|
|
return request(`/api/dashboard/doctors/${encodeURIComponent(contactId)}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export function fetchSurveyDraft(contactId, userId) {
|
|
const qs = new URLSearchParams({
|
|
contact_id: contactId,
|
|
user_id: userId,
|
|
});
|
|
return request(`/api/surveys/draft?${qs.toString()}`);
|
|
}
|
|
|
|
export function saveSurveyDraft(payload) {
|
|
return request('/api/surveys/draft', {
|
|
method: 'PUT',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export function deleteSurveyDraft(contactId, userId) {
|
|
const qs = new URLSearchParams({
|
|
contact_id: contactId,
|
|
user_id: userId,
|
|
});
|
|
return request(`/api/surveys/draft?${qs.toString()}`, {
|
|
method: 'DELETE',
|
|
});
|
|
}
|
|
|
|
export function fetchDashboardPatients(refresh = false) {
|
|
return request(`/api/dashboard/patients${refresh ? '?refresh=1' : ''}`);
|
|
}
|