import {z} from 'zod' export type Periodicity = 'Daily' | 'Weekly' | 'Monthly' | 'Quarterly' | 'Annually'; export type TurnoverEvolutionType = 'Sales' | 'Expenses' | 'Customer' | 'Supplier' | 'Product' | 'Colleague' | 'Margin' | 'Discount'; export type GetApiV1StatisticsTurnoverevolutionArgs = {from?: string | undefined, to?: string | undefined, year?: number | undefined, periodicity?: Periodicity | undefined, compare?: number | undefined, type?: TurnoverEvolutionType | undefined, cumulative?: boolean | undefined, customerId?: number | undefined, supplierId?: number | undefined, colleagueId?: number | undefined, productId?: number | undefined}; export type GetApiV1StatisticsTurnoverevolutionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const periodicity = z.enum(["Daily", "Weekly", "Monthly", "Quarterly", "Annually"]); export const dateRangeModel = z.object({from: z.string().optional(), to: z.string().optional()}); export const periodBucketModel = z.object({partial: z.boolean().optional(), range: dateRangeModel.optional(), value: z.number().optional()}); export const periodSeriesModel = z.object({buckets: z.array(periodBucketModel).nullable().optional(), label: z.string().nullable().optional(), offset: z.number().int().optional(), range: dateRangeModel.optional()}); export const turnoverEvolutionResult = z.object({periodicity: periodicity.optional(), range: dateRangeModel.optional(), series: z.array(periodSeriesModel).nullable().optional()}); 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 }; /** * Turnover over a period, bucketed, optionally compared against the same period in previous years * * GET /v1/statistics/turnoverevolution */ export const getApiV1StatisticsTurnoverevolution = async (args: GetApiV1StatisticsTurnoverevolutionArgs, options?: GetApiV1StatisticsTurnoverevolutionOptions) => { const {baseUrl = "https://api.altoviz.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.year != null) query.append("year", String(args.year)) if (args.periodicity != null) query.append("periodicity", String(args.periodicity)) if (args.compare != null) query.append("compare", String(args.compare)) if (args.type != null) query.append("type", String(args.type)) if (args.cumulative != null) query.append("cumulative", String(args.cumulative)) if (args.customerId != null) query.append("customerId", String(args.customerId)) if (args.supplierId != null) query.append("supplierId", String(args.supplierId)) if (args.colleagueId != null) query.append("colleagueId", String(args.colleagueId)) if (args.productId != null) query.append("productId", String(args.productId)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/statistics/turnoverevolution${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(turnoverEvolutionResult, await res.json()) };