import {z} from 'zod' export type ShiftStatus = 'PLANNED' | 'ACTIVE' | 'INACTIVE' | 'ON_BREAK' | 'COMPLETED' | 'CANCELLED'; export type ShiftBreakConfigurationInput = {timeWindow: Array, breakDuration: number}; export type BreakConfig = {anchor?: ('shift_start' | null) | undefined, preferOffRoute?: boolean | undefined}; export type ShiftVehicleConfigInput = {weight?: number | undefined, height?: number | undefined, length?: number | undefined, width?: number | undefined, axleCount?: number | undefined, trailerCount?: number | undefined, weightUnit?: ('kg' | 'lbs') | undefined, lengthUnit?: ('meter' | 'in') | undefined, maxStops?: number | undefined, maxOrdersAtOnce?: number | undefined, maxDistance?: number | undefined, maxDuration?: number | undefined, speedFactor?: number | undefined, capabilities?: Array | undefined, fixedCost?: (number | null) | undefined, costPerUnitDistance?: (number | null) | undefined, costPerUnitTime?: (number | null) | undefined, vehicleProfile?: (string | null) | undefined, minOrdersPerRoute?: (number | null) | undefined, parkingTime?: (number | null) | undefined}; export type ShiftOptimizationParamsInput = {serviceTimeRelativeEfficiency?: (number | null) | undefined, plannedBreaks?: Array | undefined, breakConfig?: (BreakConfig | null) | undefined, departureDelay?: (number | null) | undefined, shiftEndServiceTimeMinutes?: (number | null) | undefined, vehicleConfig?: (ShiftVehicleConfigInput | null) | undefined}; export type ShiftUpsertInput = {external_id: string, status?: ShiftStatus | undefined, store_location_id?: (string | null) | undefined, zone_ids?: Array | undefined, courier_id?: (string | null) | undefined, vehicle_id?: (string | null) | undefined, name?: (string | null) | undefined, planned_start: string, planned_end: string, planned_break_duration?: number | undefined, optimization_params?: (ShiftOptimizationParamsInput | null) | undefined, shift_metadata?: {[key: string]: unknown} | undefined}; export type UpsertShiftsByExternalIdRequest = {shifts: Array}; export type CreateApiV1ShiftsUpsertByExternalIdArgs = {body: UpsertShiftsByExternalIdRequest}; export type CreateApiV1ShiftsUpsertByExternalIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const shiftUpsertOutcome = z.enum(["created", "updated", "unchanged"]); export const shiftStatus = z.enum(["PLANNED", "ACTIVE", "INACTIVE", "ON_BREAK", "COMPLETED", "CANCELLED"]); export const plannedBreakWindow = z.object({start_offset: z.string(), end_offset: z.string(), duration: z.string()}); export const shiftVehicleConfig = z.object({vehicle_profile: z.string().nullable().optional(), speed_factor: z.number().nullable().optional(), parking_time: z.number().nullable().optional()}); export const shiftRead = z.object({id: z.string(), organization_id: z.string(), external_id: z.string().nullable().optional(), route_id: z.string().nullable().optional(), store_location_id: z.string().nullable().optional(), zone_ids: z.array(z.string()).optional(), courier_id: z.string().nullable().optional(), vehicle_id: z.string().nullable().optional(), name: z.string().nullable().optional(), status: shiftStatus, timezone_id: z.string().nullable().optional(), planned_start: z.string().nullable().optional(), planned_end: z.string().nullable().optional(), actual_start: z.string().nullable().optional(), actual_end: z.string().nullable().optional(), planned_breaks: z.array(plannedBreakWindow).optional(), taken_break_count: z.number().int().optional(), vehicle_config: shiftVehicleConfig.nullable().optional()}); export const shiftUpsertReceipt = z.object({index: z.number().int(), external_id: z.string(), id: z.string(), outcome: shiftUpsertOutcome, shift: shiftRead}); export const upsertShiftsByExternalIdResponse = z.object({results: z.array(shiftUpsertReceipt)}); 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 }; /** * Upsert shifts by external id * * POST /v1/shifts/upsert-by-external-id */ export const createApiV1ShiftsUpsertByExternalId = async (args: CreateApiV1ShiftsUpsertByExternalIdArgs, options?: CreateApiV1ShiftsUpsertByExternalIdOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/shifts/upsert-by-external-id${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(upsertShiftsByExternalIdResponse, await res.json()) };