Giter Site home page Giter Site logo

diegovictor / npser Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 2.0 630 KB

Project built during Rocketseat's Next Level Week #4

License: MIT License

JavaScript 0.14% TypeScript 96.44% Handlebars 3.12% Dockerfile 0.30%
nodejs api node typescript ts eslint sqlite typeorm prettier editorconfig

npser's Introduction

NPSer

AppVeyor 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

Or:

$ yarn typeorm migration:run

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": "[email protected]"
}
  • POST /survey

Request body:

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

Request body:

{
  "email": "[email protected]",
  "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.

npser's People

Contributors

diegovictor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.