92 lines
2.6 KiB
TypeScript
92 lines
2.6 KiB
TypeScript
// Caminho: services/pacientesApi.ts (Completo e Corrigido)
|
|
|
|
import api from './api';
|
|
|
|
export interface Patient {
|
|
id: string;
|
|
nome_completo: string;
|
|
cpf: string;
|
|
email?: string;
|
|
telefone?: string;
|
|
data_nascimento?: string;
|
|
endereco?: string;
|
|
cidade?: string;
|
|
estado?: string;
|
|
cep?: string;
|
|
convenio?: string;
|
|
created_at?: string;
|
|
updated_at?: string;
|
|
[key: string]: any;
|
|
}
|
|
|
|
export type CreatePatientData = Omit<Patient, 'id' | 'created_at' | 'updated_at'>;
|
|
export type UpdatePatientData = Partial<CreatePatientData>;
|
|
|
|
interface ListParams {
|
|
limit?: number;
|
|
offset?: number;
|
|
order?: string;
|
|
nome_completo?: string;
|
|
cpf?: string;
|
|
}
|
|
|
|
export const pacientesApi = {
|
|
list: async (params: ListParams = {}): Promise<Patient[]> => {
|
|
const response = await api.get<Patient[]>('/rest/v1/patients', {
|
|
params: {
|
|
select: '*',
|
|
...params,
|
|
},
|
|
});
|
|
return response.data;
|
|
},
|
|
|
|
/**
|
|
* Busca um único paciente pelo seu ID.
|
|
* @param id - O UUID do paciente.
|
|
*/
|
|
getById: async (id: string): Promise<Patient> => {
|
|
const response = await api.get<Patient[]>(`/rest/v1/patients`, {
|
|
params: {
|
|
id: `eq.${id}`,
|
|
select: '*',
|
|
},
|
|
// O header 'Accept' foi REMOVIDO para evitar o erro 406.
|
|
// A resposta será um array, então pegamos o primeiro item.
|
|
});
|
|
if (response.data && response.data.length > 0) {
|
|
return response.data[0];
|
|
}
|
|
throw new Error("Paciente não encontrado");
|
|
},
|
|
|
|
create: async (data: CreatePatientData): Promise<Patient> => {
|
|
const response = await api.post<Patient[]>('/rest/v1/patients', data, {
|
|
headers: {
|
|
'Prefer': 'return=representation',
|
|
}
|
|
});
|
|
return response.data[0];
|
|
},
|
|
|
|
update: async (id: string, data: UpdatePatientData): Promise<Patient> => {
|
|
const response = await api.patch<Patient[]>(`/rest/v1/patients?id=eq.${id}`, data, {
|
|
headers: {
|
|
'Prefer': 'return=representation',
|
|
}
|
|
});
|
|
return response.data[0];
|
|
},
|
|
|
|
delete: async (id: string): Promise<void> => {
|
|
await api.delete(`/rest/v1/patients?id=eq.${id}`);
|
|
},
|
|
getMockPatient: async (): Promise<Patient> => {
|
|
// Usamos uma instância separada do axios ou o `api.get` com a URL completa
|
|
// para chamar um domínio diferente.
|
|
const response = await api.get<Patient>('https://mock.apidog.com/m1/1053378-0-default/rest/v1/patients');
|
|
// Assumindo que o mock retorna um array, pegamos o primeiro item.
|
|
// Se retornar um objeto direto, seria apenas `response.data`.
|
|
return Array.isArray(response.data) ? response.data[0] : response.data;
|
|
},
|
|
}; |