import {z} from 'zod' export type VehicleDebtCustomerDTO = {email?: (string | null) | undefined, phone?: (string | null) | undefined, document?: (string | null) | undefined, name?: (string | null) | undefined}; export type VehicleDebtSearchExtraDataCustomDataDTO = {dealership?: (string | null) | undefined, dealership_document?: (string | null) | undefined, customer_score?: (string | null) | undefined, customer_age?: (string | null) | undefined, seller_age?: (string | null) | undefined, active_customer?: (string | null) | undefined, defaulting_customer?: (string | null) | undefined}; export type VehicleDebtSearchExtraDataAffiliatesDataDTO = {coupon?: (string | null) | undefined, gclid?: (string | null) | undefined, utm_source?: (string | null) | undefined, utm_medium?: (string | null) | undefined, utm_keyword?: (string | null) | undefined, utm_campaign?: (string | null) | undefined}; export type VehicleDebtSearchExtraDataDTO = {custom_field1?: (string | Record | null) | undefined, custom_data?: (VehicleDebtSearchExtraDataCustomDataDTO | null) | undefined, issue_ecrlv?: boolean | undefined, affiliates_params?: (VehicleDebtSearchExtraDataAffiliatesDataDTO | null) | undefined}; export type ZpyEnterpriseResourcesDomainDebtSchemasSearchDebtDtosVehicleDebtSearchDTO = {license_plate: string, uf?: (string | null) | undefined, renavam?: (string | null) | undefined, chassis?: (string | null) | undefined, request_id?: (string | null) | undefined, customer?: (VehicleDebtCustomerDTO | null) | undefined, extra_data?: (VehicleDebtSearchExtraDataDTO | null) | undefined}; export type SearchVehicleBatchRequestDTO = {vehicles: Array}; export type CreateApiVehicleDebtsBatchArgs = {body: SearchVehicleBatchRequestDTO}; export type CreateApiVehicleDebtsBatchOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const zpyEnterpriseResourcesDomainWebhookSchemasWebhookEnumResourceEnum = z.enum(["vehicle_debt", "vehicle_debt_batch", "checkout", "autoradar", "payment_recurrence", "documents", "monitoring", "document_order"]); export const webhookShortModel = z.object({id: z.string(), resource: zpyEnterpriseResourcesDomainWebhookSchemasWebhookEnumResourceEnum.optional(), version: z.string().optional()}); export const debtSearchBatchStatus = z.enum(["created", "processing", "done", "failed"]); export const searchVehicleBatchRequestResponseDTOInput = z.object({id: z.string(), webhook: webhookShortModel, fleet_id: z.string().nullable(), created_at: z.string(), updated_at: z.string(), status: debtSearchBatchStatus.optional(), total_vehicles: z.number().int(), processed_vehicles: z.number().int()}); 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 }; /** * Solicitação De Consulta Em Lote * * POST /vehicle/debts/batch/ */ export const createApiVehicleDebtsBatch = async (args: CreateApiVehicleDebtsBatchArgs, options?: CreateApiVehicleDebtsBatchOptions) => { const {baseUrl = "https://api.b2b.usezapay.com.br/v2", 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(/\/+$/, '')}/vehicle/debts/batch/${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(searchVehicleBatchRequestResponseDTOInput, await res.json()) };