mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-07 16:19:01 +00:00
35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { API_PATHS } from "@/api/api-paths";
|
|
import { apiGet } from "@/api/api";
|
|
import { handleApiResponse, handlePaginatedResponse } from "@/api/handle-api-response";
|
|
import { mapEmployeeOptions } from "@/domain/work-orders/mappers/work-order-mapper";
|
|
import type { EmployeeOption } from "@/domain/work-orders/types/work-order";
|
|
|
|
async function fetchEmployeesFromList(): Promise<EmployeeOption[]> {
|
|
const data = await apiGet<unknown>(API_PATHS.employee.getList, {
|
|
searchParams: { page: 1, pageSize: 1000 },
|
|
});
|
|
const paginated = handlePaginatedResponse<unknown>(data);
|
|
const items = paginated.items.map((item) => {
|
|
const record = (typeof item === "object" && item !== null ? item : {}) as Record<
|
|
string,
|
|
unknown
|
|
>;
|
|
const firstName = String(record.firstName ?? record.FirstName ?? "");
|
|
const lastName = String(record.lastName ?? record.LastName ?? "");
|
|
return {
|
|
id: record.id ?? record.Id,
|
|
name: `${firstName} ${lastName}`.trim(),
|
|
};
|
|
});
|
|
return mapEmployeeOptions(items);
|
|
}
|
|
|
|
export async function fetchWorkOrderEmployees(): Promise<EmployeeOption[]> {
|
|
try {
|
|
const data = await apiGet<unknown>(API_PATHS.employee.dropdown);
|
|
const items = handleApiResponse<unknown[]>(data);
|
|
return Array.isArray(items) ? mapEmployeeOptions(items) : [];
|
|
} catch {
|
|
return fetchEmployeesFromList();
|
|
}
|
|
}
|