/NPSer

Project built during Rocketseat's Next Level Week #4

Primary LanguageTypeScriptMIT LicenseMIT

NPSer

AppVeyor sqlite3 typescript eslint airbnb-style jest coverage MIT License PRs Welcome
Run in Insomnia}

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 use just one database: SQLite. For the fastest setup is recommended to use docker-compose, you just need to up all services:

$ docker-compose up -d

SQLite

Store all users and surveys. For more information to how to setup your database see:

You can find the application's ormconfig.json file in the root folder.

Migrations

Remember to run the database migrations:

$ yarn ts-node-dev ./node_modules/typeorm/cli.js migration:run -- -d ./src/database/datasource.ts

Or:

$ yarn typeorm migration:run -- -d ./src/database/datasource.ts

See more information on TypeORM Migrations.

.env

In this file you may configure app's port and a url to documentation (this will be returned with error responses, see error section). Rename the .env.example in the root directory to .env then just update with your settings.

key description default
URL_MAIL Url to send the NPS answer http://localhost:3333/answers
PORT Port number where the app will run. 3333
DOCS_URL An url to docs where users can find more information about the app's internal code errors. https://github.com/DiegoVictor/npser#errors-reference

Usage

To start up the app run:

$ yarn dev:server

Or:

npm run dev:server

Error Handling

Instead of only throw a simple message and HTTP Status Code this API return friendly errors:

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "User already exists",
  "code": 140,
  "docs": "https://github.com/DiegoVictor/npser#errors-reference"
}

Errors are implemented with @hapi/boom. As you can see a url to error docs are returned too. To configure this url update the DOCS_URL key from .env file. In the next sub section (Errors Reference) you can see the errors code description.

Errors Reference

code message description
140 User already exists The provided email is already registered by another user.
240 User does not exists The provided email was not found.
241 Survey does not exists The provided survey id does not references an existing registry in the database.
242 Answer not found A survey was not sent to this user.

Versioning

A simple versioning was made. Just remember to set after the host the /v1/ string to your requests.

GET http://localhost:3333/v1/surveys

Routes

route HTTP Method params description
/users POST Body with user name and email. Create a new user
/surveys GET page query parameter. Lists surveys.
/surveys POST Body with user title and description. Create a new survey
/send_mail POST Body with user email and a survey_id. Send the NPS to provided user
/answers GET page query parameter. List surveys' answers
/answers/:value GET survey value url parameter and survey user id query parameter. Set user's avaliation to one survey
/nps/:survey_id GET survey_id url parameter. Show survey NPS

Requests

  • POST /users

Request body:

{
  "name": "John Doe",
  "email": "johndoe@example.com"
}
  • POST /survey

Request body:

{
  "title": "Internal Directives Engineer",
  "description": "Cupiditate modi occaecati aut?"
}
  • POST /send_mail

Request body:

{
  "email": "johndoe@example.com",
  "survey_id": "388017f8-dfdf-4681-9112-e1bb0de009ec"
}

Running the tests

Jest was the choice to test the app, to run:

$ yarn test

Or:

$ npm run test

Coverage report

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