import {z} from 'zod' export type CostComponentExternalSchema = {kind: 'total' | 'labor' | 'material', label?: (string | null) | undefined, unit_type?: (string | null) | undefined, unit_cost_in_cents?: (number | null) | undefined, quantity: string}; export type CreateLineItemRequest = {scope_id: string, title: string, description?: (string | null) | undefined, price_reference_key?: (string | null) | undefined, notes?: (string | null) | undefined, custom_fields?: (Record | null) | undefined, status?: ('not_complete' | 'in_progress' | 'complete' | 'rejected' | null) | undefined, cost_components?: (Array | null) | undefined, room_id?: (string | null) | undefined}; export type CreateApiV1LineItemsArgs = {body: CreateLineItemRequest}; export type CreateApiV1LineItemsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const costComponentExternalSchema = z.object({kind: z.enum(["total", "labor", "material"]), label: z.string().nullable().optional(), unit_type: z.string().nullable().optional(), unit_cost_in_cents: z.number().int().gte(0).nullable().optional(), quantity: z.string()}); export const imageEvidenceExternalSchema = z.object({type: z.literal("image").optional(), image_id: z.string(), reference_key: z.string().nullable().optional(), visit_id: z.string().nullable().optional(), is_selected: z.boolean().optional(), is_starred: z.boolean().optional()}); export const audioEvidenceExternalSchema = z.object({type: z.literal("audio").optional(), start_ms: z.number().int(), end_ms: z.number().int(), text: z.string(), visit_id: z.string().nullable().optional()}); export const textEvidenceExternalSchema = z.object({type: z.literal("text").optional(), text: z.string(), visit_id: z.string().nullable().optional()}); export const lineItemRoomExternalSchema = z.object({id: z.string().nullable(), name: z.string(), room_type_id: z.string().nullable().optional()}); export const vendorOrganizationExternalSchema = z.object({id: z.string(), name: z.string(), email: z.string().nullable(), reference_key: z.string(), is_enabled: z.boolean(), market_ids: z.array(z.string())}); export const lineItemPolicyExternalSchema = z.object({title: z.string()}); export const lineItemPolicyJustificationExternalSchema = z.object({policy: lineItemPolicyExternalSchema, alignment: z.enum(["supports", "violates", "relates"]), reason: z.string()}); export const lineItemRowExternalSchema = z.object({id: z.string(), line_item_row_id: z.string(), title: z.string(), description: z.string(), status: z.enum(["suggested", "not_complete", "in_progress", "complete", "rejected"]), quantity: z.number().int(), unit_type: z.string(), unit_cost_cents: z.number().int().nullable(), cost_components: z.array(costComponentExternalSchema), price_override_reason: z.string().nullable().optional(), price_reference_key: z.string().nullable(), notes: z.string(), custom_fields: z.object({}), evidence: z.array(z.union([imageEvidenceExternalSchema, audioEvidenceExternalSchema, textEvidenceExternalSchema])), area_name: z.string().nullable(), room: lineItemRoomExternalSchema.nullable(), rejection_reason_option_id: z.string().nullable().optional(), rejection_reason_text: z.string().nullable().optional(), vendor: vendorOrganizationExternalSchema.nullable().optional(), policy_justifications: z.array(lineItemPolicyJustificationExternalSchema)}); export const lineItemRowApiResponse = z.object({line_item_row: lineItemRowExternalSchema}); 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 }; /** * Create a line item * * POST /v1/line_items */ export const createApiV1LineItems = async (args: CreateApiV1LineItemsArgs, options?: CreateApiV1LineItemsOptions) => { const {baseUrl = "https://api.navigateai.co", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/line_items${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(lineItemRowApiResponse, await res.json()) };