ignite-gym

Project built during Rocketseat's Ignite

MIT License

Stars
2

Ignite Gym

Allow users to register themselves as admin or members, allow admin users to create gyms and validate check-ins. Allow members to check-in, get their profile, search/fetch gyms. and much more! The app has pagination and use JWT to authentication.

Table of Contents

Installing

Easy peasy lemon squeezy:

$ yarn

Or:

$ npm install

Was installed and configured the eslint and prettier to keep the code clean and patterned.

Configuring

The application uses just one database: Postgres.

Postgres

Responsible to store all application data. If for any reason you would like to create a Postgres container instead of use docker-compose, you can do it by running the following command:

$ docker run --name ignite-gym-postgres -e POSTGRES_PASSWORD=docker -p 5432:5432 -d postgres

Migrations

Remember to run the database migrations:

$ npx prisma migrate dev

See more information on Prisma Migrate.

.env

In this file you may configure your JWT settings, the environment, app's port and database connection Url. Rename the .env.example in the root directory to .env then just update with your settings.

key description default
PORT Port number where the app will run. 3333
NODE_ENV App environment. dev
JWT_SECRET A alphanumeric random string. Used to create signed tokens. -
DATABASE_URL Database connection Url. postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public

Usage

To start up the app run:

$ yarn dev:server

Or:

npm run dev:server

Bearer Token

A few routes expect a Bearer Token in an Authorization header.

You can see these routes in the routes section.

POST http://localhost:3333/gyms Authorization: Bearer <token>

Get your token after authenticate through the /sessions route, it returns a token key with a Bearer Token.

Routes

route HTTP Method params description auth method
/users POST Body with user name, email and password. Create new user.
/sessions POST Body with user email and password. Authenticates user and return a Bearer Token.
/token/refresh PATCH Cookie with the refreshToken generate in the sign in (/sessions). Generate a new Bearer Token.
/me GET - Get the logged in user profile. Bearer
/gyms POST Body with gym name, description, phone, latitude and longitude. Create a new gym. Only ADMIN users are allowed. Bearer
/gyms/nearby GET Query parameters with user current latitude and longitude. Look for gyms nearby of the user. Bearer
/gyms/search GET Query parameters with q (query to use in the search) and optionally page. Search for gyms based in the query provided. Bearer
/gyms/:gymId/check-ins POST Body with user current latitude and longitude. Check in user on a gym. Bearer
/check-ins/:checkInId/validate PATCH - Validate an user check-in. Only ADMIN users are allowed. Bearer
/check-ins/history GET - Return user's check-ins. Bearer
/check-ins/metrics GET - Return user's check-ins count. Bearer

Routes with Bearer as auth method expect an Authorization header. See Bearer Token section for more information.

Requests

  • POST /users

Request body:

{
	"name": "John Doe",
	"email": "[email protected]",
	"password": "123456"
}
  • POST /sessions

Request body:

{
	"email": "[email protected]",
	"password": "123456"
}
  • POST /gyms

Request body:

{
	"name": "Ignite Gym",
	"description": "Gym for Ignite Students",
	"phone": "8772810274",
	"latitude": "79.1721",
	"longitude": "43.0377"
}
  • POST /gyms/:gymId/check-ins

Request body:

{
	"latitude": "79.1721",
	"longitude": "43.0377"
}

Running the tests

Vitest was the choice to test the app, to run the unit tests:

$ yarn test:unit

Or:

$ npm run test:unit

And to run the E2E tests:

$ yarn test:e2e

Or:

$ npm run test:e2e

Coverage report

You can see the coverage report inside tests/coverage. They are automatically created after the tests run.