mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-07 06:59:01 +00:00
fix(employees): add fallbacks for detail and dropdown calls
This commit is contained in:
parent
6beba0ba93
commit
0187556d03
1 changed files with 61 additions and 5 deletions
|
|
@ -20,6 +20,37 @@ function buildSearchParams(params: EmployeesListParams): Record<string, string |
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function findEmployeeInList(id: string | number): Promise<Employee | null> {
|
||||||
|
const targetId = String(id);
|
||||||
|
let page = 1;
|
||||||
|
const pageSize = 100;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const data = await apiGet<unknown>(API_PATHS.employee.getList, {
|
||||||
|
searchParams: { search: "", page, pageSize },
|
||||||
|
});
|
||||||
|
const paginated = handlePaginatedResponse<unknown>(data, { page, pageSize });
|
||||||
|
const match = paginated.items.find((item) => {
|
||||||
|
const record = (typeof item === "object" && item !== null ? item : {}) as Record<
|
||||||
|
string,
|
||||||
|
unknown
|
||||||
|
>;
|
||||||
|
const itemId = record.id ?? record.Id;
|
||||||
|
return String(itemId) === targetId;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
return transformEmployee(match);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (paginated.items.length === 0 || page * pageSize >= paginated.totalCount) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
page += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const employeesApi = {
|
export const employeesApi = {
|
||||||
getList: async (params: EmployeesListParams = {}): Promise<EmployeesListResult> => {
|
getList: async (params: EmployeesListParams = {}): Promise<EmployeesListResult> => {
|
||||||
const searchParams = buildSearchParams(params);
|
const searchParams = buildSearchParams(params);
|
||||||
|
|
@ -38,10 +69,14 @@ export const employeesApi = {
|
||||||
},
|
},
|
||||||
|
|
||||||
getById: async (id: string | number): Promise<Employee | null> => {
|
getById: async (id: string | number): Promise<Employee | null> => {
|
||||||
const data = await apiGet<unknown>(API_PATHS.employee.getById, {
|
try {
|
||||||
searchParams: { id },
|
const data = await apiGet<unknown>(API_PATHS.employee.getById, {
|
||||||
});
|
searchParams: { id },
|
||||||
return transformEmployee(handleApiResponse(data));
|
});
|
||||||
|
return transformEmployee(handleApiResponse(data));
|
||||||
|
} catch {
|
||||||
|
return findEmployeeInList(id);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
create: async (payload: Employee): Promise<Employee | null> => {
|
create: async (payload: Employee): Promise<Employee | null> => {
|
||||||
|
|
@ -66,5 +101,26 @@ export const employeesApi = {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getDropdown: () => apiGet<unknown>(API_PATHS.employee.dropdown),
|
getDropdown: async () => {
|
||||||
|
try {
|
||||||
|
return await apiGet<unknown>(API_PATHS.employee.dropdown);
|
||||||
|
} catch {
|
||||||
|
const data = await apiGet<unknown>(API_PATHS.employee.getList, {
|
||||||
|
searchParams: { page: 1, pageSize: 1000 },
|
||||||
|
});
|
||||||
|
const paginated = handlePaginatedResponse<unknown>(data);
|
||||||
|
return 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(),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue