mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-06 14:42:05 +00:00
95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
import { API_PATHS } from "@/api/api-paths";
|
|
import { apiGet, apiPost } from "@/api/api";
|
|
import { handleApiResponse, handlePaginatedResponse } from "@/api/handle-api-response";
|
|
import {
|
|
mapAccount,
|
|
mapAccountListItems,
|
|
mapAccountToBackend,
|
|
} from "@/domain/accounts/mappers/account-mapper";
|
|
import type {
|
|
Account,
|
|
AccountsListParams,
|
|
AccountsListResult,
|
|
} from "@/domain/accounts/types/account";
|
|
|
|
function buildSearchParams(params: AccountsListParams): Record<string, string | number> {
|
|
return {
|
|
search: params.search ?? "",
|
|
page: params.page ?? 1,
|
|
pageSize: params.pageSize ?? 12,
|
|
};
|
|
}
|
|
|
|
async function findAccountInList(id: string | number): Promise<Account> {
|
|
const targetId = String(id);
|
|
let page = 1;
|
|
const pageSize = 100;
|
|
|
|
while (true) {
|
|
const data = await apiGet<unknown>(API_PATHS.account.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 mapAccount(match);
|
|
}
|
|
|
|
if (paginated.items.length === 0 || page * pageSize >= paginated.totalCount) {
|
|
throw new Error("Account not found");
|
|
}
|
|
|
|
page += 1;
|
|
}
|
|
}
|
|
|
|
export const accountsApi = {
|
|
getList: async (params: AccountsListParams = {}): Promise<AccountsListResult> => {
|
|
const searchParams = buildSearchParams(params);
|
|
const data = await apiGet<unknown>(API_PATHS.account.getList, { searchParams });
|
|
const paginated = handlePaginatedResponse<unknown>(data, {
|
|
page: params.page,
|
|
pageSize: params.pageSize,
|
|
});
|
|
|
|
return {
|
|
items: mapAccountListItems(paginated.items),
|
|
totalCount: paginated.totalCount,
|
|
page: paginated.page,
|
|
pageSize: paginated.pageSize,
|
|
};
|
|
},
|
|
|
|
/** Backend has no GetById — resolve from paginated list. */
|
|
getById: async (id: string | number): Promise<Account> => findAccountInList(id),
|
|
|
|
create: async (payload: unknown): Promise<Account> => {
|
|
const data = await apiPost<unknown>(
|
|
API_PATHS.account.addAccounts,
|
|
mapAccountToBackend(payload),
|
|
);
|
|
return mapAccount(handleApiResponse(data));
|
|
},
|
|
|
|
update: async (id: string | number, payload: unknown): Promise<Account> => {
|
|
const data = await apiPost<unknown>(
|
|
API_PATHS.account.editAccounts,
|
|
mapAccountToBackend({ ...(payload as object), id }),
|
|
);
|
|
return mapAccount(handleApiResponse(data));
|
|
},
|
|
|
|
delete: async (id: string | number): Promise<void> => {
|
|
await apiGet<unknown>(API_PATHS.account.deleteAccounts, {
|
|
searchParams: { id },
|
|
});
|
|
},
|
|
};
|