Moves the functions and types related to patients and physicians from the single file lib/api.ts to their own dedicated files in lib/api/pacientes.ts and lib/api/medicos.ts.
129 lines
4.0 KiB
TypeScript
129 lines
4.0 KiB
TypeScript
// lib/api/medicos.ts
|
|
import { REST, baseHeaders, rangeHeaders, withPrefer, parse } from "../api";
|
|
|
|
// ===== TIPOS DE MÉDICOS =====
|
|
export type FormacaoAcademica = {
|
|
instituicao: string;
|
|
curso: string;
|
|
ano_conclusao: string;
|
|
};
|
|
|
|
export type DadosBancarios = {
|
|
banco: string;
|
|
agencia: string;
|
|
conta: string;
|
|
tipo_conta: string;
|
|
};
|
|
|
|
export type Medico = {
|
|
id: string;
|
|
nome?: string;
|
|
nome_social?: string | null;
|
|
cpf?: string;
|
|
rg?: string | null;
|
|
sexo?: string | null;
|
|
data_nascimento?: string | null;
|
|
telefone?: string;
|
|
celular?: string;
|
|
contato_emergencia?: string;
|
|
email?: string;
|
|
crm?: string;
|
|
estado_crm?: string;
|
|
rqe?: string;
|
|
formacao_academica?: FormacaoAcademica[];
|
|
curriculo_url?: string | null;
|
|
especialidade?: string;
|
|
observacoes?: string | null;
|
|
foto_url?: string | null;
|
|
tipo_vinculo?: string;
|
|
dados_bancarios?: DadosBancarios;
|
|
agenda_horario?: string;
|
|
valor_consulta?: number | string;
|
|
};
|
|
|
|
export type MedicoInput = {
|
|
nome: string;
|
|
nome_social?: string | null;
|
|
cpf?: string | null;
|
|
rg?: string | null;
|
|
sexo?: string | null;
|
|
data_nascimento?: string | null;
|
|
telefone?: string | null;
|
|
celular?: string | null;
|
|
contato_emergencia?: string | null;
|
|
email?: string | null;
|
|
crm: string;
|
|
estado_crm?: string | null;
|
|
rqe?: string | null;
|
|
formacao_academica?: FormacaoAcademica[];
|
|
curriculo_url?: string | null;
|
|
especialidade: string;
|
|
observacoes?: string | null;
|
|
tipo_vinculo?: string | null;
|
|
dados_bancarios?: DadosBancarios | null;
|
|
agenda_horario?: string | null;
|
|
valor_consulta?: number | string | null;
|
|
};
|
|
|
|
// ===== MÉDICOS (CRUD) =====
|
|
export async function listarMedicos(params?: {
|
|
page?: number;
|
|
limit?: number;
|
|
q?: string;
|
|
}): Promise<Medico[]> {
|
|
const qs = new URLSearchParams();
|
|
if (params?.q) qs.set("q", params.q);
|
|
|
|
const url = `${REST}/doctors${qs.toString() ? `?${qs.toString()}` : ""}`;
|
|
const res = await fetch(url, {
|
|
method: "GET",
|
|
headers: {
|
|
...baseHeaders(),
|
|
...rangeHeaders(params?.page, params?.limit),
|
|
},
|
|
});
|
|
return await parse<Medico[]>(res);
|
|
}
|
|
|
|
export async function buscarMedicoPorId(id: string | number): Promise<Medico> {
|
|
const url = `${REST}/doctors?id=eq.${id}`;
|
|
const res = await fetch(url, { method: "GET", headers: baseHeaders() });
|
|
const arr = await parse<Medico[]>(res);
|
|
if (!arr?.length) throw new Error("404: Médico não encontrado");
|
|
return arr[0];
|
|
}
|
|
|
|
export async function criarMedico(input: MedicoInput): Promise<Medico> {
|
|
const url = `${REST}/doctors`;
|
|
const res = await fetch(url, {
|
|
method: "POST",
|
|
headers: withPrefer({ ...baseHeaders(), "Content-Type": "application/json" }, "return=representation"),
|
|
body: JSON.stringify(input),
|
|
});
|
|
const arr = await parse<Medico[] | Medico>(res);
|
|
return Array.isArray(arr) ? arr[0] : (arr as Medico);
|
|
}
|
|
|
|
export async function atualizarMedico(id: string | number, input: MedicoInput): Promise<Medico> {
|
|
const url = `${REST}/doctors?id=eq.${id}`;
|
|
const res = await fetch(url, {
|
|
method: "PATCH",
|
|
headers: withPrefer({ ...baseHeaders(), "Content-Type": "application/json" }, "return=representation"),
|
|
body: JSON.stringify(input),
|
|
});
|
|
const arr = await parse<Medico[] | Medico>(res);
|
|
return Array.isArray(arr) ? arr[0] : (arr as Medico);
|
|
}
|
|
|
|
export async function excluirMedico(id: string | number): Promise<void> {
|
|
const url = `${REST}/doctors?id=eq.${id}`;
|
|
const res = await fetch(url, { method: "DELETE", headers: baseHeaders() });
|
|
await parse<any>(res);
|
|
}
|
|
|
|
// ===== Stubs para upload de arquivos de médico =====
|
|
export async function listarAnexosMedico(_id: string | number): Promise<any[]> { return []; }
|
|
export async function adicionarAnexoMedico(_id: string | number, _file: File): Promise<any> { return {}; }
|
|
export async function removerAnexoMedico(_id: string | number, _anexoId: string | number): Promise<void> {}
|
|
export async function uploadFotoMedico(_id: string | number, _file: File): Promise<{ foto_url?: string; thumbnail_url?: string }> { return {}; }
|
|
export async function removerFotoMedico(_id: string | number): Promise<void> {} |