All files / monitor monitor.controller.ts

100% Statements 22/22
100% Branches 2/2
100% Functions 5/5
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x       1x 1x     1x   9x     9x   1x   7x 6x     1x 1x     1x   3x 2x     1x 1x     1x   1x     1x   1x      
import {
  Controller, Get, HttpException, Logger, Post, Req
} from '@nestjs/common';
import { Request } from 'express';
import { isMonitor, Monitor } from './monitor.interface';
import { TestSessionService } from '../test-session/test-session.service';
 
@Controller()
export class MonitorController {
  constructor(
    private readonly dataService: TestSessionService
  ) {}
 
  private readonly logger = new Logger(MonitorController.name);
 
  @Post('/monitor/register')
  monitorRegister(@Req() request: Request): void {
    if (!isMonitor(request.body)) {
      throw new HttpException('not monitor data', 400);
    }
 
    this.logger.log(`monitor registered:${JSON.stringify(request.body)}`);
    this.dataService.addMonitor(request.body);
  }
 
  @Post('/monitor/unregister')
  monitorUnregister(@Req() request: Request): void {
    if (!('token' in request.body)) {
      throw new HttpException('no token in body', 400);
    }
 
    this.logger.log('monitor unregistered:', request.body);
    this.dataService.removeMonitor(request.body.token);
  }
 
  @Get('/monitors')
  monitors(@Req() request: Request): Monitor[] {
    return this.dataService.getMonitors();
  }
 
  @Get('/clients')
  clients(@Req() request: Request): string[] {
    return this.dataService.getClientTokens();
  }
}