import {z} from 'zod' export type GetApiApiBusinessBusinessIdBranchBranchIdLeadsArgs = {businessId: string, branchId: string, name?: string | undefined, status?: (Array<'NEW_LEAD' | 'CONTACT_ATTEMPTED' | 'CONTACT_MADE' | 'INTERESTED' | 'APPOINTMENT_SCHEDULED' | 'CONVERTED' | 'LOST' | 'DELETED'>) | undefined, contactableByEmail?: boolean | undefined, contactableByMobile?: boolean | undefined, fetchMessageHistory?: boolean | undefined, size?: number | undefined, page?: number | undefined}; export type GetApiApiBusinessBusinessIdBranchBranchIdLeadsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const messageHistory = z.object({type: z.string().optional(), sendDate: z.string().optional(), channel: z.string().optional()}); export const leadResponse = z.object({leadId: z.number().int().optional(), businessId: z.string().optional(), branchId: z.string().optional(), mobile: z.string().optional(), email: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), contactableByEmail: z.boolean().optional(), contactableByMobile: z.boolean().optional(), status: z.enum(["NEW_LEAD", "CONTACT_ATTEMPTED", "CONTACT_MADE", "INTERESTED", "APPOINTMENT_SCHEDULED", "CONVERTED", "LOST", "DELETED"]).optional(), source: z.string().optional(), lastActivityDate: z.string().optional(), smsMarketingOptOut: z.boolean().optional(), emailMarketingOptOut: z.boolean().optional(), clientId: z.string().optional(), updatedBy: z.string().optional(), updateAction: z.enum(["MANUAL_UPDATE", "APPOINTMENT_CONVERTED", "APPOINTMENT_CANCELLED", "APPOINTMENT_CANCELLED_OR_DELETED", "APPOINTMENT_DELETED", "APPOINTMENT_BOOKED", "CLIENT_MATCHED_BY_MOBILE", "CLIENT_MATCHED_BY_EMAIL", "EXISTING_CLIENT", "MARKETING_CONSENT", "ONE_CLICK_UNSUBSCRIBE", "SUBMITTED_FORM"]).optional(), adCampaignName: z.string().optional(), messageHistory: z.array(messageHistory).optional(), note: z.string().optional()}); export const pageMetadata = z.object({size: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), number: z.number().int().optional()}); export const pagedModelLeadResponse = z.object({_embedded: z.object({leads: z.array(leadResponse).optional()}).optional(), page: pageMetadata.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 }; /** * List leads * * GET /api/business/{businessId}/branch/{branchId}/leads */ export const getApiApiBusinessBusinessIdBranchBranchIdLeads = async (args: GetApiApiBusinessBusinessIdBranchBranchIdLeadsArgs, options?: GetApiApiBusinessBusinessIdBranchBranchIdLeadsOptions) => { 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('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.contactableByEmail != null) query.append("contactableByEmail", String(args.contactableByEmail)) if (args.contactableByMobile != null) query.append("contactableByMobile", String(args.contactableByMobile)) if (args.fetchMessageHistory != null) query.append("fetchMessageHistory", String(args.fetchMessageHistory)) if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/business/${encodeURIComponent(String(args.businessId))}/branch/${encodeURIComponent(String(args.branchId))}/leads${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(pagedModelLeadResponse, await res.json()) };