import {z} from 'zod' export type CompanyAnsweringRuleCallersInfoRequest = {callerId?: string | undefined, name?: string | undefined}; export type CompanyAnsweringRuleCalledNumberInfo = {id?: string | undefined, phoneNumber?: string | undefined}; export type CompanyAnsweringRuleTimeIntervalRequest = {from?: string | undefined, to?: string | undefined}; export type CompanyAnsweringRuleWeeklyScheduleInfoRequest = {monday?: Array | undefined, tuesday?: Array | undefined, wednesday?: Array | undefined, thursday?: Array | undefined, friday?: Array | undefined, saturday?: Array | undefined, sunday?: Array | undefined}; export type RangesInfo = {from?: string | undefined, to?: string | undefined}; export type CompanyAnsweringRuleScheduleInfoRequest = {weeklyRanges?: CompanyAnsweringRuleWeeklyScheduleInfoRequest | undefined, ranges?: Array | undefined, ref?: ('BusinessHours' | 'AfterHours') | undefined}; export type GreetingTypeEnum = 'Introductory' | 'Announcement' | 'AutomaticRecording' | 'BlockedCallersAll' | 'BlockedCallersSpecific' | 'BlockedNoCallerId' | 'BlockedPayPhones' | 'ConnectingMessage' | 'ConnectingAudio' | 'StartRecording' | 'StopRecording' | 'Voicemail' | 'Unavailable' | 'InterruptPrompt' | 'HoldMusic' | 'Company'; export type PresetInfo = {uri?: string | undefined, id?: string | undefined, name?: string | undefined}; export type CustomGreetingInfoRequest = {id?: string | undefined}; export type GreetingInfo = {type?: GreetingTypeEnum | undefined, preset?: PresetInfo | undefined, custom?: CustomGreetingInfoRequest | undefined}; export type CompanyAnsweringRuleUpdate = {enabled?: boolean | undefined, name?: string | undefined, callers?: Array | undefined, calledNumbers?: Array | undefined, schedule?: CompanyAnsweringRuleScheduleInfoRequest | undefined, callHandlingAction?: ('Operator' | 'Disconnect' | 'Bypass') | undefined, type?: ('BusinessHours' | 'AfterHours' | 'Custom') | undefined, extension?: CompanyAnsweringRuleCallersInfoRequest | undefined, greetings?: Array | undefined}; export type UpdateApiRestapiV10AccountAccountIdAnsweringRuleRuleIdArgs = {accountId: string, ruleId: string, body: CompanyAnsweringRuleUpdate}; export type UpdateApiRestapiV10AccountAccountIdAnsweringRuleRuleIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const companyAnsweringRuleCallersInfoRequest = z.object({callerId: z.string().optional(), name: z.string().optional()}); export const companyAnsweringRuleCalledNumberInfoRequest = z.object({id: z.string().optional(), phoneNumber: z.string().optional()}); export const companyAnsweringRuleTimeIntervalRequest = z.object({from: z.string().optional(), to: z.string().optional()}); export const companyAnsweringRuleWeeklyScheduleInfoRequest = z.object({monday: z.array(companyAnsweringRuleTimeIntervalRequest).optional(), tuesday: z.array(companyAnsweringRuleTimeIntervalRequest).optional(), wednesday: z.array(companyAnsweringRuleTimeIntervalRequest).optional(), thursday: z.array(companyAnsweringRuleTimeIntervalRequest).optional(), friday: z.array(companyAnsweringRuleTimeIntervalRequest).optional(), saturday: z.array(companyAnsweringRuleTimeIntervalRequest).optional(), sunday: z.array(companyAnsweringRuleTimeIntervalRequest).optional()}); export const rangesInfo = z.object({from: z.string().optional(), to: z.string().optional()}); export const companyAnsweringRuleScheduleInfo = z.object({weeklyRanges: companyAnsweringRuleWeeklyScheduleInfoRequest.optional(), ranges: z.array(rangesInfo).optional(), ref: z.enum(["BusinessHours", "AfterHours"]).optional()}); export const companyAnsweringRuleExtensionInfoRequest = z.object({id: z.string().optional()}); export const greetingTypeEnum = z.enum(["Introductory", "Announcement", "AutomaticRecording", "BlockedCallersAll", "BlockedCallersSpecific", "BlockedNoCallerId", "BlockedPayPhones", "ConnectingMessage", "ConnectingAudio", "StartRecording", "StopRecording", "Voicemail", "Unavailable", "InterruptPrompt", "HoldMusic", "Company"]); export const presetInfo = z.object({uri: z.string().optional(), id: z.string().optional(), name: z.string().optional()}); export const customGreetingInfoRequest = z.object({id: z.string().optional()}); export const greetingInfo = z.object({type: greetingTypeEnum.optional(), preset: presetInfo.optional(), custom: customGreetingInfoRequest.optional()}); export const companyAnsweringRuleInfo = z.object({id: z.string().optional(), uri: z.string().optional(), enabled: z.boolean().optional(), type: z.enum(["BusinessHours", "AfterHours", "Custom"]).optional(), name: z.string().optional(), callers: z.array(companyAnsweringRuleCallersInfoRequest).optional(), calledNumbers: z.array(companyAnsweringRuleCalledNumberInfoRequest).optional(), schedule: companyAnsweringRuleScheduleInfo.optional(), callHandlingAction: z.enum(["Operator", "Disconnect", "Bypass"]).optional(), extension: companyAnsweringRuleExtensionInfoRequest.optional(), greetings: z.array(greetingInfo).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 }; /** * Update Company Call Handling Rule * * PUT /restapi/v1.0/account/{accountId}/answering-rule/{ruleId} */ export const updateApiRestapiV10AccountAccountIdAnsweringRuleRuleId = async (args: UpdateApiRestapiV10AccountAccountIdAnsweringRuleRuleIdArgs, options?: UpdateApiRestapiV10AccountAccountIdAnsweringRuleRuleIdOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/restapi/v1.0/account/${encodeURIComponent(String(args.accountId))}/answering-rule/${encodeURIComponent(String(args.ruleId))}${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(companyAnsweringRuleInfo, await res.json()) };