Denly / JapanJobTest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About


Languages

Language:TypeScript 90.0%Language:JavaScript 7.1%Language:Makefile 2.9%