import {z} from 'zod' export type BridgeRowStatus = 'New' | 'Actioned' | 'Saved' | 'Attending' | 'Sponsoring' | 'NotInterested'; export type SetBridgeRowStatusRequest = {newStatus: BridgeRowStatus}; export type PatchApiApiExternalBridgeBridgeIdRowRowIdStatusArgs = {bridgeId: string, rowId: string, body: SetBridgeRowStatusRequest}; export type PatchApiApiExternalBridgeBridgeIdRowRowIdStatusOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const bridgeRowEntityRef = z.object({type: z.enum(["RFP", "Meeting", "Purchase", "Buyer", "TopBuyer", "Contact", "Signal", "Conference", "JobChange", "SequenceBuyer", "SequenceContact", "SequenceJobChange", "VendorPresence", "ProductCompetitorPresence", "CompanionPresence"]), id: z.string()}); export const bridgeRowColumnValueResponse = z.object({value: z.unknown(), status: z.enum(["NotProcessed", "Queued", "Processing", "Processed", "Failed", "Skipped"]).nullable().optional(), processedAt: z.string().nullable().optional()}); export const bridgeRowResponse = z.object({rowId: z.string(), name: z.string().nullable().optional(), bridgeId: z.string(), createdAt: z.string(), updatedAt: z.string(), status: z.enum(["NotProcessed", "Queued", "Processing", "Processed", "Failed", "Skipped"]), buyerId: z.string().nullable().optional(), organizerId: z.string().nullable().optional(), entity: bridgeRowEntityRef.nullable().optional(), columns: z.record(z.string(), bridgeRowColumnValueResponse)}); 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 }; /** * Set Bridge Row Status * * PATCH /api/external/bridge/{bridgeId}/row/{rowId}/status */ export const patchApiApiExternalBridgeBridgeIdRowRowIdStatus = async (args: PatchApiApiExternalBridgeBridgeIdRowRowIdStatusArgs, options?: PatchApiApiExternalBridgeBridgeIdRowRowIdStatusOptions) => { const {baseUrl = "https://dashboard.starbridge.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/external/bridge/${encodeURIComponent(String(args.bridgeId))}/row/${encodeURIComponent(String(args.rowId))}/status${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(bridgeRowResponse, await res.json()) };