import {z} from 'zod' export type PriceType = 0 | 1; export type CompanyPriceSheetRow = {itemCode?: (string | null) | undefined, price?: number | undefined, quantity?: number | undefined, fromDate?: string | undefined, endDate?: (string | null) | undefined}; export type CreateApiV1AccountsAccountIdPricesBulkArgs = {accountId: number, type: PriceType, body: Array}; export type CreateApiV1AccountsAccountIdPricesBulkOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1AccountsAccountIdPricesBulkResponse = z.number().int(); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Bulk-upsert one company's price rows from the company price sheet (re-import of the exported * `.xlsx`). Body fields match M:IO.CommonAPI.Controllers.v1.Catalog.ItemPricesController.DownloadXlsxByAccount(System.Int32,IO.Domain.Shared.Enums.PriceType,IO.Catalog.Contracts.DTO.Pricelists.AccountPricesExportRequest,System.Threading.CancellationToken)'s columns exactly; every * row is bound to the route's company, and each row's `itemCode` selects the item. Returns * the count upserted. * * POST /v1/Accounts/{accountId}/prices/bulk */ export const createApiV1AccountsAccountIdPricesBulk = async (args: CreateApiV1AccountsAccountIdPricesBulkArgs, options: CreateApiV1AccountsAccountIdPricesBulkOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json-patch+json; x-api-version=1.0") headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Accounts/${encodeURIComponent(String(args.accountId))}/prices/bulk${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1AccountsAccountIdPricesBulkResponse, await res.json()) };