import {z} from 'zod' export type ContactGroupOnCallSchedule = {state: 'ACTIVE' | 'SUPPRESSED' | 'SCHEDULED', schedule?: (Array<{id?: number | undefined, weekdays: Array, to_time?: (string | null) | undefined, type: 'WEEKLY' | 'MONTHLY' | 'ONCE', once_start_date?: (string | null) | undefined, once_end_date?: (string | null) | undefined, from_time?: (string | null) | undefined, monthday_from?: number | undefined, monthday_to?: number | undefined}>) | undefined}; export type PatchApiApiV1ContactsPkOncallArgs = {pk: string, body: ContactGroupOnCallSchedule}; export type PatchApiApiV1ContactsPkOncallOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const contactGroupOnCallSchedule = z.object({state: z.enum(["ACTIVE", "SUPPRESSED", "SCHEDULED"]), schedule: z.array(z.object({id: z.number().int().optional(), weekdays: z.array(z.number().int()), to_time: z.string().nullable().optional(), type: z.enum(["WEEKLY", "MONTHLY", "ONCE"]), once_start_date: z.string().nullable().optional(), once_end_date: z.string().nullable().optional(), from_time: z.string().nullable().optional(), monthday_from: z.number().int().gte(0).lte(31).optional(), monthday_to: z.number().int().gte(0).lte(31).optional()})).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 }; /** * Set the on-call schedule for a contact. * * PATCH /api/v1/contacts/{pk}/oncall/ */ export const patchApiApiV1ContactsPkOncall = async (args: PatchApiApiV1ContactsPkOncallArgs, options: PatchApiApiV1ContactsPkOncallOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/contacts/${encodeURIComponent(String(args.pk))}/oncall/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(contactGroupOnCallSchedule, await res.json()) };