VehicleType API

Work with vehicle type definitions

Interactive Examples

Fetch Vehicle Types

No vehicle types loaded yet. Click the button to fetch vehicle types.

Collection Access

import { initializeSGERP } from 'sgerp-frontend-lib';

const api = initializeSGERP({
  activeConnection: 'admin@localhost',
  connections: {
    'admin@localhost': {
      id: 'admin@localhost',
      environment: 'localhost',
      user: 'admin',
      password: 'your-password',
      baseUrl: 'https://sgerp-stage.d.gcdev.swatrider.com',
    },
  },
});

const vehicleTypes = api.collections.vehicletype;

Fetching Vehicle Types

// Fetch first 20 vehicle types
await vehicleTypes.fetch({ limit: 20 });

// Fetch with offset
await vehicleTypes.fetch({ limit: 20, offset: 20 });

// Fetch all vehicle types
await vehicleTypes.fetchAll();

Accessing Vehicle Type Data

// Get all vehicle types
const allTypes = vehicleTypes.models;

// Get a specific vehicle type by ID
const type = vehicleTypes.get(1);

// Check if a vehicle type exists
const exists = vehicleTypes.has(1);

// Get first/last vehicle type
const first = vehicleTypes.first();
const last = vehicleTypes.last();

// Check collection state
const isEmpty = vehicleTypes.isEmpty();
const count = vehicleTypes.length;
const hasMore = vehicleTypes.hasMore;

VehicleType Model

interface VehicleType {
  id: number;
  created_at: string;
  modified_at: string;
  capacity: Record<string, number>; // Common keys: passenger, wheelchair, g, cbcm
  routing_engine_settings: {
    key: string;
    url: string;
    road_network: string;
  };
  characteristics: Record<string, any>;
  efficiency: any;
  geofence_ids: number[];
  labels: string[];
  vehicle_cost: number;
  lifo_order_check: boolean;
  max_physical_stops: number | null;
  dynamic_break_duration: number | null;
  dynamic_break_min_path_duration: number | null;
  dynamic_break_avg_time_between_breaks: number | null;
  dynamic_break_max_latency: number | null;
  number_of_trips: number | null;
  max_trip_length: number | null;
  max_trip_duration: number | null;
  max_pickup_locations: number | null;
  max_dropoff_locations: number | null;
  zero_cost_if_only_partial_routes: any;
  assigned_nodes_protection_interval: number | null;
  assigned_nodes_protection_max_locations: number | null;
  project_id: number;
  name: string;
}

Collection Utility Methods

Grouping

// Group vehicle types by project
const byProject = vehicleTypes.groupBy('project_id');

// Group by road network
const byRoadNetwork = vehicleTypes.groupBy(vt => vt.routing_engine_settings.road_network);

Sorting

// Sort by name
const byName = vehicleTypes.sortBy('name');

// Sort by cost
const byCost = vehicleTypes.sortBy('vehicle_cost');

// Sort by custom function
const sorted = vehicleTypes.sortBy(vt => vt.max_trip_duration ?? 0);

Filtering

// Filter with custom logic
const expensiveTypes = vehicleTypes.filter(vt => vt.vehicle_cost > 20000);

// Find specific vehicle type
const found = vehicleTypes.find(vt => vt.name === 'BBTDC_10W');

// Where clause
const filtered = vehicleTypes.where({
  project_id: 712,
  lifo_order_check: false
});

Plucking Values

// Get all names
const names = vehicleTypes.pluck('name');

// Get all costs
const costs = vehicleTypes.pluck('vehicle_cost');

// Get all project IDs
const projectIds = vehicleTypes.pluck('project_id');

Pagination

// Fetch first page
await vehicleTypes.fetch({ limit: 10 });

console.log(`Loaded ${vehicleTypes.length} vehicle types`);
console.log(`Has more: ${vehicleTypes.hasMore}`);

// Load next page
if (vehicleTypes.hasMore) {
  await vehicleTypes.fetchNext();
  console.log(`Now have ${vehicleTypes.length} vehicle types`);
}

Error Handling

try {
  await vehicleTypes.fetch({ limit: 20 });
  console.log('Vehicle types loaded:', vehicleTypes.length);
} catch (error) {
  if (error instanceof APIError) {
    console.error('API Error:', error.message);
    console.error('Status:', error.status);
  } else {
    console.error('Error:', error);
  }
}

Common Patterns

Get All Vehicle Types for a Project

// Fetch all vehicle types filtered by project on the server
await vehicleTypes.fetchAll({ project_id: 712 });
console.log(`Project has ${vehicleTypes.length} vehicle types`);

Find Vehicle Types with Labels

await vehicleTypes.fetch({ limit: 100 });
const withFreezer = vehicleTypes.filter(vt => vt.labels.includes('Freezer'));
console.log(`${withFreezer.length} vehicle types have freezer capability`);

Calculate Average Vehicle Cost

await vehicleTypes.fetch({ limit: 100 });
const costs = vehicleTypes.pluck('vehicle_cost');
const avgCost = costs.reduce((a, b) => a + b, 0) / costs.length;
console.log(`Average vehicle cost: ${avgCost.toFixed(2)}`);

Type Definitions

The full VehicleType type is exported from the library:

import type { VehicleType } from 'sgerp-frontend-lib';

const vehicleType: VehicleType = {
  id: 1,
  name: 'BBTDC_10W',
  capacity: { g: 2000000, cbcm: 1000000 },
  routing_engine_settings: {
    key: '...',
    url: 'http://mapbox-osrm-proxy',
    road_network: 'van'
  },
  project_id: 712,
  // ... other fields
};