2025-11-21 17:26:43 +03:00

309 lines
6.6 KiB
TypeScript

import { request } from "./http";
export interface Intervention {
id?: number;
client_id: number;
deceased_id: number;
order_giver?: string;
location_id?: number;
type?: string;
scheduled_at?: string;
duration_min?: number;
status?: string;
attachments_count?: number;
notes?: string;
created_by?: number;
// Relations
client?: any;
deceased?: any;
practitioners?: any[];
principal_practitioner?: any;
location?: any;
// Timestamps
created_at?: string;
updated_at?: string;
}
export interface InterventionListResponse {
data: Intervention[];
meta?: {
current_page: number;
last_page: number;
per_page: number;
total: number;
};
}
export interface InterventionResponse {
data: Intervention;
}
export interface CreateInterventionPayload {
client_id: number;
deceased_id: number;
order_giver?: string;
location_id?: number;
type?: string;
scheduled_at?: string;
duration_min?: number;
status?: string;
practitioners?: number[];
principal_practitioner_id?: number;
assistant_practitioner_ids?: number[];
notes?: string;
created_by?: number;
}
export interface UpdateInterventionPayload
extends Partial<CreateInterventionPayload> {
id: number;
}
export const InterventionService = {
/**
* Get all interventions with pagination and filters
*/
async getAllInterventions(params?: {
page?: number;
per_page?: number;
search?: string;
deceased_id?: number;
client_id?: number;
status?: string;
scheduled_at?: string;
sort_by?: string;
sort_order?: "asc" | "desc";
}): Promise<InterventionListResponse> {
const response = await request<InterventionListResponse>({
url: "/api/interventions",
method: "get",
params,
});
return response;
},
/**
* Get a specific intervention by ID
*/
async getIntervention(id: number): Promise<Intervention> {
const response = await request<Intervention>({
url: `/api/interventions/${id}`,
method: "get",
});
return response;
},
/**
* Create a new intervention
*/
async createIntervention(
payload: CreateInterventionPayload
): Promise<Intervention> {
const response = await request<Intervention>({
url: "/api/interventions",
method: "post",
data: payload,
});
return response;
},
/**
* Create a new intervention with all data (deceased, client, location, documents)
*/
async createInterventionWithAllData(
formData: FormData
): Promise<Intervention> {
const response = await request<Intervention>({
url: "/api/interventions/with-all-data",
method: "post",
data: formData,
});
return response;
},
/**
* Update an existing intervention
*/
async updateIntervention(
payload: UpdateInterventionPayload
): Promise<Intervention> {
const { id, ...updateData } = payload;
const response = await request<Intervention>({
url: `/api/interventions/${id}`,
method: "put",
data: updateData,
});
return response;
},
/**
* Delete an intervention
*/
async deleteIntervention(
id: number
): Promise<{ success: boolean; message: string }> {
const response = await request<{ success: boolean; message: string }>({
url: `/api/interventions/${id}`,
method: "delete",
});
return response;
},
/**
* Get interventions by deceased ID
*/
async getInterventionsByDeceased(
deceasedId: number,
params?: {
page?: number;
per_page?: number;
}
): Promise<InterventionListResponse> {
const response = await request<InterventionListResponse>({
url: `/api/deceased/${deceasedId}/interventions`,
method: "get",
params,
});
return response;
},
/**
* Get interventions by client ID
*/
async getInterventionsByClient(
clientId: number,
params?: {
page?: number;
per_page?: number;
}
): Promise<InterventionListResponse> {
const response = await request<InterventionListResponse>({
url: `/api/clients/${clientId}/interventions`,
method: "get",
params,
});
return response;
},
/**
* Search interventions by various criteria
*/
async searchInterventions(
query: string,
params?: {
exact_match?: boolean;
}
): Promise<Intervention[]> {
const response = await request<{
data: Intervention[];
count: number;
message: string;
}>({
url: "/api/interventions/search",
method: "get",
params: {
search: query,
exact_match: params?.exact_match || false,
},
});
return response.data;
},
/**
* Update intervention status
*/
async updateInterventionStatus(
id: number,
status: string
): Promise<Intervention> {
const response = await request<Intervention>({
url: `/api/interventions/${id}/status`,
method: "patch",
data: { status },
});
return response;
},
/**
* Assign practitioner(s) to intervention
*/
async assignPractitioner(
id: number,
practitionerData: {
practitioners?: number[];
principal_practitioner_id?: number;
assistant_practitioner_ids?: number[];
}
): Promise<Intervention> {
const response = await request<Intervention>({
url: `/api/interventions/${id}/assign`,
method: "patch",
data: practitionerData,
});
return response;
},
/**
* Assign multiple practitioners to intervention
*/
async assignPractitioners(
id: number,
practitionerIds: number[],
principalPractitionerId?: number
): Promise<Intervention> {
return this.assignPractitioner(id, {
practitioners: practitionerIds,
principal_practitioner_id: principalPractitionerId,
});
},
/**
* Update practitioners for intervention (replace all existing)
*/
async updatePractitioners(
id: number,
practitionerData: {
practitioners?: number[];
principal_practitioner_id?: number;
assistant_practitioner_ids?: number[];
}
): Promise<Intervention> {
const response = await request<Intervention>({
url: `/api/interventions/${id}/practitioners`,
method: "patch",
data: practitionerData,
});
return response;
},
/**
* Get interventions by month
*/
async getInterventionsByMonth(
year: number,
month: number
): Promise<InterventionListResponse> {
const response = await request<InterventionListResponse>({
url: "/api/interventions/by-month",
method: "get",
params: { year, month },
});
return response;
},
};
export default InterventionService;