import {z} from 'zod' export type AccountLegalRole = 'CUSTODIAN' | 'ESCROW_AGENT' | 'TRUSTEE'; export type AccountRelationshipType = 'ACCOUNT_HOLDER' | 'AUTHORIZED_SIGNER' | 'AUTHORIZED_USER' | 'BENEFICIARY' | 'GUARANTOR' | 'JOINT_ACCOUNT_HOLDER' | 'PRIMARY_ACCOUNT_HOLDER'; export type AccountRelationship = {business_id?: string | undefined, created_at?: string | undefined, customer_id?: string | undefined, deleted_at?: string | undefined, id?: string | undefined, legal_roles?: Array | undefined, ownership_percentage?: number | undefined, person_id?: string | undefined, relationship_type: AccountRelationshipType, updated_at?: string | undefined}; export type UpdateApiAccountsAccountIdRelationshipsRelationshipIdArgs = {relationship_id: string, account_id: string, body: AccountRelationship}; export type UpdateApiAccountsAccountIdRelationshipsRelationshipIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const accountLegalRole = z.enum(["CUSTODIAN", "ESCROW_AGENT", "TRUSTEE"]); export const accountRelationshipType = z.enum(["ACCOUNT_HOLDER", "AUTHORIZED_SIGNER", "AUTHORIZED_USER", "BENEFICIARY", "GUARANTOR", "JOINT_ACCOUNT_HOLDER", "PRIMARY_ACCOUNT_HOLDER"]); export const relationshipResponse = z.object({account_id: z.string().optional(), business_id: z.string().optional(), created_at: z.string().optional(), customer_id: z.string().optional(), deleted_at: z.string().optional(), id: z.string().optional(), legal_roles: z.array(accountLegalRole).optional(), ownership_percentage: z.number().gte(0).lte(100).optional(), person_id: z.string().optional(), relationship_type: accountRelationshipType, updated_at: 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 account relationship * * PUT /accounts/{account_id}/relationships/{relationship_id} */ export const updateApiAccountsAccountIdRelationshipsRelationshipId = async (args: UpdateApiAccountsAccountIdRelationshipsRelationshipIdArgs, options?: UpdateApiAccountsAccountIdRelationshipsRelationshipIdOptions) => { const {baseUrl = "https://api-sandbox.synctera.com/v0", 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(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/relationships/${encodeURIComponent(String(args.relationship_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(relationshipResponse, await res.json()) };