Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APP 69 make endpoint to get accompanying passengers #3

Merged
merged 10 commits into from
Feb 9, 2024
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
6 changes: 6 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
backend-tests:
runs-on: ubuntu-latest

environment: test

strategy:
matrix:
node-version: [19.x]
Expand All @@ -28,6 +30,10 @@ jobs:

- name: Run testing suite
run: npm run test
env:
PORT: ${{ secrets.PORT }}
HOST: ${{ secrets.HOST }}
AIRTABLE_API_KEY: ${{ secrets.AIRTABLE_API_KEY }}

prettier-check:
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.env
build/
build/
.nyc_output/
1 change: 0 additions & 1 deletion .nyc_output/6ef9d3c5-9edd-4152-a8c1-360cb9258935.json

This file was deleted.

1 change: 0 additions & 1 deletion .nyc_output/d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e.json

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion .nyc_output/processinfo/index.json

This file was deleted.

58 changes: 51 additions & 7 deletions src/controllers/Passenger.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { createTestPassengerData } from '../data/test-data';
import logger from '../util/logger';
import { trimPassenger } from '../util/trim';
import Airtable from 'airtable';
import type { FieldSet, Record } from 'airtable';
import type { PassengerData } from '../interfaces/passenger/passenger.interface';
import type { Request, Response } from 'express';

/**
Expand All @@ -17,13 +22,52 @@ import type { Request, Response } from 'express';
*/
export const getAllPassengersForUser = async (req: Request, res: Response) => {
// get the userId from the query parameters
// const { userId } = req.query;

// create a fake array of passengers
const passengers = Array.from({ length: 10 }, () => createTestPassengerData());

// return the passengers for the user
res.status(200).send(passengers);
const { id } = req.query;

if (!id) {
return res.status(400).json({ error: 'Passenger ID missing' });
}

const base = new Airtable({
apiKey: process.env.AIRTABLE_API_KEY || '',
}).base('appwPsfAb6U8CV3mf');

try {
// make a call to AirTable to get all passengers for the user
await base('Passengers').find(
id.toString(),
async (err: any, record: any | undefined) => {
if (err) {
logger.error(err);
return;
} else {
// get related passengers information
const accompPassengers = [] as Record<FieldSet>[];
const accompanyingPassengersPromise = record._rawJson.fields[
'Related Accompanying Passenger(s)'
].map(async (id: string) => {
// map through the related passengers and get the passenger information for each one
const passenger = await base('Passengers').find(id.toString());
accompPassengers.push(passenger);
});

// Remove any unnecessary data from the passengers
await Promise.all(accompanyingPassengersPromise);
const trimmedPassengers = accompPassengers.map(
(passenger: Record<FieldSet>) =>
trimPassenger(passenger._rawJson as unknown as PassengerData)
);

// return the passengers for the user
return res.send(trimmedPassengers);
}
}
);
} catch (err: any) {
// if that fails return a 500
console.error(err);
return res.status(500).json({ error: 'Error fetching record' });
}
};

/**
Expand Down
3 changes: 0 additions & 3 deletions src/controllers/TestControllers/retrievePassengers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const retrievePassengers = async (req: Request, res: Response) => {
? record.fields['Flight Legs']
: []
) as string[][];
logger.info('Retrieved Flight Leg IDs', flightLegs);

try {
const trips = [] as Record<FieldSet>[][];
Expand All @@ -47,8 +46,6 @@ export const retrievePassengers = async (req: Request, res: Response) => {

await Promise.all(promises);

logger.info('Retrieved trips of flight legs', trips);

// Send the response or do further processing
res.status(200).send(trips);
} catch (err) {
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/passenger/trimmed-passenger.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface TrimmedPassenger {
'Last Name': string;
'Date of Birth': string;
Gender: string;
Relationship?;
string;
Street: string;
Country: string;
Email: string;
Expand Down
76 changes: 76 additions & 0 deletions src/tests/Passenger.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { configureServer } from '../config/server.config';
import chaiHttp from 'chai-http';
import dotenv from 'dotenv';
import chai, { expect } from 'chai';
import type { Server } from 'http';
dotenv.config();

// set up chai
chai.use(chaiHttp);
chai.should();

// set up mock server
const app = configureServer();
let server: Server;

// start mock server
before(done => {
server = app.listen(3483, () => {
done();
});
});

// close mock server
after(done => {
server.close();
done();
});

// describe is group of tests
// it is the actual test itself
// Test case
describe('GET /passenger', () => {
it('should return a 400 response', done => {
chai
.request(app)
.get('/passenger')
.query({ id: '' })
.end((err, res) => {
expect(res).to.have.status(400);
done();
});
});
it('should be an accompanying passenger', done => {
chai
.request(app)
.get('/passenger')
.query({ id: 'recleNlsBm3dheZHy' })
.end((err, res) => {
expect(res.body[0]['First Name']).to.be.oneOf(['Anakin', 'Bail']);
expect(res).to.have.status(200);
done();
});
});
it('should be an accompanying passenger', done => {
chai
.request(app)
.get('/passenger')
.query({ id: 'recleNlsBm3dheZHy' })
.end((err, res) => {
expect(res.body[1]['Gender']).to.equal('Male');
expect(res).to.have.status(200);
done();
});
});
it('should be an accompanying passenger', done => {
chai
.request(app)
.get('/passenger')
.query({ id: 'recleNlsBm3dheZHy' })
.end((err, res) => {
expect(res.body[2]['Relationship']).to.be.oneOf(['Father', undefined]);
expect(res).to.have.status(200);
done();
});
});
});
2 changes: 2 additions & 0 deletions src/util/trim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const trimPassenger = (passenger: PassengerData): TrimmedPassenger => {
id: id,
createdTime: createdTime,
Type: Type,
Relationship: fields['Relationship'],
'First Name': fields['First Name'],
'Last Name': fields['Last Name'],
'Date of Birth': fields['Date of Birth'],
Expand All @@ -37,6 +38,7 @@ export const trimPassenger = (passenger: PassengerData): TrimmedPassenger => {
'Full Name': fields['Full Name'],
Age: Age,
'Latest Trip': fields['Latest Trip'],
string: undefined,
};

return trimmedPassenger;
Expand Down
Loading