import {z} from 'zod' export type StatisticsStatisticsPeriod = {start?: string | undefined, end?: string | undefined, 'aggregation-type'?: ('m' | 'H' | 'D' | 'W' | 'M') | undefined, 'aggregation-value'?: number | undefined}; export type StatisticsTimeRangeFilter = {enabled?: boolean | undefined, from?: string | undefined, to?: string | undefined}; export type StatisticsFilter = {name?: ('employee' | 'employee-skill' | 'employee-section' | 'employee-unit' | 'staff-category' | 'shift-type' | 'shift-status' | 'section' | 'shift-id' | 'shift-start' | 'shift-end' | 'task-status' | 'task-type' | 'task-section' | 'punch-type' | 'punch-id' | 'absence-type' | 'absence-shift-id' | 'attest-status' | 'absence-attest-status' | 'punch-attest-status' | 'tag-ids' | 'agreement' | 'base-schedule') | undefined, values?: Array | undefined, timeRange?: StatisticsTimeRangeFilter | undefined}; export type StatisticsBaseScheduleData = {baseScheduleId?: string | undefined, dayIndex?: number | undefined}; export type StatisticsAdvancedStatisticsRequest = {groupIds?: Array | undefined, period?: StatisticsStatisticsPeriod | undefined, variables?: (Array<'scheduled-hours' | 'worked-hours' | 'leave-hours' | 'optimal-hours' | 'forecast-hours' | 'forecast-sales' | 'actual-sales' | 'actual-staff-cost' | 'forecast-staff-cost' | 'base-schedule-hours' | 'productive-hours' | 'kpi-scheduled-hours' | 'kpi-scheduled-hours-excl-ut' | 'kpi-worked-hours' | 'scheduled-staff-cost' | 'scheduled-staff-cost-excluding-absences' | 'scheduled-hours-excluding-absences' | 'worked-hours-excluding-absences' | 'expected-staff-cost' | 'scheduled-break-hours' | 'punched-break-hours' | 'base-schedule-cost' | 'expected-kpi-scheduled-hours' | 'expected-kpi-worked-hours'>) | undefined, filters?: Array | undefined, baseScheduleData?: StatisticsBaseScheduleData | undefined, baseScheduleIds?: Array | undefined, displayItems?: (Array<'people' | 'shifts' | 'tasks' | 'punches' | 'absences'>) | undefined, laborView?: boolean | undefined, statisticsFilterTypes?: ('and' | 'or') | undefined, 'people-filter-type'?: ('and' | 'or') | undefined, 'shifts-filter-type'?: ('and' | 'or') | undefined, 'tasks-filter-type'?: ('and' | 'or') | undefined, 'punches-filter-type'?: ('and' | 'or') | undefined, 'absences-filter-type'?: ('and' | 'or') | undefined}; export type CreateApiV2StatisticsStandardVariablesArgs = {body: StatisticsAdvancedStatisticsRequest}; export type CreateApiV2StatisticsStandardVariablesOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const statisticsStatisticPeriodValue = z.object({start: z.string().optional(), end: z.string().optional(), value: z.number().optional(), employeeId: z.number().int().optional()}); export const statisticsStatistic = z.object({variable: z.enum(["scheduled-hours", "worked-hours", "leave-hours", "optimal-hours", "forecast-hours", "forecast-sales", "actual-sales", "actual-staff-cost", "forecast-staff-cost", "base-schedule-hours", "productive-hours", "kpi-scheduled-hours", "kpi-scheduled-hours-excl-ut", "kpi-worked-hours", "scheduled-staff-cost", "scheduled-staff-cost-excluding-absences", "scheduled-hours-excluding-absences", "worked-hours-excluding-absences", "expected-staff-cost", "scheduled-break-hours", "punched-break-hours", "base-schedule-cost", "expected-kpi-scheduled-hours", "expected-kpi-worked-hours"]).optional(), values: z.array(statisticsStatisticPeriodValue).optional(), "total-value": z.number().optional()}); export const createApiV2StatisticsStandardVariablesResponse = z.array(statisticsStatistic); 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 standard variables * * POST /v2/statistics/standard-variables */ export const createApiV2StatisticsStandardVariables = async (args: CreateApiV2StatisticsStandardVariablesArgs, options?: CreateApiV2StatisticsStandardVariablesOptions) => { const {baseUrl = "https://api.quinyx.com", fetch: fetchFn = fetch, OAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/statistics/standard-variables${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(createApiV2StatisticsStandardVariablesResponse, await res.json()) };