import {z} from 'zod' export type GetApiV2ForecastsStaticRulesArgs = {externalUnitId: string, externalSectionId?: string | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiV2ForecastsStaticRulesOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const forecastDataIngestionExternalShiftType = z.object({externalShiftTypeId: z.string(), amount: z.number().int().lte(100000)}); export const forecastDataIngestionExternalStaticRule = z.object({weekdays: z.array(z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6)])), repeatPeriod: z.number().int().lte(100), shiftType: forecastDataIngestionExternalShiftType, startTime: z.string(), endTime: z.string(), startDate: z.string(), endDate: z.string().optional(), comment: z.string().optional()}); export const forecastDataIngestionPagination = z.object({totalCount: z.number().int().optional(), currentPage: z.number().int().optional(), totalPages: z.number().int().optional()}); export const forecastDataIngestionPaginatedResultListExternalStaticRule = z.object({result: z.array(forecastDataIngestionExternalStaticRule).optional(), pagination: forecastDataIngestionPagination.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 }; /** * List static rules * * GET /v2/forecasts/static-rules */ export const getApiV2ForecastsStaticRules = async (args: GetApiV2ForecastsStaticRulesArgs, options?: GetApiV2ForecastsStaticRulesOptions) => { const {baseUrl = "https://api.quinyx.com", fetch: fetchFn = fetch, OAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.externalUnitId != null) query.append("externalUnitId", String(args.externalUnitId)) if (args.externalSectionId != null) query.append("externalSectionId", String(args.externalSectionId)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/forecasts/static-rules${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(forecastDataIngestionPaginatedResultListExternalStaticRule, await res.json()) };