Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Codefied-CodePix/Karokar-backend/llms.txt

Use this file to discover all available pages before exploring further.

Bookings are the primary revenue-generating business process in KaroKar. Every vendor-owned vehicle transaction flows through a formal booking lifecycle that governs who can act, what actions are permitted, and when availability is reserved or released. The lifecycle is enforced by an explicit state machine inside BookingService — ensuring that every transition is intentional, validated, and accompanied by a domain event that downstream domains can react to.
Booking state transitions must only occur through BookingService methods. Never update booking.status directly from controllers, scheduled jobs, event listeners, or any code outside the service layer. Direct status mutations bypass transition validation, skip domain event emission, and break availability synchronization.

Booking Entity

FieldTypeDescription
iduuidUnique booking identifier (from BaseEntity)
vehicleIduuidThe vehicle being booked
vendorOrganizationIduuidThe vendor who owns the vehicle
corporateOrganizationIduuidThe corporate organization making the booking
requestedByuuidUser ID of the corporate employee who created the request
startDatetimestamptzRequested booking period start
endDatetimestamptzRequested booking period end
statusBookingStatusCurrent state in the booking lifecycle
createdAttimestamptzRecord creation timestamp
updatedAttimestamptzLast modification timestamp

Booking States

KaroKar defines nine booking states covering the full lifecycle from initial drafting to final resolution.
StateDescription
DRAFTBooking created but not yet submitted to the vendor
REQUESTEDBooking submitted; awaiting vendor decision
APPROVEDVendor accepted the booking; vehicle reserved
REJECTEDVendor declined the booking; terminal state
ASSIGNEDAn employee has been assigned to the approved booking
ACTIVEVehicle has been handed over to the employee
COMPLETEDVehicle returned; booking reached its planned conclusion
TERMINATEDBooking ended prematurely after becoming operational
CANCELLEDBooking cancelled before vehicle handover

State Transition Table

Transitions are strictly enforced by the ALLOWED_TRANSITIONS map in BookingService. Attempting an invalid transition throws a DomainException with code INVALID_BOOKING_TRANSITION.
Current StateAllowed Next States
DRAFTREQUESTED
REQUESTEDAPPROVED, REJECTED, CANCELLED
APPROVEDASSIGNED, CANCELLED
ASSIGNEDACTIVE, CANCELLED
ACTIVECOMPLETED, TERMINATED
COMPLETED(terminal — no further transitions)
TERMINATED(terminal — no further transitions)
REJECTED(terminal — no further transitions)
CANCELLED(terminal — no further transitions)

Vendor-Owned Vehicle Booking Workflow

The following walkthrough covers the complete journey for a vendor-owned vehicle from the corporate organization’s perspective.
1

Create a Draft

A corporate user selects a vehicle and desired dates. A POST /bookings call with CreateBookingDto creates the booking in DRAFT status. No availability record is created yet — the vehicle remains searchable by others.
// CreateBookingDto
{
  vehicleId: "uuid",
  vendorOrganizationId: "uuid",
  corporateOrganizationId: "uuid",
  requestedBy: "uuid",
  startDate: "2026-07-01T00:00:00Z",
  endDate: "2026-07-31T23:59:59Z"
}
2

Submit the Request

BookingService.submitRequest(id) transitions the booking from DRAFT → REQUESTED and emits BookingRequestedEvent. The vendor is notified. Still no availability block is created — the vendor may reject.
3

Vendor Approves

The vendor calls BookingService.approveBooking(id, dto), transitioning the booking to APPROVED. A BookingApprovedEvent is emitted.Availability impact: The Availability domain listens to booking.approved and creates a VehicleAvailability record:
sourceType: BOOKING
status:     RESERVED
startDate:  booking.startDate
endDate:    booking.endDate
The vehicle is now blocked from overlapping bookings.
4

Assign an Employee

The corporate admin calls BookingService.assignEmployee(id, employeeId), transitioning the booking to ASSIGNED and emitting BookingAssignedEvent.Availability impact: The RESERVED availability record transitions to ALLOCATED.Assignment domain impact: The Assignment domain listens to booking.assigned and creates an Assignment record linking the booking to the employee.
5

Activate the Booking

When the employee physically receives the vehicle, BookingService.activateBooking(id) transitions the booking to ACTIVE and emits BookingActivatedEvent. This marks the start of operational usage.
6

