This is just a simple Express API (NodeJS) that sends saved surveys to registered users' email and calculates NPS (Net Promoter Score) based on received users' survey answers. It's a project made during NLW#4 event promoted by 🚀 Rocketseat on their NodeJS path. Learn more about NPS Score
The purpose of my participation on this event was of just curiosity. I've been told how great Rocketseat was, so I thought that I should get to know them better as well and see wether I'd learn something new. I liked those new (for me) libraries they chose to work on this project.
This API can register user's e-mail, save survey's questions, re/send an e-mail of any desired survey to any user's e-mail, receive a user's answer with a score in a range from 1 to 10. I did add just a few fixes for original code and despite the fact they were so simple IMHO I think those should have been addressed during their presentation.
Clone the repository
git clone https://github.com/trepichio/nps_api.git
Follow these instructions once you're at project's root path in your favorite terminal.
Install dependencies
npm install
Rename .env.example file to .env
mv .env.example .env
by default this web server will run at port 3399. If you want to run in a different port just open the .env file and change both the PORT variable and port value in URL_MAIL and save it.
run the migrations to create database and its tables
npm run typeorm -- migration:run
and then run web server
npm run dev
Then you can consume one of its endpoints using your favourite REST API tool such as Postman, Insomnia or any other.
For example: (GET) http://localhost:3399/surveys/ to retrieve all surveys from database or (POST) http://localhost:3399/sendMail/ to send a saved survey to a registered user email, both parameters must be provided in the body of this request. More details about in section Usage.
You can run tests by
npm run test
Some tests are already coded that tests things such as if it can create users, avoid create a new user with an already existent email, can get a list of surveys and create new surveys.
Consume one of its endpoints using your favourite REST API tool such as Postman, Insomnia or any other.
-
POST /users
- Register a new user email
- Parameters to pass into JSON request body: { name, email }
-
GET /surveys
- Retrieve all saved surveys
-
POST /surveys
- Save a new survey into database
- Parameters to pass into JSON request body: { title, description }
- Use title for the subject of email to send
- Use description for the question
-
POST /sendMail
- Send an e-mail with a selected survey to a user's e-mail
- Parameters to pass into JSON request body: { email, survey_id }
- Use email for a registered user email
- Use survey_id to select which survey send to
-
GET /answers/:value?u=
- This url will be in the score links in sent email. Used for update ONCE user's answer to a specific survey contained in their sent email.
- value is the chosen score. A number from 1 to 10.
- Parameter to pass into request query: u
- Use u to pass surveyUser_id to identify which survey this user answer belongs to.
-
GET /nps/:survey_id
- Retrieve a calculation of NPS - Net Promoter Score of the specified survey and its related data.
- Parameter to pass into url path: survey_id
- Use survey_id to identify which survey to retrieve NPS data.
- Typescript - a Superset of Javascript that adds types check and more.
- SQLite - Database
- Typeorm - ORM for TypeScript and JavaScript.
- better-sqlite3 - The fastest and simplest library for SQLite3 in Node.js.
- Express - Server Framework
- NodeJs - Server Environment
- express-async-errors - Async Error Handling Middleware for Express
- Jest - Testing Framework
- Handlebars - Minimal templating on steroids
- Nodemailer - a module for Node.js applications to allow easy as cake email sending.
- Yup - Dead simple Object schema validation
- Supertest - Super-agent driven library for testing node.js HTTP servers
João Trepichio |
🔥 GitHub |
- References: