- Introduction
- Exercise
- Project description
- Prerequisites
- Installation
- Testing
- Database and migrations
- Troubleshooting
- Further reading
Introduction
This is a software development exercise for a TypeScript / Node.js backend developer position.
Exercise
Your task
Your task is to
-
Add a new field
phoneNumber
for users -
This field is required during the registration process
-
The field must save a normalised (i.e: no spaces or other separator characters) international phone number in the database. Technically, this is called MSISDN format. Please save the plus character prefix in the database. E.g. a valid column value would be
+358401231234
(Finland). -
You do not need to check if the phone number is legit - e.g. if the country code exists. This would be super difficult tasks as good heuristics do not exist.
-
Update e2e tests to cover different phone number input cases - the API must gracefully handle different valid and invalid inputs
-
Create necessary migrations to upgrade the production database
-
In the pull request describe the changes and the actions that your colleagues need to understand: API migration instructions for frontend developers, database migration instructions for a devop team
-
For the extra impression, you can add other recommendations in the pull request commenting section. However, any code and style changes, should go to its own separate pull request. The exercise pull request must consider only the task in the hand.
How to submit the exercise
- Create a private copy of this Github repository
- Complete the task above
- Create a new pull request against your private repository
- In the PR, write down number of hours you spent on this exercise (we do not use this to rank you, we use it to adjust the laborisity of future exercises)
- Invite a Github user
miohtama
to your repository - Send email to
dev-careers@fb.io
that you have completed the exercise
How you will be ranked
We will look
- If the instructions were properly followed
- If the task was correctly completed
- Code quality
- Code comment quality
- Pull request commenting quality
Project description
This exercise is based on NestJS framework TypeScript starter project.
The local development flow is
-
PostgreSQL databases running in a docker image: local development and integration testing databases. PostgreSQL docker binds non-default port 54320.
-
App and NestJS is installed locally, not inside Docker
-
The project uses NestJS OpenAPI (Swagger) plugin for automatic interface generation.
The development environment is tested on OSX, but should work on Linux systems unmodified. We do not recommend trying to undertake the exercise on native Windows.
Prerequisites
-
You need to understand UNIX shell, TypeScript, Node.js, Docker, PostgreSQL
-
This exercise was developed on Node v11.0.0
Installation
Setting up PostgreSQL database
This is will make a new PostgreSQL running in the standard port 5432. Please shutdown any previous conflicting PostgreSQL instances before starting this.
docker-compose up -d
Check it is up
docker logs -f local_db
Create an empty development database:
docker exec -it local_db psql -U local_dev -c "create database local_db" -d template1
Check that you can log into a database with psql
docker exec -it local_db psql -U local_dev local_db
Setting up a local app
Then do the local app installation
npm install
Generata a dist
folder (this will transpile .ts migrations to .js)
npm run build
Run initial migrations to set up initial database tables
npm run migration:run
Running the app
Development
npm run start
Then visit http://localhost:3000 to get the app landing page. There is nothing to see there.
Visit http://localhost:3000/api/ to get the Swagger generated REST API tool. which you can use against a hot reloading dev server.
To run the dev server in watch mode:
npm run start:dev
Manual usage using Swagger
-
Post a registration request to
/register
-
Confirm your email using
/users/confirm-email-admin
Testing
Please note that the server logger is not muted during these tests, so you get API errors logged in the console.
Create the testing database (needs to be done only once).
docker exec -it local_db psql -U local_dev -c "create database e2e_test" local_db
Run jest:
npm run test:e2e
We have only integration tests. No unit tests are available, or asked, in this exercise.
Database and migrations
Automatically generating migrations
You can generate migration files
-
After updating entity source code
-
You have an up-to-date local development database
# Make sure we have not stale JS transpilation code around
rm -rf dist
# Rebuild transpilation
npm run build
# You need to start the dev server to generate dist/migrations
# NestJS bug https://github.com/nrwl/nx/issues/1393
npm run start
# Create a file under migration/
npm run migration:generate -- -n CreateUsers
Run migrations against a local db
npm run migration:run
Check the result of migrations
docker exec -it local_db psql -U local_dev -c "\dt" local_db
Troubleshooting
Restart database from the scratch
Nuking the local development database:
docker-compose down -v
Display database content
See what lurks in the user table:
docker exec -it local_db psql -U local_dev -c "select * from site_user" local_db
Debugging from Visual Studio Code
Visual Studio Code Auto [Attach feature] works.
You just need to run
npm run start:debug
... and Visual Studio Code will notice this and the debugger bar appears. All breakpoints are honoured.
Launching Jest from Visual Studio Code
Example launcher how to attach a debugger to Jest tests.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--runInBand",
"--config",
"./test/jest-e2e.json"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
},
"env": {
"PATH": "/Users/moo/.nvm/versions/node/v11.0.0/bin:${env:PATH}"
},
},
]
}
Further reading
NestJS and TypeORM in 30 minutes
Another NestJS and TypeORM tutorial
Cats NestJS + Swagger sample full example code
Testing database interaction with TypeORM and related source code