mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-01 23:13:12 +00:00
71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
import { API_PATHS } from "@/api/api-paths";
|
|
import { apiDelete, apiGet, apiPost, apiPut } from "@/api/api";
|
|
import { handleApiResponse, handlePaginatedResponse } from "@/api/handle-api-response";
|
|
import {
|
|
mapLocation,
|
|
mapLocationListItems,
|
|
mapLocationSites,
|
|
mapLocationToBackend,
|
|
} from "@/domain/locations/mappers/location-mapper";
|
|
import type {
|
|
Location,
|
|
LocationSite,
|
|
LocationsListParams,
|
|
LocationsListResult,
|
|
} from "@/domain/locations/types/location";
|
|
|
|
function buildSearchParams(params: LocationsListParams): Record<string, string | number> {
|
|
return {
|
|
search: params.search ?? "",
|
|
page: params.page ?? 1,
|
|
pageSize: params.pageSize ?? 12,
|
|
};
|
|
}
|
|
|
|
export const locationsApi = {
|
|
getList: async (params: LocationsListParams = {}): Promise<LocationsListResult> => {
|
|
const searchParams = buildSearchParams(params);
|
|
const data = await apiGet<unknown>(API_PATHS.location.getList, { searchParams });
|
|
const paginated = handlePaginatedResponse<unknown>(data, {
|
|
page: params.page,
|
|
pageSize: params.pageSize,
|
|
});
|
|
|
|
return {
|
|
items: mapLocationListItems(paginated.items),
|
|
totalCount: paginated.totalCount,
|
|
page: paginated.page,
|
|
pageSize: paginated.pageSize,
|
|
};
|
|
},
|
|
|
|
getById: async (id: string | number): Promise<Location> => {
|
|
const data = await apiGet<unknown>(`${API_PATHS.rest.locations}/${id}`);
|
|
return mapLocation(handleApiResponse(data));
|
|
},
|
|
|
|
getSites: async (search?: string): Promise<LocationSite[]> => {
|
|
const data = await apiGet<unknown>(API_PATHS.location.sites, {
|
|
searchParams: search?.trim() ? { search: search.trim() } : undefined,
|
|
});
|
|
const items = handleApiResponse<unknown[]>(data);
|
|
return Array.isArray(items) ? mapLocationSites(items) : [];
|
|
},
|
|
|
|
create: async (payload: unknown): Promise<Location> => {
|
|
const data = await apiPost<unknown>(API_PATHS.rest.locations, mapLocationToBackend(payload));
|
|
return mapLocation(handleApiResponse(data));
|
|
},
|
|
|
|
update: async (id: string | number, payload: unknown): Promise<Location> => {
|
|
const data = await apiPut<unknown>(
|
|
`${API_PATHS.rest.locations}/${id}`,
|
|
mapLocationToBackend(payload),
|
|
);
|
|
return mapLocation(handleApiResponse(data));
|
|
},
|
|
|
|
delete: async (id: string | number): Promise<void> => {
|
|
await apiDelete<unknown>(`${API_PATHS.rest.locations}/${id}`);
|
|
},
|
|
};
|