Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions features/flight/management/flight.list.feature
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,58 @@ Feature: Get flights list
Then the response status should be 200
And the response header "X-Total-Count" should be "16"

Scenario: As a cabin crew I can list only the flights I captained
Given I am signed in as "cabin crew"
When I send a "GET" request to "/api/v1/flight?phase=finished&captain=me&sort=actualOnBlockTime&order=desc"
Then the response status should be 200
And the response header "X-Total-Count" should be "3"
And the response body property "0.flightNumber" should contain:
"""json
"LH40"
"""
And the response body property "2.flightNumber" should contain:
"""json
"AA4905"
"""

Scenario: As a cabin crew who hasn't finished a flight yet I list none of my own
Given I am signed in as "Alan Doe"
When I send a "GET" request to "/api/v1/flight?phase=finished&captain=me"
Then the response status should be 200
And the response header "X-Total-Count" should be "0"
And the response body should contain:
"""json
[]
"""

Scenario: As an unauthorized user I cannot list my own flights
When I send a "GET" request to "/api/v1/flight?captain=me"
Then the response status should be 401
And the response body should contain:
"""json
{
"statusCode": 401,
"error": "Unauthorized",
"message": "Sign in to list your own flights."
}
"""

Scenario: As cabin crew I cannot list the flights of another captain
Given I am signed in as "cabin crew"
When I send a "GET" request to "/api/v1/flight?captain=someone"
Then the response status should be 400
And the response body should contain:
"""json
{
"statusCode": 400,
"message": "Request validation failed.",
"error": "Bad Request",
"violations": {
"captain": ["captain must be one of the following values: me"]
}
}
"""

