2026-09-16 18:42:31 -03:00
|
|
|
import { apiGet, apiPost, apiPut } from "@/api/api";
|
|
|
|
|
import { API_PATHS } from "@/api/api-paths";
|
|
|
|
|
import { handleApiResponse } from "@/api/handle-api-response";
|
2026-09-16 19:13:00 -03:00
|
|
|
import {
|
|
|
|
|
mapCompletionDocTemplates,
|
|
|
|
|
mapService,
|
|
|
|
|
mapServices,
|
|
|
|
|
} from "@/domain/services/mappers/service-mapper";
|
|
|
|
|
import type {
|
|
|
|
|
CompletionDocTemplateOption,
|
|
|
|
|
Service,
|
|
|
|
|
ServiceInput,
|
|
|
|
|
} from "@/domain/services/types/service";
|
2026-09-16 18:42:31 -03:00
|
|
|
|
2026-09-16 19:05:02 -03:00
|
|
|
const SERVICE_WORK_ORDER_TYPE_API = {
|
|
|
|
|
PM: 2,
|
|
|
|
|
Emergency: 3,
|
|
|
|
|
Reactive: 6,
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
function toApiInput(input: ServiceInput) {
|
|
|
|
|
return {
|
|
|
|
|
...input,
|
|
|
|
|
supportedWorkOrderTypes: input.supportedWorkOrderTypes.map(
|
|
|
|
|
(type) => SERVICE_WORK_ORDER_TYPE_API[type],
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-16 18:42:31 -03:00
|
|
|
export const servicesApi = {
|
|
|
|
|
getAll: async (isActive?: boolean): Promise<Service[]> => {
|
|
|
|
|
const data = await apiGet<unknown>(API_PATHS.services.list, {
|
|
|
|
|
searchParams: isActive === undefined ? undefined : { isActive },
|
|
|
|
|
});
|
|
|
|
|
return mapServices(handleApiResponse(data));
|
|
|
|
|
},
|
|
|
|
|
|
2026-09-16 19:13:00 -03:00
|
|
|
getCompletionTemplates: async (): Promise<CompletionDocTemplateOption[]> => {
|
|
|
|
|
const data = await apiGet<unknown>(API_PATHS.workOrder.completionTemplates);
|
|
|
|
|
return mapCompletionDocTemplates(handleApiResponse(data));
|
|
|
|
|
},
|
|
|
|
|
|
2026-09-16 18:42:31 -03:00
|
|
|
create: async (input: ServiceInput): Promise<Service> => {
|
2026-09-16 19:05:02 -03:00
|
|
|
const data = await apiPost<unknown>(API_PATHS.services.list, toApiInput(input));
|
2026-09-16 18:42:31 -03:00
|
|
|
return mapService(handleApiResponse(data));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
update: async (id: string | number, input: ServiceInput): Promise<Service> => {
|
2026-09-16 19:05:02 -03:00
|
|
|
const data = await apiPut<unknown>(API_PATHS.services.byId(id), toApiInput(input));
|
2026-09-16 18:42:31 -03:00
|
|
|
return mapService(handleApiResponse(data));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
deactivate: async (id: string | number): Promise<void> => {
|
|
|
|
|
await apiPost(API_PATHS.services.deactivate(id));
|
|
|
|
|
},
|
|
|
|
|
};
|