fix wrong enum values and validate the inputs in the endpoints

This commit is contained in:
Sven Czarnian
2022-10-25 08:23:59 +02:00
parent 652b4aef7c
commit 3e7f729106
4 changed files with 63 additions and 3 deletions

View File

@@ -24,7 +24,7 @@ export class InboundDto {
@IsNotEmpty() @IsNotEmpty()
@ApiProperty({ @ApiProperty({
description: 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', example: 'EDDB',
}) })
@Length(3) @Length(3)

View File

@@ -29,6 +29,11 @@ import { Inbound } from './models/inbound.model';
import { Planning } from './models/planning.model'; import { Planning } from './models/planning.model';
import { PredictedWaypoint } from './models/predictedwaypoint.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') @Controller('inbound')
export class InboundController { export class InboundController {
constructor( constructor(
@@ -200,7 +205,62 @@ export class InboundController {
status: 404, status: 404,
description: 'The destination airport is unknown', 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<boolean> { async insert(inbound: InboundDto): Promise<boolean> {
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 return this.airportService
.airport(inbound.destination) .airport(inbound.destination)
.then(async (airport) => { .then(async (airport) => {

View File

@@ -14,7 +14,7 @@ export class Aircraft {
@Prop({ @Prop({
required: true, required: true,
type: String, type: String,
enum: ['L', 'M', 'H', 'S'], enum: ['L', 'M', 'H', 'J'],
}) })
wtc: string; wtc: string;

View File

@@ -29,7 +29,7 @@ export class Inbound {
@Prop({ @Prop({
required: true, required: true,
type: String, type: String,
enum: ['UNK', 'DEL', 'GRD', 'TWR', 'APP', 'DEP', 'CTR'], enum: ['UNK', 'DEL', 'GND', 'TWR', 'APP', 'DEP', 'CTR'],
}) })
reporter: string; reporter: string;