import {z} from 'zod' export type CustomFieldRequest = {id?: number | undefined, value?: string | undefined}; export type Commission = {id?: number | undefined, splitPayType?: ('TEAM' | 'AGENT' | 'VENDOR_PARTNER' | 'LEAD' | 'OTHER' | 'BROKERAGE' | 'UPLINE') | undefined, splitPayTo?: number | undefined, splitPayToName?: string | undefined, payType?: ('TEAM' | 'AGENT' | 'VENDOR_PARTNER' | 'LEAD' | 'OTHER' | 'BROKERAGE' | 'UPLINE') | undefined, payTo?: number | undefined, payToName?: string | undefined, value?: number | undefined, calculateType?: ('PERCENT' | 'AMOUNT') | undefined, desc?: string | undefined}; export type Commissions = {preSplit?: Array | undefined, split?: Array | undefined, postSplit?: Array | undefined}; export type LeadTransaction = {created?: number | undefined, updated?: number | undefined, transactionId?: number | undefined, transactionName: string, leadName?: string | undefined, assignedAgent?: number | undefined, transactionType?: ('Purchase' | 'Listing' | 'Lease' | 'Other') | undefined, homePrice?: number | undefined, transactionStatus?: string | undefined, expectedCloseDate?: number | undefined, closeDate?: number | undefined, commissionRate?: number | undefined, gci?: number | undefined, teamRevenue?: number | undefined, agentRevenue?: number | undefined, appointmentDate?: number | undefined, agreementSignedDate?: number | undefined, offerDate?: number | undefined, contractDate?: number | undefined, appraisalDate?: number | undefined, homeInspectionDate?: number | undefined, escrowDate?: number | undefined, expiration?: number | undefined, customFields?: Array | undefined, commissions?: Commissions | undefined}; export type UpdateApiV10LeadsLeadIdTransactionTransactionIdArgs = {leadId: number, transactionId: number, Authorization: string, 'Content-Type': string, body: LeadTransaction}; export type UpdateApiV10LeadsLeadIdTransactionTransactionIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const customFieldRequest = z.object({id: z.number().int().optional(), value: z.string().optional()}); export const commission = z.object({id: z.number().int().optional(), splitPayType: z.enum(["TEAM", "AGENT", "VENDOR_PARTNER", "LEAD", "OTHER", "BROKERAGE", "UPLINE"]).optional(), splitPayTo: z.number().int().optional(), splitPayToName: z.string().optional(), payType: z.enum(["TEAM", "AGENT", "VENDOR_PARTNER", "LEAD", "OTHER", "BROKERAGE", "UPLINE"]).optional(), payTo: z.number().int().optional(), payToName: z.string().optional(), value: z.number().int().optional(), calculateType: z.enum(["PERCENT", "AMOUNT"]).optional(), desc: z.string().optional()}); export const commissions = z.object({preSplit: z.array(commission).optional(), split: z.array(commission).optional(), postSplit: z.array(commission).optional()}); export const leadTransaction = z.object({created: z.number().int().optional(), updated: z.number().int().optional(), transactionId: z.number().int().optional(), transactionName: z.string(), leadName: z.string().optional(), assignedAgent: z.number().int().optional(), transactionType: z.enum(["Purchase", "Listing", "Lease", "Other"]).optional(), homePrice: z.number().optional(), transactionStatus: z.string().optional(), expectedCloseDate: z.number().int().optional(), closeDate: z.number().int().optional(), commissionRate: z.number().optional(), gci: z.number().optional(), teamRevenue: z.number().optional(), agentRevenue: z.number().optional(), appointmentDate: z.number().int().optional(), agreementSignedDate: z.number().int().optional(), offerDate: z.number().int().optional(), contractDate: z.number().int().optional(), appraisalDate: z.number().int().optional(), homeInspectionDate: z.number().int().optional(), escrowDate: z.number().int().optional(), expiration: z.number().int().optional(), customFields: z.array(customFieldRequest).optional(), commissions: commissions.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 Transaction * * PUT /v1.0/leads/{leadId}/transaction/{transactionId} */ export const updateApiV10LeadsLeadIdTransactionTransactionId = async (args: UpdateApiV10LeadsLeadIdTransactionTransactionIdArgs, options?: UpdateApiV10LeadsLeadIdTransactionTransactionIdOptions) => { const {baseUrl = "https://api.lofty.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1.0/leads/${encodeURIComponent(String(args.leadId))}/transaction/${encodeURIComponent(String(args.transactionId))}${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(leadTransaction, await res.json()) };