95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
// Caminho: services/agendamentosApi.ts (Completo, com adição de update e delete)
|
|
|
|
import api from './api';
|
|
|
|
export interface Appointment {
|
|
id: string;
|
|
doctor_id: string;
|
|
patient_id: string;
|
|
scheduled_at: string;
|
|
duration_minutes?: number;
|
|
status: 'requested' | 'confirmed' | 'completed' | 'cancelled';
|
|
created_by?: string;
|
|
cancel_reason?: string;
|
|
reschedule_reason?: string;
|
|
appointment_type?: 'presencial' | 'telemedicina';
|
|
notes?: string;
|
|
doctors?: {
|
|
full_name: string;
|
|
specialty: string;
|
|
};
|
|
}
|
|
|
|
export type CreateAppointmentData = Omit<Appointment, 'id' | 'doctors' | 'cancel_reason' | 'reschedule_reason'>;
|
|
export type UpdateAppointmentData = Partial<Omit<Appointment, 'id' | 'doctors' | 'created_by' | 'patient_id' | 'doctor_id'>>;
|
|
|
|
export interface AvailableSlot {
|
|
time: string;
|
|
available: boolean;
|
|
}
|
|
|
|
export const agendamentosApi = {
|
|
listByPatient: async (patientId: string): Promise<Appointment[]> => {
|
|
const response = await api.get<Appointment[]>('/rest/v1/appointments', {
|
|
params: {
|
|
patient_id: `eq.${patientId}`,
|
|
select: '*,doctors(full_name,specialty)',
|
|
order: 'scheduled_at.asc',
|
|
},
|
|
});
|
|
return response.data;
|
|
},
|
|
|
|
create: async (data: CreateAppointmentData): Promise<Appointment> => {
|
|
const response = await api.post<Appointment[]>('/rest/v1/appointments', data, {
|
|
headers: { 'Prefer': 'return=representation' },
|
|
});
|
|
return response.data[0];
|
|
},
|
|
|
|
/**
|
|
* Atualiza um agendamento existente (PATCH).
|
|
* @param id - O UUID do agendamento.
|
|
* @param data - Os campos a serem atualizados.
|
|
*/
|
|
update: async (id: string, data: UpdateAppointmentData): Promise<Appointment> => {
|
|
const response = await api.patch<Appointment[]>(`/rest/v1/appointments?id=eq.${id}`, data, {
|
|
headers: {
|
|
'Prefer': 'return=representation',
|
|
},
|
|
});
|
|
return response.data[0];
|
|
},
|
|
|
|
/**
|
|
* Exclui um agendamento.
|
|
* @param id - O UUID do agendamento a ser excluído.
|
|
*/
|
|
delete: async (id: string): Promise<void> => {
|
|
await api.delete(`/rest/v1/appointments?id=eq.${id}`);
|
|
},
|
|
|
|
getAvailableSlots: async (doctorId: string, date: string): Promise<{ slots: AvailableSlot[] }> => {
|
|
const response = await api.post<{ slots: AvailableSlot[] }>('/functions/v1/get-available-slots', {
|
|
doctor_id: doctorId,
|
|
date: date,
|
|
});
|
|
return response.data;
|
|
},
|
|
|
|
getMockAppointments: async (): Promise<Appointment[]> => {
|
|
const response = await api.get<any[]>('https://mock.apidog.com/m1/1053378-0-default/rest/v1/doctors');
|
|
return response.data.map((doctor: any, index: number) => ({
|
|
id: `mock-${index + 1}`,
|
|
doctor_id: doctor.id || `doc-mock-${index + 1}`,
|
|
patient_id: 'patient-mock-1',
|
|
scheduled_at: new Date(Date.now() + 86400000 * (index + 2)).toISOString(),
|
|
status: index % 2 === 0 ? 'confirmed' : 'requested',
|
|
appointment_type: index % 2 === 0 ? 'presencial' : 'telemedicina',
|
|
doctors: {
|
|
full_name: doctor.full_name || `Dr. Exemplo ${index + 1}`,
|
|
specialty: doctor.specialty || 'Especialidade',
|
|
},
|
|
}));
|
|
},
|
|
}; |