Skip to content

JuniorCarrillo/mobility-api

Repository files navigation

mobility-api v0.0.1

Nestjs GitHub tag Sourcegraph License JuniorCarrillo - mobility-api stars - mobility-api forks - mobility-api OS - macOS OS - Linux Made with Docker

This is an Uber-like API (for mobility) developed in TypeScript with Nestjs, using TypeORM with PostgreSQL for data persistence. It integrates a demo payment gateway and allows cost calculation for a linear service by calculating distance, time, and adding a base fee. It uses native Axios within Nestjs to consume data.

This API was developed to be used with a PostgreSQL instance in Docker, so having Docker along with Node and Yarn are the prerequisites.

This project was developed on MacOS

Install

  1. Clone the repository
git clone https://github.com/JuniorCarrillo/mobility-api.git
  1. Copy environments
cd mobility-api && cp .env.example .env
  1. Install dependencies (require Docker)
docker compose up -d && yarn make
  1. Run application
yarn start:dev
  1. Running on port 3000
open http://localhost:3000/docs

Use

Here are listed the available endpoints or routes within the REST service. It should be noted that you can expand this information by accessing the Swagger generated from the same API at the /docs endpoint, since security middleware, JWT, and route control by roles are implemented.

POST /auth/driver/register (Register driver in POST method)

Request example:

curl --location 'http://localhost:3000/auth/driver/register' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
  "name": "Driver",
  "password": "12345678",
  "email": "driver@app.co"
}'

Response example:

{
    "name": "Driver",
    "email": "driver@app.co",
    "role": "DRIVER",
    "id": 1,
    "createAt": "2023-04-15T21:01:17.399Z",
    "updateAt": "2023-04-15T21:01:17.399Z"
}

POST /auth/rider/register (Rider driver in POST method)

Request example:

curl --location 'http://localhost:3000/auth/rider/register' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
  "name": "Rider",
  "password": "12345678",
  "email": "rider@app.co"
}'

Response example:

{
    "name": "Rider",
    "email": "rider@app.co",
    "role": "RIDER",
    "id": 2,
    "createAt": "2023-04-15T21:01:58.395Z",
    "updateAt": "2023-04-15T21:01:58.395Z"
}

POST /auth/login (Login in POST method)

Request example:

curl --location 'http://localhost:3000/auth/login' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
    "password": "12345678",
    "email": "driver@app.co"
}'

Response example for rider:

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiUklERVIiLCJzdWIiOjIsImlhdCI6MTY4MTU5MjU1MCwiZXhwIjoxNzEzMTUwMTUwfQ.QSXE4lz4D0IfmKdJNWjofOPWtvekvR5ybajO_RJiMH4",
    "user": {
        "id": 2,
        "name": "Rider",
        "email": "rider@app.co",
        "role": "RIDER",
        "createAt": "2023-04-15T21:01:58.395Z",
        "updateAt": "2023-04-15T21:01:58.395Z"
    }
}

Response example for driver:

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiRFJJVkVSIiwic3ViIjoxLCJpYXQiOjE2ODE1OTI1ODMsImV4cCI6MTcxMzE1MDE4M30.umwXqvUNz2Ft48bKqIEwNC1RLGH2dkxWyP28GAVPNg0",
    "user": {
        "id": 1,
        "name": "Driver",
        "email": "driver@app.co",
        "role": "DRIVER",
        "createAt": "2023-04-15T21:01:17.399Z",
        "updateAt": "2023-04-15T21:01:17.399Z"
    }
}

POST /user/card (Add new card to user)

Request example:

curl --location 'http://localhost:3000/user/card' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiUklERVIiLCJzdWIiOjIsImlhdCI6MTY4MTU5MjU1MCwiZXhwIjoxNzEzMTUwMTUwfQ.QSXE4lz4D0IfmKdJNWjofOPWtvekvR5ybajO_RJiMH4' \
--data '{
    "number": "4242424242424242",
    "cvc": "123",
    "exp_month": "08",
    "exp_year": "28",
    "card_holder": "José Pérez"
}'

Response example:

