API: Create a New Trip Instance

Request

Create a new trip instance with a POST request:

POST /trip-instances

Request Body:


{
  "coach_id": 2,
  "bus_id": 1,
  "schedule_id": 1,
  "seat_plan_id": 1,
  "route_id": 1,
  "coach_type": 1,
  "trip_date": "2025-09-18",
  "boarding_dropping_points": [
    {
      "counter_id": 1,
      "type": 1,
      "time": "08:00",
      "starting_point_status": true,
      "ending_point_status": false,
      "status": 1
    },
    {
      "counter_id": 2,
      "type": 1,
      "time": "08:30",
      "starting_point_status": false,
      "ending_point_status": false,
      "status": 1
    },
    {
      "counter_id": 3,
      "type": 2,
      "time": "14:00",
      "starting_point_status": false,
      "ending_point_status": false,
      "status": 1
    },
    {
      "counter_id": 4,
      "type": 2,
      "time": "14:30",
      "starting_point_status": false,
      "ending_point_status": true,
      "status": 1
    }
  ]
}
                

Sample Response:


{
  "status": "success",
  "code": 201,
  "message": "Trip instance created successfully",
  "data": {
    "data": {
      "trip_instance": {
        "coach_id": 2,
        "bus_id": 1,
        "schedule_id": 1,
        "seat_plan_id": 1,
        "route_id": 1,
        "coach_type": 1,
        "driver_id": null,
        "supervisor_id": null,
        "trip_date": "2025-09-28T00:00:00.000000Z",
        "status": 1,
        "migrated_trip_id": null,
        "created_by": 2,
        "id": 5,
        "updated_at": "2025-08-17T10:28:31.000000Z",
        "created_at": "2025-08-17T10:28:31.000000Z"
      },
      "seat_inventory_created": true,
      "seat_inventory": {
        "trip_id": 5,
        "total_seats": 24,
        "created_inventories": 24,
        "inventories": [
          {
            "trip_id": 5,
            "seat_id": 1,
            "booking_status": 1,
            "blocked_until": null,
            "booking_id": null,
            "last_locked_user_id": null,
            "created_by": 2,
            "id": 196,
            "updated_at": "2025-08-17T10:28:31.000000Z",
            "created_at": "2025-08-17T10:28:31.000000Z"
          },
          {
            "trip_id": 5,
            "seat_id": 2,
            "booking_status": 1,
            "blocked_until": null,
            "booking_id": null,
            "last_locked_user_id": null,
            "created_by": 2,
            "id": 198,
            "updated_at": "2025-08-17T10:28:31.000000Z",
            "created_at": "2025-08-17T10:28:31.000000Z"
          }
        ]
      }
    },
    "message": "Trip instance created successfully with seat inventory"
  }
}
                

Validation Rules:

  • coach_id - Required, must exist in coaches table
  • bus_id - Required, must exist in buses table
  • schedule_id - Required, must exist in schedules table
  • seat_plan_id - Required, must exist in seat_plans table
  • route_id - Required, must exist in routes table
  • coach_type - Required, 1 = AC, 2 = Non-AC
  • trip_date - Required, valid date format (YYYY-MM-DD HH:MM:SS)

Automatic Features:

  • Seat Inventory Creation - Automatically creates seat inventory for all seats in the seat plan
  • Booking Status - All seats start with booking_status = 1 (available)
  • Partition Management - Automatically handles database partitioning by month
  • User Tracking - Automatically sets created_by to the authenticated user

Response Components:

  • trip_instance - The created trip instance details
  • seat_inventory_created - Boolean indicating if seat inventory was created
  • seat_inventory - Details about the created seat inventory including all individual seat records
  • total_seats - Total number of seats in the seat plan
  • created_inventories - Number of seat inventories successfully created

Notes:

  • Creating a trip instance automatically generates seat inventory for booking management
  • Each seat starts as available (booking_status = 1)
  • The response includes complete seat inventory data for immediate use
  • Trip instances are partitioned by month for performance optimization