From 3e7f729106f99539949f7831d4e58baee5e9e59d Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Tue, 25 Oct 2022 08:23:59 +0200 Subject: [PATCH] fix wrong enum values and validate the inputs in the endpoints --- src/inbound/dto/inbound.dto.ts | 2 +- src/inbound/inbound.controller.ts | 60 ++++++++++++++++++++++++++++ src/inbound/models/aircraft.model.ts | 2 +- src/inbound/models/inbound.model.ts | 2 +- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/inbound/dto/inbound.dto.ts b/src/inbound/dto/inbound.dto.ts index d30c91f..927defc 100644 --- a/src/inbound/dto/inbound.dto.ts +++ b/src/inbound/dto/inbound.dto.ts @@ -24,7 +24,7 @@ export class InboundDto { @IsNotEmpty() @ApiProperty({ description: - 'The reported controller level (possible: UNK, DEL, GRD, TWR, APP, DEP, CTR)', + 'The reported controller level (possible: UNK, DEL, GND, TWR, APP, DEP, CTR)', example: 'EDDB', }) @Length(3) diff --git a/src/inbound/inbound.controller.ts b/src/inbound/inbound.controller.ts index 0b039c3..9dff477 100644 --- a/src/inbound/inbound.controller.ts +++ b/src/inbound/inbound.controller.ts @@ -29,6 +29,11 @@ import { Inbound } from './models/inbound.model'; import { Planning } from './models/planning.model'; import { PredictedWaypoint } from './models/predictedwaypoint.model'; +const AllowedWtcs = ['L', 'M', 'H', 'J']; +const AllowedWakeRecats = ['A', 'B', 'C', 'D', 'E', 'F']; +const AllowedEngineTypes = ['ELECTRIC', 'TURBOPROP', 'JET']; +const AllowedReporterTypes = ['UNK', 'DEL', 'GND', 'TWR', 'APP', 'DEP', 'CTR']; + @Controller('inbound') export class InboundController { constructor( @@ -200,7 +205,62 @@ export class InboundController { status: 404, description: 'The destination airport is unknown', }) + @ApiResponse({ + status: 422, + description: 'The WTC is invalid', + }) + @ApiResponse({ + status: 422, + description: 'The wake recat is invalid', + }) + @ApiResponse({ + status: 422, + description: 'The engine type is invalid', + }) + @ApiResponse({ + status: 422, + description: 'The reporter is invalid', + }) async insert(inbound: InboundDto): Promise { + if ( + AllowedWtcs.find((element) => element === inbound.aircraft.wtc) === + undefined + ) { + throw new HttpException( + 'The WTC is invalid', + HttpStatus.UNPROCESSABLE_ENTITY, + ); + } + if ( + AllowedWakeRecats.find( + (element) => element === inbound.aircraft.wakeRecat, + ) === undefined + ) { + throw new HttpException( + 'The wake recat is invalid', + HttpStatus.UNPROCESSABLE_ENTITY, + ); + } + if ( + AllowedEngineTypes.find( + (element) => element === inbound.aircraft.engineType, + ) === undefined + ) { + throw new HttpException( + 'The engine type is invalid', + HttpStatus.UNPROCESSABLE_ENTITY, + ); + } + if ( + AllowedReporterTypes.find((element) => element === inbound.reporter) === + undefined + ) { + throw new HttpException( + 'The reporter type is invalid', + HttpStatus.UNPROCESSABLE_ENTITY, + ); + } + return this.airportService .airport(inbound.destination) .then(async (airport) => { diff --git a/src/inbound/models/aircraft.model.ts b/src/inbound/models/aircraft.model.ts index b9d2813..5305841 100644 --- a/src/inbound/models/aircraft.model.ts +++ b/src/inbound/models/aircraft.model.ts @@ -14,7 +14,7 @@ export class Aircraft { @Prop({ required: true, type: String, - enum: ['L', 'M', 'H', 'S'], + enum: ['L', 'M', 'H', 'J'], }) wtc: string; diff --git a/src/inbound/models/inbound.model.ts b/src/inbound/models/inbound.model.ts index b37ef14..11cf7ac 100644 --- a/src/inbound/models/inbound.model.ts +++ b/src/inbound/models/inbound.model.ts @@ -29,7 +29,7 @@ export class Inbound { @Prop({ required: true, type: String, - enum: ['UNK', 'DEL', 'GRD', 'TWR', 'APP', 'DEP', 'CTR'], + enum: ['UNK', 'DEL', 'GND', 'TWR', 'APP', 'DEP', 'CTR'], }) reporter: string;