import {z} from 'zod' export type RotationTimeWindow = {days?: Array | undefined, end_minute: number, start_minute: number}; export type ShiftRule = {anchor_micros: number, ends_at?: (number | null) | undefined, members: Array, name?: string | undefined, priority?: number | undefined, restrictions?: Array | undefined, shift_micros: number, starts_at?: (number | null) | undefined}; export type Rotation = {id: string, name: string, shift_rules?: Array | undefined, source?: (string | null) | undefined}; export type SetScheduleRequest = {rotations?: Array | undefined, timezone?: (string | null) | undefined}; export type UpdateApiApiOrgIdOncallTeamsTeamIdScheduleArgs = {org_id: string, team_id: string, body: SetScheduleRequest}; export type UpdateApiApiOrgIdOncallTeamsTeamIdScheduleOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleOverride = z.object({covering_for: z.string().nullable().optional(), created_at: z.number().int(), created_by: z.string(), end_at: z.number().int(), id: z.string(), org_id: z.string(), reason: z.string().nullable().optional(), rotation_id: z.string(), start_at: z.number().int(), team_id: z.string(), user_email: z.string()}); export const rotationTimeWindow = z.object({days: z.array(z.number().int().gte(0)).optional(), end_minute: z.number().int().gte(0), start_minute: z.number().int().gte(0)}); export const shiftRule = z.object({anchor_micros: z.number().int(), ends_at: z.number().int().nullable().optional(), members: z.array(z.string()), name: z.string().optional(), priority: z.number().int().optional(), restrictions: z.array(rotationTimeWindow).optional(), shift_micros: z.number().int(), starts_at: z.number().int().nullable().optional()}); export const rotation = z.object({id: z.string(), name: z.string(), shift_rules: z.array(shiftRule).optional(), source: z.string().nullable().optional()}); export const unavailability = z.object({created_at: z.number().int(), created_by: z.string(), end_at: z.number().int(), id: z.string(), org_id: z.string(), reason: z.string().nullable().optional(), start_at: z.number().int(), user_email: z.string()}); export const schedule = z.object({created_at: z.number().int(), id: z.string(), org_id: z.string(), overrides: z.array(scheduleOverride).optional(), rotations: z.array(rotation), team_id: z.string(), timezone: z.string(), unavailability: z.array(unavailability).optional(), updated_at: 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 }; /** * Replace a team's schedule * * PUT /api/{org_id}/oncall/teams/{team_id}/schedule */ export const updateApiApiOrgIdOncallTeamsTeamIdSchedule = async (args: UpdateApiApiOrgIdOncallTeamsTeamIdScheduleArgs, options: UpdateApiApiOrgIdOncallTeamsTeamIdScheduleOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/oncall/teams/${encodeURIComponent(String(args.team_id))}/schedule${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(schedule, await res.json()) };