{
    "token": "tok_test_14154_3461ef5827d0C716Ec16f1649b67b3bA",
    "brand": "VISA",
    "last_four": "4242",
    "exp_year": "28",
    "exp_month": "08",
    "id": 1,
    "createAt": "2023-04-15T21:03:38.134Z",
    "updateAt": "2023-04-15T21:03:38.134Z"
}

POST /rider (Request a ride by user rider)

Request example:

curl --location 'http://localhost:3000/rider' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiUklERVIiLCJzdWIiOjIsImlhdCI6MTY4MTU5MjU1MCwiZXhwIjoxNzEzMTUwMTUwfQ.QSXE4lz4D0IfmKdJNWjofOPWtvekvR5ybajO_RJiMH4' \
--data '{
  "location": [6.227613, -75.5799254],
  "destination": [6.1664595, -75.625661],
  "installments": 5
}'

Response example:

{
    "location": [
        6.227613,
        -75.5799254
    ],
    "destination": [
        6.1664595,
        -75.625661
    ],
    "installments": 5,
    "rider": {
        "id": 2,
        "name": "Rider",
        "email": "rider@app.co",
        "role": "RIDER",
        "createAt": "2023-04-15T21:01:58.395Z",
        "updateAt": "2023-04-15T21:01:58.395Z"
    },
    "driver": {
        "id": 1,
        "name": "Driver",
        "email": "driver@app.co",
        "role": "DRIVER",
        "createAt": "2023-04-15T21:01:17.399Z",
        "updateAt": "2023-04-15T21:01:17.399Z"
    },
    "card": {
        "id": 1,
        "token": "tok_test_14154_3461ef5827d0C716Ec16f1649b67b3bA",
        "brand": "VISA",
        "last_four": "4242",
        "exp_year": "28",
        "exp_month": "08",
        "createAt": "2023-04-15T21:03:38.134Z",
        "updateAt": "2023-04-15T21:03:38.134Z"
    },
    "id": 1,
    "status": "REQUEST",
    "finishAt": "2023-04-15T21:04:37.493Z",
    "createAt": "2023-04-15T21:04:37.493Z",
    "updateAt": "2023-04-15T21:04:37.493Z"
}

POST /driver/finish/:id (Finish ride by user driver)

Request example:

curl --location --request POST 'http://localhost:3000/driver/finish/1' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiRFJJVkVSIiwic3ViIjoxLCJpYXQiOjE2ODE1OTI1ODMsImV4cCI6MTcxMzE1MDE4M30.umwXqvUNz2Ft48bKqIEwNC1RLGH2dkxWyP28GAVPNg0'

Response example:

{
    "id": 1,
    "location": [
        6.227613,
        -75.5799254
    ],
    "destination": [
        6.1664595,
        -75.625661
    ],
    "status": "COMPLETE",
    "installments": 5,
    "finishAt": "2023-04-15T21:05:20.454Z",
    "createAt": "2023-04-15T21:04:37.493Z",
    "updateAt": "2023-04-15T21:05:20.462Z",
    "driver": {
        "id": 1,
        "name": "Driver",
        "email": "driver@app.co",
        "role": "DRIVER",
        "createAt": "2023-04-15T21:01:17.399Z",
        "updateAt": "2023-04-15T21:01:17.399Z"
    },
    "rider": {
        "id": 2,
        "name": "Rider",
        "email": "rider@app.co",
        "role": "RIDER",
        "createAt": "2023-04-15T21:01:58.395Z",
        "updateAt": "2023-04-15T21:01:58.395Z"
    },
    "card": {
        "id": 1,
        "token": "tok_test_14154_3461ef5827d0C716Ec16f1649b67b3bA",
        "brand": "VISA",
        "last_four": "4242",
        "exp_year": "28",
        "exp_month": "08",
        "createAt": "2023-04-15T21:03:38.134Z",
        "updateAt": "2023-04-15T21:03:38.134Z"
    }
}

Contribution

If you want to contribute to this project, please follow these steps:

  1. Fork the repository
  2. Create a branch (git checkout -b feature/new-feature)
  3. Make the necessary changes and commit (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/new-feature)
  5. Open a pull request

License

MIT License © Junior Carrillo

Contact us