import {z} from 'zod' export type CreateContactRequestDTO = {name?: (string | null) | undefined, phone?: (string | null) | undefined, email?: (string | null) | undefined}; export type State = 'AC' | 'AL' | 'AP' | 'AM' | 'BA' | 'CE' | 'DF' | 'ES' | 'GO' | 'MA' | 'MT' | 'MS' | 'MG' | 'PA' | 'PB' | 'PR' | 'PE' | 'PI' | 'RJ' | 'RN' | 'RS' | 'RO' | 'RR' | 'SC' | 'SP' | 'SE' | 'TO'; export type CreateDriverRequestDTO = {name: string, document: string, cnh: string, state: State, cnh_expiration_date?: (string | null) | undefined, first_cnh_date?: (string | null) | undefined, birth_date?: (string | null) | undefined}; export type CreateVehicleRequestDTO = {license_plate: string, state?: (State | null) | undefined, owner_document?: (string | null) | undefined, renavam?: (string | null) | undefined, chassis?: (string | null) | undefined}; export type CreateVehicleHistoryDTO = {license_plate: string}; export type V2PackagesEnterpriseSchemasMonitoringRequestsCreateSubscriptionRequestDTO = {external_id?: (string | null) | undefined, contact?: (CreateContactRequestDTO | null) | undefined, drivers?: Array | undefined, vehicles?: Array | undefined, vehicles_history?: Array | undefined}; export type CreateApiMonitoringPlansPlanIdSubscriptionsArgs = {plan_id: string, body: V2PackagesEnterpriseSchemasMonitoringRequestsCreateSubscriptionRequestDTO}; export type CreateApiMonitoringPlansPlanIdSubscriptionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const restContactDTO = z.object({name: z.string().nullable().optional(), email: z.string().nullable().optional(), phone: z.string().nullable().optional()}); export const state = z.enum(["AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SP", "SE", "TO"]); export const restVehicleResponseDTO = z.object({id: z.string(), contract_id: z.string(), license_plate: z.string(), state: state.nullable().optional(), owner_document: z.string().nullable().optional(), renavam: z.string().nullable().optional(), chassis: z.string().nullable().optional(), is_active: z.boolean().nullable().optional()}); export const restDriverResponseDTO = z.object({name: z.string(), document: z.string(), actual_points: z.number().int().nullable().optional()}); export const restVehicleHistoryDTO = z.object({license_plate: z.string()}); export const restCompleteSubscriptionResponseDTO = z.object({id: z.string(), contact: restContactDTO, external_id: z.string().nullable().optional(), created_at: z.string(), next_search_at: z.string().nullable().optional(), plan_id: z.string(), is_active: z.boolean(), is_canceled: z.boolean(), vehicles: z.array(restVehicleResponseDTO).optional(), drivers: z.array(restDriverResponseDTO).optional(), vehicle_histories: z.array(restVehicleHistoryDTO).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 }; /** * Cria uma nova assinatura de monitoramento * * POST /monitoring/plans/{plan_id}/subscriptions */ export const createApiMonitoringPlansPlanIdSubscriptions = async (args: CreateApiMonitoringPlansPlanIdSubscriptionsArgs, options?: CreateApiMonitoringPlansPlanIdSubscriptionsOptions) => { 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(/\/+$/, '')}/monitoring/plans/${encodeURIComponent(String(args.plan_id))}/subscriptions${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(restCompleteSubscriptionResponseDTO, await res.json()) };