/DAF-task

Coding Task for DAF (Digital Access to Finance) company

Primary LanguageJavaScript

Intro

DAF backend coding task.

1️⃣ The applicaiton is unit and integration tested using jasmine
2️⃣ The application is built on top of NodeJs, Express, and PostgreSQL, I've not used any ORM, it's a pure raw sql. :three: Login/Signup are using JWT authentication

project structure

📦src
 ┣ 📂controllers
 ┃ ┣ 📜auth.controller.js
 ┃ ┣ 📜order.controller.js
 ┃ ┗ 📜product.controller.js
 ┣ 📂database
 ┃ ┗ 📜database.init.js
 ┣ 📂middlewares
 ┃ ┣ 📜auth.js
 ┃ ┗ 📜jwt.js
 ┣ 📂models
 ┃ ┣ 📜Order.model.js
 ┃ ┣ 📜Product.model.js
 ┃ ┗ 📜User.model.js
 ┣ 📂routes
 ┃ ┣ 📜auth.routes.js
 ┃ ┣ 📜order.routes.js
 ┃ ┣ 📜product.routes.js
 ┃ ┗ 📜user.routes.js
 ┣ 📂utils
 ┣ 📜app.js
 ┗ 📜index.js

DB Diagram

DB_SQL

DB_SQL

API endpoints

Auth

method endpoint action params
POST /auth/signup signup user -
POST /auth/login login user -

Order

method endpoint action params query params
POST /orders/create create new order - -
POST /orders/:orderId/products/:productId add product to an order orderId, productId -
GET /orders/users/:userId/ get user's orders userId -
PUT /orders/:orderId/ change order status orderId status

Product

method endpoint action params query params
POST /products/create create new product - -

Installing

Install the project dependencies

npm install

Setup and Connect to database

  1. creating user
CREATE USER daf_user WITH PASSWORD 'password123';
  1. create both development and testing db
CREATE DATABASE daf;
CREATE DATABASE daf_test;
  1. grant all privileges on both databases
\c daf;
GRANT ALL PRIVILEGES ON DATABASE DAF To daf_user;
\c daf_test 
GRANT ALL PRIVILEGES ON DATABASE DAF_test To daf_user;
  • setting enviroment variables
touch .env
nano .env

## place the enviroment variables below inside the .env file
# .env
APP_PORT=3000
POSTGRES_HOST=localhost
POSTGRES_USER=daf_user
POSTGRES_DB=daf
POSTGRES_PASSWORD=password123
POSTGRES_TEST_DB=daf_test
ENV=dev
BCRYPT_PASS=thisissupersecretpassword
SALT_ROUNDS=10
PEPPER=password
JWT_SECRET_KEY=thisissupersecretpassword
  • port of the posgres database server 5432
  • port of the backend: the server runs localhost

Running application

  • to run the application
npm run dev

Running Tests

To run tests, you need only two steps

  1. Export ENV to test
export ENV=test
  1. Run testing command, which will set the ENV to test if you've forgotten to do the first step, and will run the testing database migrations, then running the test suits
npm run test

Avaiable scripts

npm run migrate # running testing migrations
npm run jasmine # running jasmine for testing
npm run migrate-down # down the migrations
npm run dev # run the application

Tests passed ✅

Logs

  • 🚀 Signup
  • 🚀 Login
  • 🚀 accept/reject order
  • 🚀 create order
  • 🚀 get user order
  • 🚀 add product to order
  • 🚀 setup testing
    • Unit testing
    • Integration testing