import { API_PATHS } from "@/api/api-paths"; import { apiGet, apiPost } from "@/api/api"; import { handleApiResponse, handlePaginatedResponse } from "@/api/handle-api-response"; import { mapPmSchedule, mapPmScheduleListItem, mapPmScheduleToSavePayload, } from "@/domain/pm-schedules/mappers/pm-schedule-mapper"; import type { PmSchedule, PmSchedulesListParams, PmSchedulesListResult, } from "@/domain/pm-schedules/types/pm-schedule"; export const pmSchedulesApi = { getList: async (params: PmSchedulesListParams = {}): Promise => { const data = await apiGet(API_PATHS.pmSchedule.getList, { searchParams: { search: params.search ?? "", page: params.page ?? 1, pageSize: params.pageSize ?? 12, }, }); const paginated = handlePaginatedResponse(data, { page: params.page, pageSize: params.pageSize, }); return { items: paginated.items.map(mapPmScheduleListItem), totalCount: paginated.totalCount, page: paginated.page, pageSize: paginated.pageSize, }; }, getById: async (id: string | number): Promise => { const data = await apiGet(API_PATHS.pmSchedule.getById, { searchParams: { id }, }); return mapPmSchedule(handleApiResponse(data)); }, /** Create and update both use PMSchedule/Save — Id=0 for create. */ save: async (schedule: Partial & { id?: number }): Promise => { const payload = mapPmScheduleToSavePayload(schedule); const data = await apiPost(API_PATHS.pmSchedule.save, payload); return mapPmSchedule(handleApiResponse(data)); }, delete: async (id: string | number): Promise => { await apiPost(API_PATHS.pmSchedule.delete, undefined, { searchParams: { id }, }); }, };