Skip to content

Commit

Permalink
make endpoint to get accompanying passengers
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophiahooley committed Feb 7, 2024
1 parent 20d4c88 commit 7628686
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 9 deletions.
1 change: 1 addition & 0 deletions .nyc_output/26ebb75a-bcc7-4f51-86e7-1546eecf58e3.json

Large diffs are not rendered by default.

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parent":null,"pid":14891,"argv":["/opt/homebrew/Cellar/node/20.6.1/bin/node","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/node_modules/.bin/mocha","-r","dotenv/config","--timeout","20000","--exit","-r","ts-node/register","src/tests/Examples.tests.ts","src/tests/Passenger.tests.ts","src/tests/Trimming.tests.ts","src/tests/User.tests.ts"],"execArgv":[],"cwd":"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend","time":1707261398684,"ppid":14890,"coverageFilename":"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/.nyc_output/26ebb75a-bcc7-4f51-86e7-1546eecf58e3.json","externalId":"","uuid":"26ebb75a-bcc7-4f51-86e7-1546eecf58e3","files":["/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/tests/Examples.tests.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/config/server.config.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/routes/routes.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/Passenger.controller.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/data/test-data.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/data/constants.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/util/logger.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/util/trim.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/FlightRequest.controller.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/TestControllers/queryParameterExample.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/TestControllers/pathParameterExample.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/TestControllers/bodyParameterExample.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/TestControllers/retrievePassengers.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/User.controller.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/tests/Passenger.tests.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/tests/Trimming.tests.ts","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/tests/User.tests.ts"]}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parent":"26ebb75a-bcc7-4f51-86e7-1546eecf58e3","pid":14891,"argv":["/opt/homebrew/Cellar/node/20.6.1/bin/node","/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/node_modules/thread-stream/lib/worker.js"],"execArgv":[],"cwd":"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend","time":1707261400818,"ppid":14890,"coverageFilename":"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/.nyc_output/cef9e676-5d26-489f-a7fe-42f784933324.json","externalId":"","uuid":"cef9e676-5d26-489f-a7fe-42f784933324","files":[]}

This file was deleted.

2 changes: 1 addition & 1 deletion .nyc_output/processinfo/index.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"processes":{"6ef9d3c5-9edd-4152-a8c1-360cb9258935":{"parent":"d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e","children":[]},"d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e":{"parent":null,"children":["6ef9d3c5-9edd-4152-a8c1-360cb9258935"]}},"files":{"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/tests/Examples.tests.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/config/server.config.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/routes/routes.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/controllers/Passenger.controller.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/data/test-data.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/data/constants.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/controllers/FlightRequest.controller.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/controllers/TestControllers/queryParameterExample.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/controllers/TestControllers/pathParameterExample.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/controllers/TestControllers/bodyParameterExample.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/controllers/TestControllers/retrievePassengers.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/util/logger.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/controllers/User.controller.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/tests/Trimming.tests.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/util/trim.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"],"/Users/jacoblurie/Git/College/ChangePlusPlus/miracleflightsbackend/src/tests/User.tests.ts":["d17279ea-2c6c-4bc8-bf91-e1388e3c7c6e"]},"externalIds":{}}
{"processes":{"26ebb75a-bcc7-4f51-86e7-1546eecf58e3":{"parent":null,"children":["cef9e676-5d26-489f-a7fe-42f784933324"]},"cef9e676-5d26-489f-a7fe-42f784933324":{"parent":"26ebb75a-bcc7-4f51-86e7-1546eecf58e3","children":[]}},"files":{"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/tests/Examples.tests.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/config/server.config.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/routes/routes.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/Passenger.controller.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/data/test-data.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/data/constants.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/util/logger.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/util/trim.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/FlightRequest.controller.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/TestControllers/queryParameterExample.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/TestControllers/pathParameterExample.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/TestControllers/bodyParameterExample.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/TestControllers/retrievePassengers.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/controllers/User.controller.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/tests/Passenger.tests.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/tests/Trimming.tests.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"],"/Users/sophiascomputer/Documents/vanderbilt/changeplusplus/MFbackend/miracleflightsbackend/src/tests/User.tests.ts":["26ebb75a-bcc7-4f51-86e7-1546eecf58e3"]},"externalIds":{}}
51 changes: 46 additions & 5 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 @@ -19,11 +24,47 @@ 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' });
}

logger.info(id);

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

try {
await base('Passengers').find(
id.toString(),
async function (err: any, record: any | undefined) {
if (err) {
logger.error(err);
return;
} else {
const accompPassengers = [] as Record<FieldSet>[];
const accompanyingPassengersPromise = record._rawJson.fields[
'Related Accompanying Passenger(s)'
].map(async (id: string) => {
const passenger = await base('Passengers').find(id.toString());
accompPassengers.push(passenger);
});
await Promise.all(accompanyingPassengersPromise);
const trimmedPassengers = accompPassengers.map(
(passenger: Record<FieldSet>) =>
trimPassenger(passenger._rawJson as unknown as PassengerData)
);
res.send(trimmedPassengers);
}
}
);
} catch (err: any) {
// if that fails return a 500 (hint, use try/catch)
console.error(err);
return res.status(500).json({ error: 'Error fetching record' });
}
};

/**
Expand Down
54 changes: 54 additions & 0 deletions src/tests/Passenger.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.equal('Bail');
expect(res).to.have.status(200);
done();
});
});
});

0 comments on commit 7628686

Please sign in to comment.