import {z} from 'zod' export type ScheduleAbsenceRequest = {absenceTypeId: number, employeeId: number, begin: string, end: string, employeeAttested?: boolean | undefined, managerAttested?: boolean | undefined, absenceScheduleId?: number | undefined, shiftsInThisPeriod?: ('UNASSIGN' | 'DELETE' | 'KEEP') | undefined, managerComment?: string | undefined}; export type CreateApiV2ScheduleGroupsGroupIdAbsencesArgs = {groupId: number, overlapAbsences?: boolean | undefined, ignoreOverridableValidations?: boolean | undefined, body: ScheduleAbsenceRequest}; export type CreateApiV2ScheduleGroupsGroupIdAbsencesOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleAbsenceResponse = z.object({id: z.number().int().optional(), groupId: z.number().int().optional(), absenceTypeId: z.number().int().optional(), employeeId: z.number().int().optional(), begin: z.string().optional(), end: z.string().optional(), employeeAttested: z.boolean().optional(), managerAttested: z.boolean().optional(), absenceScheduleId: z.number().int().optional(), shiftsInThisPeriod: z.enum(["UNASSIGN", "DELETE", "KEEP"]).optional(), managerComment: z.string().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 }; /** * Create full-time absence for employee * * POST /v2/schedule/groups/{groupId}/absences */ export const createApiV2ScheduleGroupsGroupIdAbsences = async (args: CreateApiV2ScheduleGroupsGroupIdAbsencesArgs, options?: CreateApiV2ScheduleGroupsGroupIdAbsencesOptions) => { 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 (args.overlapAbsences != null) query.append("overlapAbsences", String(args.overlapAbsences)) if (args.ignoreOverridableValidations != null) query.append("ignoreOverridableValidations", String(args.ignoreOverridableValidations)) if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/schedule/groups/${encodeURIComponent(String(args.groupId))}/absences${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(scheduleAbsenceResponse, await res.json()) };