import {z} from 'zod' export type GetApiApiOrderFormArgs = {email: string, comercial_id?: (number | null) | undefined}; export type GetApiApiOrderFormOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const contactDetailsResponse = z.object({id: z.string(), firstname: z.string().nullable().optional(), lastname: z.string().nullable().optional(), email: z.string().nullable().optional(), phone: z.string().nullable().optional(), address: z.string().nullable().optional(), city: z.string().nullable().optional(), zip: z.string().nullable().optional(), province: z.string().nullable().optional(), numero_documento_identidad: z.string().nullable().optional(), tipo_documento: z.string().nullable().optional()}); export const panelResponse = z.object({id: z.number().int(), nombre: z.string(), potencia_wp: z.number().int(), tipo_instalacion: z.string(), marca_hubspot: z.string().nullable().optional(), modelo_hubspot: z.string().nullable().optional(), ficha_tecnica_url: z.string().nullable().optional(), video_url: z.string().nullable().optional(), activo: z.boolean(), created_at: z.string()}); export const microInvestorResponse = z.object({id: z.number().int(), nombre: z.string(), modelo_hubspot: z.string().nullable().optional(), ficha_tecnica_url: z.string().nullable().optional(), video_url: z.string().nullable().optional(), tipo_instalacion: z.string().nullable().optional(), activo: z.boolean(), created_at: z.string()}); export const promotionResponse = z.object({id: z.number().int(), nombre: z.string(), tipo_instalacion: z.string(), descuento: z.string(), aplicacion: z.string(), activo: z.boolean(), created_at: z.string()}); export const extraResponse = z.object({id: z.number().int(), nombre: z.string(), tipo: z.string(), precio: z.string(), tipo_instalacion: z.string().nullable().optional(), ficha_tecnica_url: z.string().nullable().optional(), video_url: z.string().nullable().optional(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const provinceProdResponse = z.object({id: z.number().int(), provincia: z.string(), produccion_microinv: z.number().int(), produccion_inv_central: z.number().int(), descuento: z.number().nullable(), precio_maximo_descuento: z.number().nullable(), created_at: z.string(), updated_at: z.string().nullable()}); export const structureResponse = z.object({id: z.number().int(), nombre: z.string(), precio_por_panel: z.string(), marca_hubspot: z.string().nullable().optional(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const optionResponse = z.object({id: z.number().int(), nombre: z.string(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const paymentMethodResponse = z.object({id: z.number().int(), nombre: z.string(), activo: z.boolean(), created_at: z.string()}); export const paymentMethodCashResponse = z.object({id: z.number().int(), nombre: z.string(), descuento: z.string().nullable(), forma_pago_id: z.number().int(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const batteryResponse = z.object({id: z.number().int(), nombre: z.string(), precio: z.string(), promocion_id: z.number().int().nullable().optional(), marca_hubspot: z.string().nullable().optional(), modelo_hubspot: z.string().nullable().optional(), ficha_tecnica_url: z.string().nullable().optional(), video_url: z.string().nullable().optional(), tipo_instalacion: z.string().nullable().optional(), capacidad_kwh: z.string().nullable().optional(), capacidad_kwh_ampliable: z.string().nullable().optional(), potencia_salida: z.string().nullable().optional(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const electricalProtectionResponse = z.object({id: z.number().int(), nombre: z.string(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const financingQuotaPromotionResponse = z.object({id: z.number().int(), nombre: z.string(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const groundTypeResponse = z.object({id: z.number().int(), nombre: z.string(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const subsidyResponse = z.object({id: z.number().int(), nombre: z.string(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const financingSignedResponse = z.object({id: z.number().int(), nombre: z.string(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const constructionLicenseResponse = z.object({id: z.number().int(), nombre: z.string(), activo: z.boolean(), created_at: z.string(), updated_at: z.string().nullable()}); export const allCatalogsResponse = z.object({panels: z.array(panelResponse), micro_investors: z.array(microInvestorResponse), promotions: z.array(promotionResponse), extras: z.array(extraResponse), provinces: z.array(provinceProdResponse), structures: z.array(structureResponse), options: z.array(optionResponse), payment_methods: z.array(paymentMethodResponse), payment_methods_cash: z.array(paymentMethodCashResponse), financing_entities: z.array(z.string()), available_months: z.array(z.number().int()), financing_entity_months: z.record(z.string(), z.array(z.number().int())), battery: z.array(batteryResponse), electrical_protections: z.array(electricalProtectionResponse), financing_quota_promotions: z.array(financingQuotaPromotionResponse), ground_types: z.array(groundTypeResponse), subsidies: z.array(subsidyResponse), financing_signed: z.array(financingSignedResponse), construction_licenses: z.array(constructionLicenseResponse)}); export const completeBudgetFormResponse = z.object({client: contactDetailsResponse, commercial_name: z.string().nullable().optional(), commercial_phone: z.string().nullable().optional(), catalogs: allCatalogsResponse}); 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 }; /** * Get Order Form Endpoint * * GET /api/order/form */ export const getApiApiOrderForm = async (args: GetApiApiOrderFormArgs, options: GetApiApiOrderFormOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.email != null) query.append("email", String(args.email)) if (args.comercial_id != null) query.append("comercial_id", String(args.comercial_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/order/form${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(completeBudgetFormResponse, await res.json()) };