import {z} from 'zod' export type DepartmentStatus = 1 | 2 | 3; export type Department = {id?: number | undefined, accountId?: number | undefined, code?: (string | null) | undefined, name?: (string | null) | undefined, status?: DepartmentStatus | undefined, isDefault?: boolean | undefined, timezoneId?: (number | null) | undefined, createdAt?: string | undefined, updatedAt?: (string | null) | undefined, deletedAt?: (string | null) | undefined, deleteTargetDepartmentId?: (number | null) | undefined, channelSnapshot?: (string | null) | undefined}; export type PermissionProfileRight = {id?: number | undefined, profileId?: number | undefined, rightModule?: number | undefined, viewRecord?: boolean | undefined, editRecord?: boolean | undefined, deleteRecord?: boolean | undefined, newRecord?: boolean | undefined, exportRecord?: boolean | undefined, profile?: PermissionProfile | undefined}; export type PermissionProfileVoipRight = {id?: number | undefined, profileId?: number | undefined, voipUserId?: number | undefined, view?: boolean | undefined, listen?: boolean | undefined, delete?: boolean | undefined, saveAs?: boolean | undefined, profile?: PermissionProfile | undefined}; export type PermissionProfileExternalNumberRight = {id?: number | undefined, profileId?: number | undefined, externalNumberId?: number | undefined, view?: boolean | undefined, listen?: boolean | undefined, delete?: boolean | undefined, saveAs?: boolean | undefined, profile?: PermissionProfile | undefined}; export type PermissionProfile = {id?: number | undefined, accountId?: (number | null) | undefined, departmentId?: (number | null) | undefined, name?: (string | null) | undefined, code?: (string | null) | undefined, isSystem?: boolean | undefined, isLicensed?: boolean | undefined, createdBy?: (number | null) | undefined, createdAt?: string | undefined, updatedAt?: (string | null) | undefined, department?: Department | undefined, rights?: (Array | null) | undefined, voipRights?: (Array | null) | undefined, externalNumberRights?: (Array | null) | undefined}; export type CreateApiV1PermissionProfilesArgs = {body: PermissionProfile}; export type CreateApiV1PermissionProfilesOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const departmentStatus = z.union([z.literal(1), z.literal(2), z.literal(3)]); export const department = z.object({id: z.number().int().optional(), accountId: z.number().int().optional(), code: z.string().nullable().optional(), name: z.string().nullable().optional(), status: departmentStatus.optional(), isDefault: z.boolean().optional(), timezoneId: z.number().int().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().nullable().optional(), deletedAt: z.string().nullable().optional(), deleteTargetDepartmentId: z.number().int().nullable().optional(), channelSnapshot: z.string().nullable().optional()}); export const permissionProfileRight = z.object({id: z.number().int().optional(), profileId: z.number().int().optional(), rightModule: z.number().int().optional(), viewRecord: z.boolean().optional(), editRecord: z.boolean().optional(), deleteRecord: z.boolean().optional(), newRecord: z.boolean().optional(), exportRecord: z.boolean().optional(), profile: z.lazy(() => permissionProfile.optional())}); export const permissionProfileVoipRight = z.object({id: z.number().int().optional(), profileId: z.number().int().optional(), voipUserId: z.number().int().optional(), view: z.boolean().optional(), listen: z.boolean().optional(), delete: z.boolean().optional(), saveAs: z.boolean().optional(), profile: z.lazy(() => permissionProfile.optional())}); export const permissionProfileExternalNumberRight = z.object({id: z.number().int().optional(), profileId: z.number().int().optional(), externalNumberId: z.number().int().optional(), view: z.boolean().optional(), listen: z.boolean().optional(), delete: z.boolean().optional(), saveAs: z.boolean().optional(), profile: z.lazy(() => permissionProfile.optional())}); export const permissionProfile: z.ZodType = z.object({id: z.number().int().optional(), accountId: z.number().int().nullable().optional(), departmentId: z.number().int().nullable().optional(), name: z.string().nullable().optional(), code: z.string().nullable().optional(), isSystem: z.boolean().optional(), isLicensed: z.boolean().optional(), createdBy: z.number().int().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().nullable().optional(), department: department.optional(), rights: z.array(permissionProfileRight).nullable().optional(), voipRights: z.array(permissionProfileVoipRight).nullable().optional(), externalNumberRights: z.array(permissionProfileExternalNumberRight).nullable().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 }; /** * POST /v1/permission-profiles * * Also accepts auth: API Key */ export const createApiV1PermissionProfiles = async (args: CreateApiV1PermissionProfilesArgs, options: CreateApiV1PermissionProfilesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/permission-profiles${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(permissionProfile, await res.json()) };