Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Hotel Reservation System

This project is a Hotel Reservation System API built using NestJS, GraphQL, and Mongoose. It provides functionality for managing hotel reservations, including creating, retrieving, and cancelling reservations. It also supports JWT-based authentication and error handling.

Installation

$ pnpm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

Features

  • JWT-based authentication
  • CRUD operations for reservations
  • Pagination and filtering
  • DataLoader for efficient data fetching
  • GraphQL API
  • Error handling and validation

Requirements

  • Node.js (version 14 or higher)
  • MongoDB

Enviroment Variables

Create a .env file and configure the following variables

  • MONGO_URI=
  • JWT_SECRET=

Documentation

To check complete documentation of the GraphQL endpoints visit API Reference or use

  • /api-reference endpoint for Documentation of Project
  • /graphql-root for graphQL inspection

Test Examples to execute queries and mutations on graphql playground

To get all reservations of the user, you need to follow these steps

  • Sign up using query signup
  • Sign in and get token to access all protected queries and mutation
  • Pass the Authentication token in headers as { Authorization: Bearer <YOUR_TOKEN>}
  • Use variables that need to pass to the query.
  • Below are the examples.

User Signin

query Query($userInput: LoginUserDto!) {
  signIn(userInput: $userInput) {
    accessToken
  }
}

/* Variables */
{
  "userInput": {
    "email": "test@test.com",
    "password": "tester"
  }
}
/* Headers */
{ Authorization: Bearer <accessToken> }

User Signup

mutation Mutation($userInput: CreateUserDto!) {
  signUp(userInput: $userInput) {
    id
    name
    email
    phoneNumber
  }
}

/* Variables */
{
  "userInput": {
    "email": "test5@test.com",
    "name": "Test 5 User",
    "password": "tester",
    "phoneNumber": "1-212-456-7890"
  }
}
/* Headers */
{ Authorization: Bearer <accessToken> }

Get all hotels

query GetHotels {
  getHotels {
    _id
    name
    location {
      lat
      long
    }
    rating
    baseAmount
    taxAmount
  }
}

/* Headers */
{ Authorization: Bearer <accessToken> }

Create Reservation

mutation CreateReservation($createReservationDto: CreateReservationDto!) {
  createReservation(createReservationDto: $createReservationDto) {
    arrivalDate
    departureDate
    _id
    hotelId {
      _id
      name
    }
    userId {
      _id
      name
    }
    amount
    status
  }
}

/* Variables */
{
  "createReservationDto": {
    "arrivalDate": "2024-06-28T08:41:41.143Z",
    "departureDate": "2024-06-29T08:41:41.143Z",
    "hotelId": "666d7ec4319cadc293131d76"
  }
}
/* Headers */
{ Authorization: Bearer <accessToken> }

Get All Reservations [All not for specific user]

query Query($paginationDto: PaginationDto!) {
  getReservations(paginationDto: $paginationDto) {
    edges {
      cursor
      node {
        _id
        amount
        arrivalDate
        departureDate
        status
        userId {
          name
          _id
        }
        hotelId {
          _id
          name
        }
      }
    }
  }
}

/* Variables */
{
  "paginationDto": {
    "cursor": null,
    "limit": 20
  }
}
/* Headers */
{ Authorization: Bearer <accessToken> }

Get Reservation by ID

query GetReservation($id: String!) {
  getReservation(_id: $id) {
    arrivalDate
    departureDate
    _id
    hotelId {
      _id
      name
    }
    userId {
      _id
      name
    }
    amount
    status
  }
}
/* Variables */
{
  "id": "66729c85228a9875dce161d0"
}
/* Headers */
{ Authorization: Bearer <accessToken> }

Cancel Reservation

mutation Mutation($id: String!) {
  cancelReservation(_id: $id)
}

/* Variables */
{
  "id": "666da4096b3835179380424d"
}
/* Headers */
{ Authorization: Bearer <accessToken> }

Update Reservation

mutation UpdateReservation($id: String!, $updateReservationDto: UpdateReservationDto!) {
  updateReservation(_id: $id, updateReservationDto: $updateReservationDto) {
    arrivalDate
    departureDate
    _id
    hotelId {
      _id
      name
    }
    userId {
      _id
      name
    }
    amount
    status
  }
}

/* Variables */
{
  "updateReservationDto": {
    "arrivalDate": "2024-07-10T08:41:41.143Z",
    "departureDate": "2024-07-11T08:41:41.143Z",
    "amount": 1200
  },
  "id": "66729c85228a9875dce161d0"
}
/* Headers */
{ Authorization: Bearer <accessToken> }

Get Guest Stay Summary

query GuestSummary {
  guestSummary {
    guestId
    upcomingStaysCount
    upComingTotalAmount
    upComingTotalNights
    pastStaysCount
    pastTotalAmount
    pastTotalNights
    cancelledStaysCount
    totalStaysAmount
  }
}

/* Headers */
{ Authorization: Bearer <accessToken> }

Get Stays In Range (Start to End Date)

query GetStaysInRange($getStaysArgs: GetStaysDto!) {
  getStaysInRange(getStaysArgs: $getStaysArgs) {
    nextCursor
    edges {
      cursor
      node {
        arrivalDate
        departureDate
        _id
        hotelId {
          _id
          name
        }
        userId {
          _id
          name
        }
        amount
        status
      }
    }
  }
}

/* Variables */
{
  "getStaysArgs": {
    "cursor": null,
    "limit": 20,
    "startDate": "2024-05-10T08:41:41.143Z",
    "endDate": "2024-07-10T08:41:41.143Z"
  }
}
/* Headers */
{ Authorization: Bearer <accessToken> }

Deployment

To run complete application here is the CodeSanbox URL

Error Handling

Errors are handled using a custom GraphQL exception filter. Errors are serialized in a consistent format for better readability.

Testing

To run the unit tests, use the following command:

$ npm run test

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

License

Nest is MIT licensed.