import {z} from 'zod' export type ExtensionNumber = string; export type PhoneNumberE164 = string; export type PostalAddress = {street: string, street2?: string | undefined, city: string, state?: string | undefined, zip: string, country: string}; export type PhoneNumberTollType = 'Toll' | 'TollFree'; export type PhoneNumberDefinitionTollType = {tollType: PhoneNumberTollType}; export type PhoneNumberDefinitionPreferredAreaCode = {preferredAreaCode: string}; export type PhoneNumberDefinitionDirectNumberWithReservationId = {phoneNumber: string, reservationId?: string | undefined}; export type PhoneNumberDefinitionNumberId = {phoneNumberId: string}; export type DeviceDefinition = {type: 'OtherPhone' | 'WebRTC', emergency: {address: PostalAddress} | {location: {id: string}}, phoneInfo: PhoneNumberDefinitionTollType | PhoneNumberDefinitionPreferredAreaCode | PhoneNumberDefinitionDirectNumberWithReservationId | PhoneNumberDefinitionNumberId}; export type BatchProvisionUsersRequestItem = {extensionNumber?: ExtensionNumber | undefined, status: 'Enabled', contact: {firstName: string, lastName: string, email: string, mobileNumber?: PhoneNumberE164 | undefined, emailAsLoginName?: boolean | undefined}, costCenter?: {id: string} | undefined, roles?: Array<{id: string}> | undefined, devices?: Array<{deviceInfo: DeviceDefinition}> | undefined, sendWelcomeEmail?: boolean | undefined}; export type BatchProvisionUsersRequest = {records: Array}; export type CreateApiRestapiV2AccountsAccountIdBatchProvisioningUsersArgs = {accountId: string, body: BatchProvisionUsersRequest}; export type CreateApiRestapiV2AccountsAccountIdBatchProvisioningUsersOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneNumberE164 = z.string().max(18); export const batchProvisionUsersSuccessItem = z.object({successful: z.boolean(), extension: z.object({id: z.string(), devices: z.array(z.object({id: z.string(), phoneNumber: phoneNumberE164}))})}); export const apiErrorWithParameter = z.object({errorCode: z.string(), message: z.string(), parameterName: z.string().optional(), parameterValue: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const batchProvisionErrorItem = z.object({successful: z.boolean(), errors: z.array(apiErrorWithParameter)}); export const batchProvisionUsersResponse = z.object({results: z.array(z.union([batchProvisionUsersSuccessItem, batchProvisionErrorItem]))}); 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 Multiple User Extensions * * POST /restapi/v2/accounts/{accountId}/batch-provisioning/users */ export const createApiRestapiV2AccountsAccountIdBatchProvisioningUsers = async (args: CreateApiRestapiV2AccountsAccountIdBatchProvisioningUsersArgs, options?: CreateApiRestapiV2AccountsAccountIdBatchProvisioningUsersOptions) => { 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/v2/accounts/${encodeURIComponent(String(args.accountId))}/batch-provisioning/users${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(batchProvisionUsersResponse, await res.json()) };