import {z} from 'zod' export type AddressData = {streetAddress1?: string | undefined, streetAddress2?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined}; export type ClientUpdateRequest = {linkedClientMobile?: string | undefined, landLine?: string | undefined, email?: string | undefined, address?: AddressData | undefined, birthDate?: string | undefined, clientSince?: string | undefined, gender?: ('MALE' | 'FEMALE' | 'NON_BINARY' | 'NOT_SPECIFIED' | 'PREFER_NOT_TO_SAY' | 'UNKNOWN') | undefined, notes?: string | undefined, smsMarketingConsent?: boolean | undefined, emailMarketingConsent?: boolean | undefined, smsReminderConsent?: boolean | undefined, emailReminderConsent?: boolean | undefined, preferredStaffId?: string | undefined, externalId?: string | undefined, creatingBranchId?: string | undefined, archived?: boolean | undefined, banned?: boolean | undefined, clientCategoryIds?: Array | undefined, clientId?: string | undefined, version?: number | undefined, firstName: string, lastName: string, mobile?: string | undefined, firstVisit?: string | undefined}; export type UpdateApiApiBusinessBusinessIdClientClientIdArgs = {businessId: string, clientId: string, body: ClientUpdateRequest}; export type UpdateApiApiBusinessBusinessIdClientClientIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const addressData = z.object({streetAddress1: z.string().optional(), streetAddress2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postalCode: z.string().optional(), country: z.string().optional()}); export const creditAccount = z.object({outstandingBalance: z.number().optional(), creditDays: z.number().int().optional(), creditLimit: z.number().optional()}); export const loyaltyCard = z.object({serial: z.string().optional(), points: z.number().int().optional()}); export const clientResponse = z.object({linkedClientMobile: z.string().optional(), landLine: z.string().optional(), email: z.string().optional(), address: addressData.optional(), birthDate: z.string().optional(), clientSince: z.string().optional(), gender: z.enum(["MALE", "FEMALE", "NON_BINARY", "NOT_SPECIFIED", "PREFER_NOT_TO_SAY", "UNKNOWN"]).optional(), notes: z.string().optional(), smsMarketingConsent: z.boolean().optional(), emailMarketingConsent: z.boolean().optional(), smsReminderConsent: z.boolean().optional(), emailReminderConsent: z.boolean().optional(), preferredStaffId: z.string().optional(), externalId: z.string().optional(), creatingBranchId: z.string().optional(), archived: z.boolean().optional(), banned: z.boolean().optional(), clientCategoryIds: z.array(z.string()).optional(), clientId: z.string().optional(), version: z.number().int().optional(), firstName: z.string(), lastName: z.string(), mobile: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), photoUrl: z.string().optional(), creditAccount: creditAccount.optional(), loyaltyCard: loyaltyCard.optional(), mergedToClientId: z.string().optional(), linkedClientId: z.string().optional(), deleted: z.boolean().optional(), firstVisit: z.string().optional(), lastVisit: z.string().optional(), lastVisitedBranchId: z.string().optional(), hasActiveMembership: z.boolean().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 a client * * PUT /api/business/{businessId}/client/{clientId} */ export const updateApiApiBusinessBusinessIdClientClientId = async (args: UpdateApiApiBusinessBusinessIdClientClientIdArgs, options?: UpdateApiApiBusinessBusinessIdClientClientIdOptions) => { const {baseUrl = "http://api-gateway-eu.phorest.com/third-party-api-server", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/business/${encodeURIComponent(String(args.businessId))}/client/${encodeURIComponent(String(args.clientId))}${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(clientResponse, await res.json()) };