2026-06-12 11:53:01 -03:00
|
|
|
import { API_PATHS } from "@/api/api-paths";
|
2026-06-15 11:17:40 -03:00
|
|
|
import { apiGet, apiPost } from "@/api/api";
|
2026-06-12 11:53:01 -03:00
|
|
|
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> => {
|
2026-06-15 11:17:40 -03:00
|
|
|
const data = await apiGet<unknown>(API_PATHS.account.getById, {
|
|
|
|
|
searchParams: { id },
|
|
|
|
|
});
|
2026-06-12 11:53:01 -03:00
|
|
|
return mapAccount(handleApiResponse(data));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
create: async (payload: unknown): Promise<Account> => {
|
2026-06-15 11:17:40 -03:00
|
|
|
const data = await apiPost<unknown>(
|
|
|
|
|
API_PATHS.account.addAccounts,
|
|
|
|
|
mapAccountToBackend(payload),
|
|
|
|
|
);
|
2026-06-12 11:53:01 -03:00
|
|
|
return mapAccount(handleApiResponse(data));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
update: async (id: string | number, payload: unknown): Promise<Account> => {
|
2026-06-15 11:17:40 -03:00
|
|
|
const data = await apiPost<unknown>(
|
|
|
|
|
API_PATHS.account.editAccounts,
|
|
|
|
|
mapAccountToBackend({ ...(payload as object), id }),
|
2026-06-12 11:53:01 -03:00
|
|
|
);
|
|
|
|
|
return mapAccount(handleApiResponse(data));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
delete: async (id: string | number): Promise<void> => {
|
2026-06-15 11:17:40 -03:00
|
|
|
await apiGet<unknown>(API_PATHS.account.deleteAccounts, {
|
|
|
|
|
searchParams: { id },
|
|
|
|
|
});
|
2026-06-12 11:53:01 -03:00
|
|
|
},
|
|
|
|
|
};
|