/JapanJobTest

Primary LanguageTypeScript

KrakenFlex Back End Test

๐Ÿš€ Quick Start

  1. Create a file named ".env.development.local" with the following text as env var
# PORT
PORT = 3000

# TOKEN
SECRET_KEY = secretKey
API_KEY = myApiKey
KRAKEN_API_KEY = EltgJ5G8m44IzwE6UN2Y4B4NjPW77Zk6FJK3lL23

# LOG
LOG_FORMAT = dev
LOG_DIR = ../logs

# CORS
ORIGIN = *
CREDENTIALS = true
  1. npm install and npm run dev This will start with the env var that has our API_KEY and KRAKEN_API_KEY (Yes they should not be in the repo but this is test)

  2. Go to /src/http/report.http and use REST Client in VS Code to interact with the API, that is POST {{ baseURL }}/report/norwich-pear-tree the API_KEY=myApiKey is already attached. (Or call the api by any tool you like)

๐Ÿ—‚ Code Structure

The files with arrows bellow are the important ones

โ”‚
โ”œโ”€โ”€๐Ÿ“‚ .vscode
โ”‚  โ”œโ”€โ”€ launch.json
โ”‚  โ””โ”€โ”€ settings.json
โ”‚
โ”œโ”€โ”€๐Ÿ“‚ src
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ config
โ”‚  โ”‚  โ””โ”€โ”€ index.ts                  <-- remember to config env var api keys if not in dev
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ controllers
|  |  โ”œโ”€โ”€ report.controller.ts      <-- report api (the main task in the test)
|  |  โ”œโ”€โ”€ krakenflex.controller.ts  <-- all krakenflex api 
โ”‚  โ”‚  โ”œโ”€โ”€ auth.controller.ts        // ignore auth
โ”‚  โ”‚  โ””โ”€โ”€ users.controller.ts       // ignore users
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ dtos
โ”‚  โ”‚  โ””โ”€โ”€ users.dto.ts
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ exceptions
โ”‚  โ”‚  โ””โ”€โ”€ httpException.ts
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ http
โ”‚  โ”‚  โ”œโ”€โ”€ auth.http
โ”‚  โ”‚  โ””โ”€โ”€ users.http
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ interfaces
โ”‚  โ”‚  โ”œโ”€โ”€ event.interface.ts       <-- define event type
โ”‚  โ”‚  โ”œโ”€โ”€ siteInfo.interface.ts    <-- define siteInfo type
โ”‚  โ”‚  โ”œโ”€โ”€ apiResponse.interface.ts <-- define apiResponse type
โ”‚  โ”‚  โ”œโ”€โ”€ auth.interface.ts
โ”‚  โ”‚  โ”œโ”€โ”€ routes.interface.ts
โ”‚  โ”‚  โ””โ”€โ”€ users.interface.ts
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ middlewares
โ”‚  โ”‚  โ”œโ”€โ”€ validApiKey.middleware.ts <-- validApiKey middleware
โ”‚  โ”‚  โ”œโ”€โ”€ auth.middleware.ts
โ”‚  โ”‚  โ”œโ”€โ”€ error.middleware.ts
โ”‚  โ”‚  โ””โ”€โ”€ validation.middleware.ts
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ models
โ”‚  โ”‚  โ””โ”€โ”€ users.model.ts
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ routes
โ”‚  โ”‚  โ”œโ”€โ”€ report.route.ts     <-- report route
โ”‚  โ”‚  โ”œโ”€โ”€ krakenflex.route.ts <-- krakenflex api route
โ”‚  โ”‚  โ”œโ”€โ”€ auth.route.ts
โ”‚  โ”‚  โ””โ”€โ”€ users.route.ts
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ services
โ”‚  โ”‚  โ”œโ”€โ”€ report.service.ts     <-- filter and report logic
โ”‚  โ”‚  โ”œโ”€โ”€ krakenflex.service.ts <-- service to call krakenflex api
โ”‚  โ”‚  โ”œโ”€โ”€ auth.service.ts
โ”‚  โ”‚  โ””โ”€โ”€ users.service.ts
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ test
โ”‚  โ”‚  โ””โ”€โ”€ report.test.ts       <- report test 
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€๐Ÿ“‚ utils
โ”‚  โ”‚  โ”œโ”€โ”€ logger.ts
โ”‚  โ”‚  โ””โ”€โ”€ vaildateEnv.ts
โ”‚  โ”‚
โ”‚  โ”œโ”€โ”€ app.ts
โ”‚  โ””โ”€โ”€ server.ts
โ”‚
โ”œโ”€โ”€ .dockerignore
โ”œโ”€โ”€ .editorconfig
โ”œโ”€โ”€ .env.development.local
โ”œโ”€โ”€ .env.production.local
โ”œโ”€โ”€ .env.test.local
โ”œโ”€โ”€ .eslintignore
โ”œโ”€โ”€ .eslintrc
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ .huskyrc
โ”œโ”€โ”€ .lintstagedrc.json
โ”œโ”€โ”€ .prettierrc
โ”œโ”€โ”€ .swcrc
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ Dockerfile.dev
โ”œโ”€โ”€ Dockerfile.prod
โ”œโ”€โ”€ ecosystem.config.js
โ”œโ”€โ”€ jest.config.js
โ”œโ”€โ”€ Makefile
โ”œโ”€โ”€ nginx.conf
โ”œโ”€โ”€ nodemon.json
โ”œโ”€โ”€ package-lock.json
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ swagger.yaml
โ””โ”€โ”€ tsconfig.json

โ˜Ž๏ธ API

It has all the kraken api working in swagger

  1. GET /outages which returns all outages in our system
  2. GET /site-info/{siteId} which returns specific information about a site
  3. POST /site-outages/{siteId} which expects outages for a specific site to be posted to it

And it's own API working in report.http

  1. POST /report/{siteId} Given a siteId, report the relevant outages events to POST /site-outages/{siteId}

๐Ÿงช Test

run npm test to test or try it in report.http

๐Ÿฅก Models

Site:
siteId: readable name, e.g  "id: norwich-pear-tree"

A siteId has multiple deviceId

Device:
id (deviceId): hex code of devices, e.g "id": "002b28fc-283c-47ec-9af2-ea287336dc1b", 
name (deviceName): readable name, e.g "name": "Battery 1"

A deviceId has mulitple outages events with begin and end timestamp

Event:
end: timestamp string
begin: timestamp string
id (deviceId)

๐Ÿ›Ž Available Commands for the Server

  • Run the Server in production mode : npm run start
  • Run the Server in development mode : npm run dev with dev api keys
  • Run all unit-tests : npm test
  • Check for linting errors : npm run lint
  • Fix for linting : npm run lint:fix