Complete or Terminate

  • Planned completion: BookingService.completeBooking(id)COMPLETED. Availability released; vehicle returns to searchable pool.
  • Early termination: BookingService.terminateBooking(id, dto)TERMINATED. A BookingTermination record is persisted. Availability released (unless a maintenance block is required, e.g., after ACCIDENT or VEHICLE_BREAKDOWN).

CANCELLED vs TERMINATED

These two terminal states are explicitly distinct and must not be confused.

CANCELLED

The booking was cancelled before the vehicle was ever handed over to an employee. The vehicle was never actively used under this booking.Valid from: REQUESTED, APPROVED, ASSIGNEDAvailability is released if a RESERVED record existed (i.e., if cancelled from APPROVED or ASSIGNED).

TERMINATED

The booking became operational (reached ACTIVE) but ended before its planned endDate. The vehicle was handed over and in active use.Valid from: ACTIVE onlyA BookingTermination record is always created. Certain termination reasons (e.g., ACCIDENT, VEHICLE_BREAKDOWN) may additionally trigger a maintenance block.

BookingTermination Record

When a booking is terminated, a separate BookingTermination entity is persisted to record the operational details of the early closure.
FieldTypeDescription
iduuidUnique termination record identifier
bookingIduuidThe booking that was terminated
reasonTerminationReasonEnumerated reason for termination
terminatedByuuidUser ID who performed the termination
terminatedAttimestamptzExact timestamp of termination
notesstring | nullOptional free-text notes

TerminateBookingDto

class TerminateBookingDto {
  @IsEnum(TerminationReason)
  reason: TerminationReason;   // Required — must be a valid enum value

  @IsUUID()
  terminatedBy: string;        // Required — ID of the user terminating the booking

  @IsOptional()
  @IsString()
  notes?: string;              // Optional — additional context
}

Termination Reasons

ReasonDescription
VEHICLE_BREAKDOWNVehicle became inoperable during the booking period
ACCIDENTVehicle was involved in an accident
VENDOR_RECALLVendor recalled the vehicle
CORPORATE_RECALLCorporate organization recalled the vehicle
EMPLOYEE_EXITAssigned employee left the organization
COMPLIANCE_VIOLATIONA compliance rule was breached
INSURANCE_EXPIREDVehicle insurance lapsed during the booking
ADMIN_ACTIONPlatform administrator forced termination
OTHERAny other reason not covered above
New termination reasons can be added to the TerminationReason enum in future phases without changing the state machine or booking lifecycle logic.

Domain Events

Every state transition emits a domain event via EventEmitter2. Downstream domains subscribe to these events without being coupled to the Booking domain.
EventEmitted OnKey Consumers
BookingRequestedEventDRAFT → REQUESTEDNotifications, Audit, Admin Dashboard
BookingApprovedEventREQUESTED → APPROVEDAvailability Domain, Notifications, Audit
BookingRejectedEventREQUESTED → REJECTEDNotifications, Audit
BookingAssignedEventAPPROVED → ASSIGNEDAssignment Domain, Notifications, Audit
BookingActivatedEventASSIGNED → ACTIVEFleet Operations, Analytics, Audit
BookingCompletedEventACTIVE → COMPLETEDAvailability Domain, Analytics, Audit
BookingTerminatedEventACTIVE → TERMINATEDAvailability Domain, Assignment Domain, Notifications, Audit
BookingCancelledEvent* → CANCELLEDAvailability Domain, Notifications, Audit

Availability Integration

The Booking domain never directly modifies availability records — it emits events and the Availability domain reacts.
Booking TransitionAvailability Effect
REQUESTEDNo change — vehicle stays available
APPROVEDRESERVED availability record created for the booking period
ASSIGNEDAvailability status transitions RESERVED → ALLOCATED
COMPLETEDAvailability record released; vehicle re-enters searchable pool
CANCELLED (from APPROVED/ASSIGNED)RESERVED / ALLOCATED record released
TERMINATEDAvailability record released (maintenance block may be added separately)

Assignment Integration

A booking and its assigned employee are not directly linked. The correct model is:
Booking → Assignment → Employee
This separation — enforced by emitting BookingAssignedEvent which the Assignment domain consumes — enables future reassignments, temporary allocations, and pool vehicle scenarios without modifying booking logic. See the Assignments guide for full details.

Build docs developers (and LLMs) love