import {z} from 'zod' export type LeadUpdateRequest = {mobile?: string | undefined, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, contactableByEmail?: boolean | undefined, contactableByMobile?: boolean | undefined, status?: ('NEW_LEAD' | 'CONTACT_ATTEMPTED' | 'CONTACT_MADE' | 'INTERESTED' | 'APPOINTMENT_SCHEDULED' | 'CONVERTED' | 'LOST' | 'DELETED') | undefined, smsMarketingOptOut?: boolean | undefined, emailMarketingOptOut?: boolean | undefined, clientId?: string | undefined}; export type UpdateApiApiBusinessBusinessIdBranchBranchIdLeadsLeadIdArgs = {businessId: string, branchId: string, leadId: number, body: LeadUpdateRequest}; export type UpdateApiApiBusinessBusinessIdBranchBranchIdLeadsLeadIdOptions = {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 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 an existing lead * * PUT /api/business/{businessId}/branch/{branchId}/leads/{leadId} */ export const updateApiApiBusinessBusinessIdBranchBranchIdLeadsLeadId = async (args: UpdateApiApiBusinessBusinessIdBranchBranchIdLeadsLeadIdArgs, options?: UpdateApiApiBusinessBusinessIdBranchBranchIdLeadsLeadIdOptions) => { 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))}/branch/${encodeURIComponent(String(args.branchId))}/leads/${encodeURIComponent(String(args.leadId))}${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(leadResponse, await res.json()) };