Booking API
Work with booking data from simulations
Interactive Examples
Fetch Bookings
No bookings loaded yet. Click the button to fetch bookings.
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 bookings = api.collections.booking;
Fetching Bookings
// Fetch first 20 bookings
await bookings.fetch({ limit: 20 });
// Fetch with offset
await bookings.fetch({ limit: 20, offset: 20 });
// Fetch all bookings (be careful with large datasets)
await bookings.fetchAll();
Accessing Booking Data
// Get all bookings
const allBookings = bookings.models;
// Get a specific booking by ID
const booking = bookings.get(7764432);
// Check if a booking exists
const exists = bookings.has(7764432);
// Get first/last booking
const first = bookings.first();
const last = bookings.last();
// Check collection state
const isEmpty = bookings.isEmpty();
const count = bookings.length;
const hasMore = bookings.hasMore;
Booking Model
interface Booking {
id: number;
created_at: string;
modified_at: string;
uid: string;
group_uid: string;
has_linked_bookings: boolean;
customer_id: string;
data: Record<string, any>;
requested_at: string;
min_pickup_time: string;
max_pickup_time: string;
min_dropoff_time: string;
max_dropoff_time: string;
min_trip_duration: number | null;
max_trip_duration: number;
pickup_location_lat: number;
pickup_location_lon: number;
pickup_location_name: string;
dropoff_location_lat: number;
dropoff_location_lon: number;
dropoff_location_name: string;
state: string;
booking_type: string;
planned_pickup_time: string | null;
planned_dropoff_time: string | null;
actual_pickup_time: string | null;
actual_dropoff_time: string | null;
planned_journey_duration: number | null;
actual_journey_duration: number | null;
simulation_id: number;
service_time: number;
demand: Record<string, number>;
penalty: number;
project_id: number;
is_invalidated: boolean;
pickup_service_time: number;
dropoff_service_time: number;
vehicle_characteristics: Record<string, any>;
vehicle_labels: Record<string, any>;
air_distance: number;
// ... many more fields
}
Collection Utility Methods
Grouping
// Group bookings by simulation
const bySimulation = bookings.groupBy('simulation_id');
// Group by state
const byState = bookings.groupBy('state');
// Group by booking type
const byType = bookings.groupBy('booking_type');
Sorting
// Sort by pickup time
const byPickupTime = bookings.sortBy('min_pickup_time');
// Sort by ID
const byId = bookings.sortBy('id');
// Sort by custom function
const sorted = bookings.sortBy(b => b.air_distance);
Filtering
// Filter with custom logic
const longDistance = bookings.filter(b => b.air_distance > 10000);
// Find specific booking
const found = bookings.find(b => b.uid === '4cab6d99-7642-4f41-8521-f8983ec8cd37');
// Where clause
const filtered = bookings.where({
project_id: 929,
state: 'assigned'
});
Plucking Values
// Get all simulation IDs
const simIds = bookings.pluck('simulation_id');
// Get all states
const states = bookings.pluck('state');
// Get all pickup locations
const pickupLocations = bookings.models.map(b => ({
lat: b.pickup_location_lat,
lon: b.pickup_location_lon
}));
Pagination
// Fetch first page
await bookings.fetch({ limit: 10 });
console.log(`Loaded ${bookings.length} bookings`);
console.log(`Has more: ${bookings.hasMore}`);
// Load next page
if (bookings.hasMore) {
await bookings.fetchNext();
console.log(`Now have ${bookings.length} bookings`);
}
Error Handling
try {
await bookings.fetch({ limit: 20 });
console.log('Bookings loaded:', bookings.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 Bookings for a Simulation
// Fetch all bookings filtered by simulation on the server
await bookings.fetchAll({ simulation_id: 211238 });
console.log(`Simulation has ${bookings.length} bookings`);
Find Assigned Bookings
// Fetch all assigned bookings filtered on the server
await bookings.fetchAll({ state: 'assigned' });
console.log(`${bookings.length} bookings are assigned`);
Calculate Average Distance
await bookings.fetch({ limit: 100 });
const distances = bookings.pluck('air_distance');
const avgDistance = distances.reduce((a, b) => a + b, 0) / distances.length;
console.log(`Average distance: ${avgDistance.toFixed(2)} meters`);
Type Definitions
The full Booking type is exported from the library:
import type { Booking } from 'sgerp-frontend-lib';
const booking: Booking = {
id: 7764432,
uid: '4cab6d99-7642-4f41-8521-f8983ec8cd37',
state: 'assigned',
pickup_location_lat: 1.35948337264389,
pickup_location_lon: 103.833963930413,
dropoff_location_lat: 1.30762282594567,
dropoff_location_lon: 103.82859041681,
simulation_id: 211238,
project_id: 929,
// ... other fields
};