The booking-service
is a microservice within the Flight Booking Application, responsible for handling flight bookings, managing payments, and ensuring seat availability. It is built using Node.js and Express.js, with MySQL as the database and Sequelize as the ORM.
- Express.js server for handling API requests
- Sequelize ORM for database interactions
- MySQL database for storing booking details
- Booking management with status updates
- Payment processing with idempotency key handling
- Seat availability validation to prevent overbooking
- Automated cleanup of unpaid bookings using cron jobs
- Logging with Winston for debugging and monitoring
Ensure you have a .env
file with the following variables:
PORT=4000
FLIGHT_SERVICE='http://localhost:3500'
- Node.js installed on your machine
- MySQL database running
- Clone the repository:
git clone <https://github.com/faridomarAf/flight-booking-service.git> cd booking-service
- Install dependencies:
npm install
- Initialize Sequelize:
npx sequelize init
- Create the
Booking
model:npx sequelize model:generate --name Booking --attributes flightId:integer,userId:integer,status:enum,noOfSeats:integer,totalCost:integer
- Run database migrations:
npx sequelize db:migrate
To start the server in development mode:
npm run dev
- Users can book a flight by providing flight details, number of seats, and payment confirmation.
- Seat availability is validated before confirming a booking.
- If a booking is canceled, the reserved seats are returned to the available seats in the flight.
- A function
cancelBooking
is implemented to handle this logic.
- If a booking is initiated but not paid within 5 minutes, it is automatically canceled.
- To achieve this,
node-cron
is used to schedule a task that checks and cancels such bookings every 5 minutes. - Install
node-cron
:npm install node-cron
- An idempotent key ensures that a payment request is processed only once, even if sent multiple times.
- A temporary in-memory object is used to store
idempotencyKey
in thepayment-controller
. - A better approach is to use Redis or a database table for storing
idempotencyKey
.
express
- Web framework for Node.jsdotenv
- Environment variable managementwinston
- Logging libraryhttp-status-codes
- Standard HTTP status codesmysql2
- MySQL database driversequelize
- ORM for MySQLsequelize-cli
- CLI tool for managing Sequelizenode-cron
- Task scheduling for automatic cleanupaxios
- HTTP client for external API calls
Farid