Scenario: As cabin crew I can list upcoming flights by scheduled off-block, earliest first
Given I am signed in as "cabin crew"
When I send a "GET" request to "/api/v1/flight?phase=upcoming&sort=scheduledOffBlockTime&order=asc&limit=5"
Expand Down
4 changes: 4 additions & 0 deletions features/user/simbrief/simbrief.verify.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ Feature: Verify a Simbrief user ID
"origin": {
"icaoCode": "EDDF",
"iataCode": "FRA",
"city": "Frankfurt",
"name": "FRANKFURT INTL"
},
"destination": {
"icaoCode": "EPWA",
"iataCode": "WAW",
"city": "Warsaw",
"name": "WARSAW CHOPIN"
},
"aircraft": {
Expand Down Expand Up @@ -45,11 +47,13 @@ Feature: Verify a Simbrief user ID
"origin": {
"icaoCode": "EDDF",
"iataCode": "FRA",
"city": "Frankfurt",
"name": "FRANKFURT INTL"
},
"destination": {
"icaoCode": "EPWA",
"iataCode": "WAW",
"city": "Warsaw",
"name": "WARSAW CHOPIN"
},
"aircraft": {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flight-tracker-api",
"version": "3.89.0",
"version": "3.90.0",
"private": true,
"homepage": "https://mypreflight.io",
"author": "mypreflight",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class ListAllFlightsQuery extends Query<ListAllFlightsResult> {
constructor(
public readonly onlyPublic: boolean,
public readonly filters?: FlightListFilters,
public readonly captainId: string | null = null,
) {
super();
}
Expand All @@ -47,6 +48,7 @@ export class ListAllFlightsHandler implements IQueryHandler<ListAllFlightsQuery>
const { flights, totalCount } = await this.repository.findAll(
query.filters,
query.onlyPublic,
query.captainId,
);

const [pilotsById, flightsWithNotoc] = await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,14 @@ export class FlightsRepository {
async findAll(
filters?: FlightListFilters,
onlyPublic?: boolean,
captainId?: string | null,
): Promise<FlightsWithCount> {
const where: Prisma.FlightWhereInput = {};

if (captainId) {
where.captainId = captainId;
}

if (filters?.phase === FlightPhase.Upcoming) {
where.status = {
in: [FlightStatus.Created, FlightStatus.Ready],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import { UnauthorizedResponse } from '../../../../../../core/http/response/unaut
import { SkipAuth } from '../../../../../../core/http/auth/decorator/skip-auth.decorator';
import { AuthorizedRequest } from '../../../../../../core/http/request/authorized.request';
import {
FlightListCaptain,
FlightListOrder,
FlightListSort,
FlightPhase,
} from '../../../../model/flight.model';
import { ListAllFlightsQuery } from '../../../../application/query/list-all-flights.query';
import { OwnFlightsRequireSignInError } from '../../../../model/error/flight.error';

@ApiTags('flight')
@Controller('api/v1/flight')
Expand Down Expand Up @@ -59,6 +61,14 @@ export class ListFlightsAction {
default: FlightListSort.CreatedAt,
required: false,
})
@ApiQuery({
name: 'captain',
description:
'Only the flights the signed-in user captained, that is checked in to. Requires signing in.',
type: 'string',
enum: FlightListCaptain,
required: false,
})
@ApiQuery({
name: 'order',
description: 'Direction of the ordering',
Expand Down Expand Up @@ -87,7 +97,14 @@ export class ListFlightsAction {
@Res() response: Response,
): Promise<void> {
const onlyPublic = !request.user;
const query = new ListAllFlightsQuery(onlyPublic, filters);

if (filters.captain === FlightListCaptain.Me && !request.user) {
throw new OwnFlightsRequireSignInError();
}

const captainId =
filters.captain === FlightListCaptain.Me ? request.user.sub : null;
const query = new ListAllFlightsQuery(onlyPublic, filters, captainId);
const { flights, totalCount } = await this.queryBus.execute(query);

response.setHeader('X-Total-Count', totalCount.toString());
Expand Down
5 changes: 5 additions & 0 deletions src/modules/flights/infra/http/request/flight.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { Loadsheet } from '../../../model/loadsheet.model';
import { Type } from 'class-transformer';
import {
FlightListCaptain,
FlightListOrder,
FlightListSort,
FlightPhase,
Expand Down Expand Up @@ -401,6 +402,10 @@ export class FlightListFilters {
@IsOptional()
@IsEnum(FlightListOrder)
order: FlightListOrder = FlightListOrder.Descending;

@IsOptional()
@IsEnum(FlightListCaptain)
captain?: FlightListCaptain;
}

export class UpdateFlightVisibilityRequest extends PickType(FlightDto, [
Expand Down
7 changes: 7 additions & 0 deletions src/modules/flights/model/error/flight.error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import {
ConflictError,
ForbiddenError,
NotFoundError,
UnauthorizedError,
UnprocessableError,
} from '../../../../core/errors/domain-error';

export class OwnFlightsRequireSignInError extends UnauthorizedError {
constructor() {
super('Sign in to list your own flights.');
}
}

export class FlightNotFoundError extends NotFoundError {
constructor() {
super('Flight with given ID not found.');
Expand Down
4 changes: 4 additions & 0 deletions src/modules/flights/model/flight.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export enum FlightListOrder {
Descending = 'desc',
}

export enum FlightListCaptain {
Me = 'me',
}

export enum FlightSource {
Manual = 'manual',
Simbrief = 'simbrief',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
import { SimbriefClient } from '../../../../core/provider/simbrief/client/simbrief.client';
import { SimbriefUserNotFoundError } from '../../../../core/provider/simbrief/error/simbrief.error';
import { OperationalFlightPlan } from '../../../../core/provider/simbrief/type/simbrief.types';
import { GetAirportByIcaoCodeQuery } from '../../../airports/application/query/get-airport-by-icao-code.query';
import { AirportByIcaoCodeNotFoundError } from '../../../airports/model/error/airport.error';
import { QueryBus } from '@nestjs/cqrs';

const ofp = {
fetch: { userid: '987654', status: 'Success' },
Expand All @@ -16,14 +19,29 @@ const ofp = {
times: { sched_out: '1786891620', sched_in: '1786898820' },
} as OperationalFlightPlan;

type KnownAirports = Record<string, { iataCode: string; city: string }>;

function handlerReturning(
plan: OperationalFlightPlan | null,
knownAirports: KnownAirports = {},
): VerifySimbriefUserHandler {
const client = {
findOperationalFlightPlan: () => Promise.resolve(plan),
} as unknown as SimbriefClient;
const queryBus = {
execute: (query: GetAirportByIcaoCodeQuery) => {
const known = knownAirports[query.icaoCode];

return known
? Promise.resolve({
iataCode: known.iataCode,
city: { name: known.city },
})
: Promise.reject(new AirportByIcaoCodeNotFoundError());
},
} as unknown as QueryBus;

return new VerifySimbriefUserHandler(client);
return new VerifySimbriefUserHandler(client, queryBus);
}

describe('VerifySimbriefUserHandler', () => {
Expand All @@ -39,11 +57,13 @@ describe('VerifySimbriefUserHandler', () => {
origin: {
icaoCode: 'EDDF',
iataCode: 'FRA',
city: null,
name: 'FRANKFURT INTL',
},
destination: {
icaoCode: 'EPWA',
iataCode: 'WAW',
city: null,
name: 'WARSAW CHOPIN',
},
aircraft: {
Expand Down Expand Up @@ -74,15 +94,47 @@ describe('VerifySimbriefUserHandler', () => {

expect(account.latestFlight).toEqual({
callsign: 'DLH0400',
origin: { icaoCode: 'EDDF', iataCode: null, name: null },
destination: { icaoCode: 'EPWA', iataCode: null, name: null },
origin: { icaoCode: 'EDDF', iataCode: null, city: null, name: null },
destination: { icaoCode: 'EPWA', iataCode: null, city: null, name: null },
aircraft: { registration: 'D-AIRC', type: null, name: null },
scheduledOffBlockTime: null,
scheduledOnBlockTime: null,
generatedAt: null,
});
});

it('takes the IATA code and city of an airport MyPreflight knows', async () => {
const sparse = {
params: {},
general: { icao_airline: 'DLH', flight_number: '0400' },
origin: { icao_code: 'EDDF', iata_code: {}, name: {} },
destination: {
icao_code: 'EPWA',
iata_code: 'WAW',
name: 'WARSAW CHOPIN',
},
aircraft: { reg: 'D-AIRC' },
times: {},
} as unknown as OperationalFlightPlan;

const account = await handlerReturning(sparse, {
EDDF: { iataCode: 'FRA', city: 'Frankfurt' },
}).execute(new VerifySimbriefUserQuery('987654'));

expect(account.latestFlight.origin).toEqual({
icaoCode: 'EDDF',
iataCode: 'FRA',
city: 'Frankfurt',
name: null,
});
expect(account.latestFlight.destination).toEqual({
icaoCode: 'EPWA',
iataCode: 'WAW',
city: null,
name: 'WARSAW CHOPIN',
});
});

it('rejects a user ID Simbrief does not know', async () => {
await expect(
handlerReturning(null).execute(new VerifySimbriefUserQuery('999999')),
Expand Down
Loading
Loading