import {z} from 'zod' export type PermissionEffect = 'ALLOW' | 'DENY'; export type PermissionAction = 'ALL' | 'CONFIG_CREATE' | 'CONFIG_RETRIEVE' | 'CONFIG_UPDATE' | 'CONFIG_DELETE' | 'CONFIG_EXPORT' | 'ANALYTICS_QUERY' | 'MEASUREMENTS_UPLOAD' | 'MEASUREMENTS_FILEUPLOAD' | 'MEASUREMENTS_RETRIEVE' | 'MEASUREMENTS_EXPORT' | 'FORECAST_RETRIEVE' | 'HEALTHSCORES_RETRIEVE' | 'ANOMALIES_RETRIEVE' | 'EXPORTS_DOWNLOAD' | 'MARKETPLACE_USAGE_CREATE' | 'MARKETPLACE_USAGE_RETRIEVE' | 'AUDIT_RETRIEVE'; export type PermissionStatement = {effect: PermissionEffect, action: Array, resource: Array}; export type OrgUserRequest = {version?: number | undefined, permissionPolicy?: Array | undefined, dtEndAccess?: string | undefined}; export type UpdateApiOrganizationsOrgIdUsersIdArgs = {orgId: string, id: string, body: OrgUserRequest}; export type UpdateApiOrganizationsOrgIdUsersIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const permissionEffect = z.enum(["ALLOW", "DENY"]); export const permissionAction = z.enum(["ALL", "CONFIG_CREATE", "CONFIG_RETRIEVE", "CONFIG_UPDATE", "CONFIG_DELETE", "CONFIG_EXPORT", "ANALYTICS_QUERY", "MEASUREMENTS_UPLOAD", "MEASUREMENTS_FILEUPLOAD", "MEASUREMENTS_RETRIEVE", "MEASUREMENTS_EXPORT", "FORECAST_RETRIEVE", "HEALTHSCORES_RETRIEVE", "ANOMALIES_RETRIEVE", "EXPORTS_DOWNLOAD", "MARKETPLACE_USAGE_CREATE", "MARKETPLACE_USAGE_RETRIEVE", "AUDIT_RETRIEVE"]); export const permissionStatement = z.object({effect: permissionEffect, action: z.array(permissionAction), resource: z.array(z.string())}); export const orgUserResponse = z.object({id: z.string(), version: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional(), contactNumber: z.string().optional(), email: z.string().optional(), organizations: z.array(z.string()).optional(), firstAcceptedTermsAndConditions: z.string().optional(), lastAcceptedTermsAndConditions: z.string().optional(), supportUser: z.boolean().optional(), dtCreated: z.string().optional(), dtLastModified: z.string().optional(), createdBy: z.string().optional(), lastModifiedBy: z.string().optional(), permissionPolicy: z.array(permissionStatement).optional(), dtEndAccess: z.string().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 OrgUser * * PUT /organizations/{orgId}/users/{id} */ export const updateApiOrganizationsOrgIdUsersId = async (args: UpdateApiOrganizationsOrgIdUsersIdArgs, options?: UpdateApiOrganizationsOrgIdUsersIdOptions) => { const {baseUrl = "https://api.m3ter.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(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.orgId))}/users/${encodeURIComponent(String(args.id))}${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(orgUserResponse, await res.json()) };