Skip to content

Commit

Permalink
Merge branch main into APP-67-Flight-Request-Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblurie29 committed Feb 12, 2024
2 parents 6006bcc + 1d0f565 commit 7904b59
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 25 deletions.
10 changes: 4 additions & 6 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,14 +30,10 @@ jobs:

- name: Run testing suite
run: npm run test

env:
MONGODB_URI: ${{ secrets.MONGODB_URI }}
PORT: ${{ secrets.PORT }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
REGION: ${{ secrets.REGION }}
S3_BUCKET: ${{ secrets.S3_BUCKET }}
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 @@ -31,7 +31,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 @@ -49,8 +48,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
23 changes: 20 additions & 3 deletions src/controllers/User.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { trimPassenger } from '../util/trim';
import Airtable from 'airtable';
import Joi from 'joi';
import type { Request, Response } from 'express';

import type { PassengerData } from '../interfaces/passenger/passenger.interface';

/**
* This function returns all passengers connected to a user
*
* Steps to complete:
* 1. Get the first name, last name, and birthdate from the request body, if it doesn't exist return a 400
* 2. Make a call to AirTable to check if the user exists, if that fails return a 500 (hint, use try/catch)
* Another hint - we will be filtering by the "Passenger ID" field in the AirTable
* 3. Remove any unnecessary data from the passenger (there is a lot of data in the AirTable response we don't need)
* 4. Return the passengers for the user
*
* @param req - the request object
* @param res - the response object
*/
export const createUser = async (req: Request, res: Response) => {
// given a first name, last name, and birthdate, check if a user exists in the database
const schema = Joi.object({
firstName: Joi.string().required(),
lastName: Joi.string().required(),
birthdate: Joi.date().required(),
birthdate: Joi.string().required(),
});

// validate the request body
Expand Down Expand Up @@ -39,5 +54,7 @@ export const createUser = async (req: Request, res: Response) => {
return res.status(400).send('User does not exist');
}

return res.status(200).send(passenger[0].fields);
return res
.status(200)
.send(trimPassenger(passenger[0] as unknown as PassengerData));
};
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

0 comments on commit 7904b59

Please sign in to comment.