import {z} from 'zod' export type BrokerageContactRequest = {name?: string | undefined, email?: string | undefined, phone?: string | undefined, phoneExtension?: string | undefined, tmsId?: string | undefined, roles?: (Array<'CARRIER_REP' | 'ACCOUNT_MANAGER' | 'OPERATIONS_REP' | 'OPERATIONS_MANAGER' | 'BOOKED_BY' | 'CUSTOMER_REP' | 'CUSTOMER_TEAM' | 'CARRIER_TEAM' | 'AP_REP' | 'AFTER_HOURS_REP'>) | undefined, preferredContactMode?: ('EMAIL' | 'PHONE_CALL' | 'TEXT_MESSAGE' | 'TELEGRAM') | undefined}; export type CarrierContactRequest = {name?: string | undefined, email?: string | undefined, phone?: string | undefined, phoneExtension?: string | undefined, tmsId?: string | undefined, roles?: (Array<'ACCOUNTANT' | 'AP_REP' | 'DISPATCHER' | 'AFTER_HOURS_DISPATCHER' | 'WEEKEND_DISPATCHER' | 'BOOKING' | 'TRACKING' | 'DRIVER' | 'OTHER'>) | undefined, preferredContactMode?: ('EMAIL' | 'PHONE_CALL' | 'TEXT_MESSAGE' | 'TELEGRAM') | undefined}; export type CarrierCustomData = {[key: string]: unknown}; export type CarrierRequest = {name: string, tmsId: string, status?: ({code?: ('ACTIVE' | 'DO_NOT_USE') | undefined, reason?: string | undefined}) | undefined, complianceStatus?: ({codes?: {[key: string]: string} | undefined}) | undefined, mcNumber?: number | undefined, dotNumber?: number | undefined, scac?: string | undefined, address?: ({street1?: string | undefined, street2?: string | undefined, city?: string | undefined, stateOrProvince?: string | undefined, postalCode?: string | undefined, country?: string | undefined, name?: string | undefined, company?: string | undefined}) | undefined, email?: string | undefined, phone?: string | undefined, phoneExtension?: string | undefined, managers?: Array | undefined, contacts?: Array | undefined, notes?: string | undefined, customData?: CarrierCustomData | undefined}; export type BulkUpsertCarriersRequest = {carriers: Array}; export type CreateApiV1CarriersBulkArgs = {body: BulkUpsertCarriersRequest}; export type CreateApiV1CarriersBulkOptions = {baseUrl?: string | undefined, httpBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const bulkUpsertCarriersResponse = z.object({total: z.number().int().optional(), successCount: z.number().int().optional(), failureCount: z.number().int().optional(), errors: z.array(z.object({index: z.number().int().optional(), error: z.string().optional()})).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 }; /** * Bulk create or update carriers * * POST /v1/carriers/bulk */ export const createApiV1CarriersBulk = async (args: CreateApiV1CarriersBulkArgs, options?: CreateApiV1CarriersBulkOptions) => { const {baseUrl = "https://api.prod.goaugment.com", fetch: fetchFn = fetch, httpBasic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (httpBasic !== undefined) headers.set('authorization', `Basic ${btoa(`${httpBasic.username}:${httpBasic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/carriers/bulk${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(bulkUpsertCarriersResponse, await res.json()) };