mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-06 07:42:04 +00:00
61 lines
1.9 KiB
TypeScript
61 lines
1.9 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 {
|
|
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,
|
|
};
|
|
}
|
|
|
|
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,
|
|
};
|
|
},
|
|
|
|
getById: async (id: string | number): Promise<Account> => {
|
|
const data = await apiGet<unknown>(`${API_PATHS.rest.accounts}/${id}`);
|
|
return mapAccount(handleApiResponse(data));
|
|
},
|
|
|
|
create: async (payload: unknown): Promise<Account> => {
|
|
const data = await apiPost<unknown>(API_PATHS.rest.accounts, mapAccountToBackend(payload));
|
|
return mapAccount(handleApiResponse(data));
|
|
},
|
|
|
|
update: async (id: string | number, payload: unknown): Promise<Account> => {
|
|
const data = await apiPut<unknown>(
|
|
`${API_PATHS.rest.accounts}/${id}`,
|
|
mapAccountToBackend(payload),
|
|
);
|
|
return mapAccount(handleApiResponse(data));
|
|
},
|
|
|
|
delete: async (id: string | number): Promise<void> => {
|
|
await apiDelete<unknown>(`${API_PATHS.rest.accounts}/${id}`);
|
|
},
|
|
};
|