import {z} from 'zod' export type MileageSort = {orderBy?: ('DATE' | 'STATUS') | undefined, direction?: ('ASC' | 'DESC') | undefined}; export type MileageStatusListFilter = {in?: (Array<'ACTIVE' | 'INACTIVE' | 'REMOVED'>) | undefined, notIn?: (Array<'ACTIVE' | 'INACTIVE' | 'REMOVED'>) | undefined, equal?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, notEqual?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, greaterThan?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, greaterThanEqual?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, lessThan?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, lessThanEqual?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined}; export type MileageTripStatusListFilter = {in?: (Array<'PENDING' | 'BUSINESS' | 'PERSONAL'>) | undefined, notIn?: (Array<'PENDING' | 'BUSINESS' | 'PERSONAL'>) | undefined, equal?: ('PENDING' | 'BUSINESS' | 'PERSONAL') | undefined, notEqual?: ('PENDING' | 'BUSINESS' | 'PERSONAL') | undefined, greaterThan?: ('PENDING' | 'BUSINESS' | 'PERSONAL') | undefined, greaterThanEqual?: ('PENDING' | 'BUSINESS' | 'PERSONAL') | undefined, lessThan?: ('PENDING' | 'BUSINESS' | 'PERSONAL') | undefined, lessThanEqual?: ('PENDING' | 'BUSINESS' | 'PERSONAL') | undefined}; export type MileageFilterParam = {status?: MileageStatusListFilter | undefined, mileageStatus?: MileageTripStatusListFilter | undefined}; export type MileageGetParam = {cursor?: (string | null) | undefined, limit?: number | undefined, sort?: MileageSort | undefined, filter?: MileageFilterParam | undefined}; export type CreateApiV6MileageQueryArgs = {body: MileageGetParam}; export type CreateApiV6MileageQueryOptions = {baseUrl?: string | undefined, EmbedToken?: string | undefined, fetch?: typeof fetch | undefined}; export const customerExpanded = z.object({id: z.string(), name: z.string().optional(), email: z.string().optional()}).nullable(); export const dimensionExpanded = z.object({id: z.string(), name: z.string().optional(), requiredness: z.enum(["REQUIRED", "NOT_REQUIRED"]).optional()}); export const childDimensionValue: z.ZodType = z.object({id: z.string(), dimension: dimensionExpanded.optional(), name: z.string().optional(), child: z.lazy(() => childDimensionValue.optional())}); export const dimensionValueExpanded = z.object({id: z.string(), dimension: dimensionExpanded.optional(), name: z.string().optional(), child: childDimensionValue.optional()}); export const mileageDistanceGetDto = z.object({type: z.enum(["MILE", "KILOMETER"]), value: z.string()}).nullable(); export const locationExpanded = z.object({id: z.string(), name: z.string()}).nullable(); export const addressGetDto = z.object({countryCode: z.enum(["USA", "CAN", "GBR"]).optional(), state: z.string().optional(), city: z.string().optional(), zip: z.string().optional(), phoneWork: z.string().optional(), address1: z.string().optional(), address2: z.string().optional()}).nullable(); export const mileageLocationGetDto = z.object({time: z.string(), lat: z.string(), lng: z.string(), location: locationExpanded.nullable(), address: addressGetDto}).nullable(); export const mileageDeductionGetDto = z.object({amount: z.number().int()}).nullable(); export const waypointGetDto = z.object({lat: z.string(), lng: z.string()}); export const routeExpanded = z.object({id: z.string(), waypoints: z.array(waypointGetDto)}).nullable(); export const carExpanded = z.object({id: z.string(), name: z.string().nullable(), make: z.string().nullable(), model: z.string().nullable()}).nullable(); export const jsonNode = z.object({empty: z.boolean().optional(), array: z.boolean().optional(), null: z.boolean().optional(), object: z.boolean().optional(), float: z.boolean().optional(), string: z.boolean().optional(), number: z.boolean().optional(), missingNode: z.boolean().optional(), valueNode: z.boolean().optional(), nodeType: z.enum(["ARRAY", "BINARY", "BOOLEAN", "MISSING", "NULL", "NUMBER", "OBJECT", "POJO", "STRING"]).optional(), pojo: z.boolean().optional(), integralNumber: z.boolean().optional(), floatingPointNumber: z.boolean().optional(), short: z.boolean().optional(), int: z.boolean().optional(), long: z.boolean().optional(), double: z.boolean().optional(), bigDecimal: z.boolean().optional(), bigInteger: z.boolean().optional(), textual: z.boolean().optional(), boolean: z.boolean().optional(), binary: z.boolean().optional(), container: z.boolean().optional(), embeddedValue: z.boolean().optional()}).nullable(); export const mileageGetDto = z.object({id: z.string(), date: z.string(), status: z.enum(["ACTIVE", "INACTIVE", "REMOVED"]), mileageStatus: z.enum(["PENDING", "BUSINESS", "PERSONAL"]), description: z.string().nullable(), notes: z.string().nullable(), customer: customerExpanded.nullable(), dimensionValues: z.array(dimensionValueExpanded), distance: mileageDistanceGetDto, start: mileageLocationGetDto, end: mileageLocationGetDto, duration: z.number().int(), deduction: mileageDeductionGetDto, route: routeExpanded.nullable(), car: carExpanded.nullable(), customData: jsonNode.optional()}); export const responseCursor = z.object({after: z.string().optional(), before: z.string().optional()}); export const paginatedResponseMileageGetDto = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: z.array(mileageGetDto).optional(), cursor: responseCursor.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 }; /** * Query mileage * * POST /v6/mileage/query */ export const createApiV6MileageQuery = async (args: CreateApiV6MileageQueryArgs, options?: CreateApiV6MileageQueryOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, EmbedToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (EmbedToken !== undefined) headers.set('authorization', `Bearer ${EmbedToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v6/mileage/query${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(paginatedResponseMileageGetDto, await res.json()) };