import {z} from 'zod' export type PaymentType = 'bills' | 'cards' | 'external_expenses' | 'reimbursements'; export type UserUpdate = {role_id?: string | undefined, payment_types?: Array | undefined, out_of_office_mode?: boolean | undefined, out_of_office_start_date?: string | undefined, out_of_office_end_date?: string | undefined, delegate_approver?: (number | null) | undefined, expense_delegate?: Array | undefined}; export type PatchApiV1BusinessesClientIdUsersUserIdArgs = {client_id: number, user_id: number, body: UserUpdate}; export type PatchApiV1BusinessesClientIdUsersUserIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentType = z.enum(["bills", "cards", "external_expenses", "reimbursements"]); export const user = z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), name: z.string().optional(), email: z.string().optional(), role: z.string().optional(), role_id: z.string().optional(), is_deleted: z.boolean().optional(), created: z.string().optional(), updated: z.string().optional(), payment_types: z.array(paymentType).optional(), out_of_office_mode: z.boolean().optional(), out_of_office_start_date: z.string().nullable().optional(), out_of_office_end_date: z.string().nullable().optional(), delegate_approver: z.number().int().nullable().optional(), expense_delegate: z.array(z.number().int()).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 }; /** * Update user * * PATCH /v1/businesses/{client_id}/users/{user_id} */ export const patchApiV1BusinessesClientIdUsersUserId = async (args: PatchApiV1BusinessesClientIdUsersUserIdArgs, options?: PatchApiV1BusinessesClientIdUsersUserIdOptions) => { const {baseUrl = "https://public.letsweel.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/businesses/${encodeURIComponent(String(args.client_id))}/users/${encodeURIComponent(String(args.user_id))}${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(user, await res.json()) };