import {z} from 'zod' export type AddressPayload = {address1: string, address2?: string | undefined, addressSuburb?: string | undefined, addressCity?: string | undefined, addressRegion?: string | undefined, addressPostcode?: string | undefined, addressCountry?: string | undefined}; export type PatchApiUsersUserIdArgs = {userId: number, body: {firstName?: string | undefined, lastName?: string | undefined, address?: AddressPayload | undefined, payRate?: number | undefined, chargeOutRate?: number | undefined, contactItems?: (Array<{id?: number | undefined, contactType: 'phone' | 'mobile' | 'other' | 'fax' | 'website', contactValue: string}>) | undefined}}; export type PatchApiUsersUserIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const addressContact = z.object({id: z.number(), address1: z.string().nullable(), address2: z.string().nullable(), addressSuburb: z.string().nullable(), addressCity: z.string().nullable(), addressRegion: z.string().nullable(), addressPostcode: z.string().nullable(), addressCountry: z.string().nullable()}); export const contactItem = z.object({id: z.number(), contactType: z.enum(["email", "phone", "mobile", "other", "fax", "website"]), contactValue: z.string()}); export const links = z.object({href: z.string(), rel: z.string(), type: z.enum(["GET", "POST", "PUT", "PATCH"])}); export const user = z.object({id: z.number(), companyId: z.number(), firstName: z.string().nullable(), lastName: z.string().nullable(), email: z.string(), userType: z.enum(["contractor", "time_sheet_only", "field_worker", "apprentice", "tradesman", "advisor", "full_user"]), createdAt: z.string(), modifiedAt: z.string(), address: addressContact, contactItems: z.array(contactItem).nullable().optional(), payRate: z.number().nullable(), chargeOutRate: z.number().nullable(), status: z.enum(["active", "disabled", "invited"]), isOwner: z.boolean(), links: z.array(links)}); export const userByIdResponse = z.object({result: z.string(), data: user}); 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 /users/{userId} * * Also accepts auth: PersonalAccessToken */ export const patchApiUsersUserId = async (args: PatchApiUsersUserIdArgs, options?: PatchApiUsersUserIdOptions) => { const {baseUrl = "https://api.fergus.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(/\/+$/, '')}/users/${encodeURIComponent(String(args.userId))}${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(userByIdResponse, await res.json()) };