119 lines
3.9 KiB
TypeScript
119 lines
3.9 KiB
TypeScript
// Caminho: services/disponibilidadeApi.ts (Completo e Corrigido)
|
|
|
|
import api from './api';
|
|
|
|
// --- Tipagem para Disponibilidade Semanal ---
|
|
|
|
export interface DoctorAvailability {
|
|
id: string;
|
|
doctor_id: string;
|
|
weekday: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
start_time: string;
|
|
end_time: string;
|
|
slot_minutes: number;
|
|
appointment_type: 'presencial' | 'telemedicina';
|
|
active: boolean;
|
|
created_at: string;
|
|
updated_at: string;
|
|
created_by: string;
|
|
}
|
|
|
|
export type CreateDoctorAvailabilityData = Omit<DoctorAvailability, 'id' | 'created_at' | 'updated_at'>;
|
|
export type UpdateDoctorAvailabilityData = Partial<Omit<DoctorAvailability, 'id' | 'created_at' | 'updated_at' | 'created_by'>>;
|
|
|
|
interface ListAvailabilityParams {
|
|
doctor_id?: string;
|
|
weekday?: number;
|
|
active?: boolean;
|
|
}
|
|
|
|
// --- Tipagem para Exceções de Agenda ---
|
|
|
|
export interface DoctorException {
|
|
id: string;
|
|
doctor_id: string;
|
|
date: string;
|
|
kind: 'bloqueio' | 'disponibilidade_extra';
|
|
start_time: string | null;
|
|
end_time: string | null;
|
|
reason?: string;
|
|
created_at: string;
|
|
created_by: string;
|
|
}
|
|
|
|
export type CreateDoctorExceptionData = Omit<DoctorException, 'id' | 'created_at'>;
|
|
|
|
interface ListExceptionParams {
|
|
doctor_id?: string;
|
|
date?: string;
|
|
kind?: 'bloqueio' | 'disponibilidade_extra';
|
|
}
|
|
|
|
// --- Objeto do Serviço de API ---
|
|
|
|
export const disponibilidadeApi = {
|
|
// --- Métodos para Disponibilidade Semanal ---
|
|
|
|
list: async (params: ListAvailabilityParams = {}): Promise<DoctorAvailability[]> => {
|
|
// CORREÇÃO: Formata os parâmetros para o padrão do Supabase
|
|
const queryParams: { [key: string]: string } = { select: '*' };
|
|
if (params.doctor_id) queryParams.doctor_id = `eq.${params.doctor_id}`;
|
|
if (params.weekday !== undefined) queryParams.weekday = `eq.${params.weekday}`;
|
|
if (params.active !== undefined) queryParams.active = `eq.${params.active}`;
|
|
|
|
const response = await api.get<DoctorAvailability[]>('/rest/v1/doctor_availability', {
|
|
params: queryParams,
|
|
});
|
|
return response.data;
|
|
},
|
|
|
|
getById: async (id: string): Promise<DoctorAvailability> => {
|
|
const response = await api.get<DoctorAvailability[]>(`/rest/v1/doctor_availability?id=eq.${id}&select=*`);
|
|
if (response.data && response.data.length > 0) {
|
|
return response.data[0];
|
|
}
|
|
throw new Error("Registro de disponibilidade não encontrado.");
|
|
},
|
|
|
|
create: async (data: CreateDoctorAvailabilityData): Promise<DoctorAvailability> => {
|
|
const response = await api.post<DoctorAvailability[]>('/rest/v1/doctor_availability', data, {
|
|
headers: { 'Prefer': 'return=representation' }
|
|
});
|
|
return response.data[0];
|
|
},
|
|
|
|
update: async (id: string, data: UpdateDoctorAvailabilityData): Promise<DoctorAvailability> => {
|
|
const response = await api.patch<DoctorAvailability[]>(`/rest/v1/doctor_availability?id=eq.${id}`, data, {
|
|
headers: { 'Prefer': 'return=representation' }
|
|
});
|
|
return response.data[0];
|
|
},
|
|
|
|
delete: async (id: string): Promise<void> => {
|
|
await api.delete(`/rest/v1/doctor_availability?id=eq.${id}`);
|
|
},
|
|
|
|
// --- Métodos para Exceções de Agenda ---
|
|
|
|
listExceptions: async (params: ListExceptionParams = {}): Promise<DoctorException[]> => {
|
|
// CORREÇÃO: Formata os parâmetros para o padrão do Supabase
|
|
const queryParams: { [key: string]: string } = { select: '*' };
|
|
if (params.doctor_id) queryParams.doctor_id = `eq.${params.doctor_id}`;
|
|
if (params.date) queryParams.date = `eq.${params.date}`;
|
|
if (params.kind) queryParams.kind = `eq.${params.kind}`;
|
|
|
|
const response = await api.get<DoctorException[]>('/rest/v1/doctor_exceptions', {
|
|
params: queryParams,
|
|
});
|
|
return response.data;
|
|
},
|
|
|
|
createException: async (data: CreateDoctorExceptionData): Promise<DoctorException> => {
|
|
const response = await api.post<DoctorException[]>('/rest/v1/doctor_exceptions', data, {
|
|
headers: {
|
|
'Prefer': 'return=representation',
|
|
},
|
|
});
|
|
return response.data[0];
|
|
},
|
|
}; |