Skip to content

Commit

Permalink
Merge pull request #9 from ChangePlusPlusVandy/APP-82
Browse files Browse the repository at this point in the history
Update Passenger Info
  • Loading branch information
jacoblurie29 authored Feb 25, 2024
2 parents f09aec8 + 2770638 commit f23bc00
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/controllers/FlightRequest.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-irregular-whitespace */
import { createTestFlightLegData } from '../data/test-data';
import logger from '../util/logger';
import { trimFlightLeg, trimRequest } from '../util/trim';
import Airtable from 'airtable';
import dotenv from 'dotenv';
Expand Down Expand Up @@ -111,7 +112,7 @@ export const getFlightRequestById = async (req: Request, res: Response) => {
}
);
} catch (err: any) {
console.error(err);
logger.error(err);
return res.status(500).json({ error: 'Error fetching record' });
}
};
Expand Down Expand Up @@ -145,7 +146,7 @@ export const getFlightLegsById = async (req: Request, res: Response) => {
return res.status(400).json({ error: 'No record found' });
}
} catch (err: any) {
console.error(err);
logger.error(err);
return res.status(500).json({ error: 'Error fetching record' });
}

Expand Down
61 changes: 50 additions & 11 deletions src/controllers/Passenger.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Request, Response } from 'express';
import Airtable from 'airtable';
import dotenv from 'dotenv';
import type { FieldSet, Record } from 'airtable';
import Joi from 'joi';
dotenv.config();

const base = new Airtable({
Expand Down Expand Up @@ -64,7 +65,7 @@ export const getAllPassengersForUser = async (req: Request, res: Response) => {
);
} catch (err: any) {
// if that fails return a 500
console.error(err);
logger.error(err);
return res.status(500).json({ error: 'Error fetching record' });
}
};
Expand Down Expand Up @@ -150,18 +151,56 @@ export const createPassenger = async (req: Request, res: Response) => {
* @param res - the response object
*/
export const updatePassenger = async (req: Request, res: Response) => {
// get the passengerId from the query parameters
// const { passengerId } = req.query;
const { id } = req.params;
const passengerData = req.body;

// get the passenger data from the request body
// const data = req.body;
if (!id) {
return res.status(400).send({ error: 'User ID is required' });
}
if (!passengerData) {
return res.status(400).send({ error: 'Passenger data is required' });
}

// validate the passenger data using Joi
// ...
const schema = Joi.object({
Street: Joi.string().optional(),
City: Joi.string().optional(),
State: Joi.string().optional(),
Country: Joi.string().optional(),
Email: Joi.string().email().optional(),
'Cell Phone': Joi.string().optional(),
'Home Phone': Joi.string().optional(),
Education: Joi.string().optional(),
'Household Income': Joi.number().optional(),
'Household Size': Joi.number().optional(),
'Marital Status': Joi.string().optional(),
Employment: Joi.string().optional,
'Military Service': Joi.string().optional(),
'Military Member': Joi.array().optional(),
});

if (schema.validate(passengerData).error) {
return res.status(400).send({ error: 'Invalid passenger data' });
}

// create a fake passenger
const passenger = createTestPassengerData();
const base = new Airtable({
apiKey: process.env.AIRTABLE_API_KEY || '',
}).base('appwPsfAb6U8CV3mf');

// return the updated passenger
res.status(200).send(passenger);
try {
// make a call to AirTable to update the passenger
await base('Passengers').update(
[{ id, fields: passengerData }],
async (err, records) => {
if (err) {
logger.error(err);
return;
}
res.status(200).send(records);
}
);
} catch (err: any) {
// if that fails return a 500
logger.error(err);
return res.status(500).json({ error: 'Error updating' });
}
};
72 changes: 72 additions & 0 deletions src/tests/Passenger.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,75 @@ describe('GET /passenger', () => {
});
});
});
describe('PUT passenger/:id', () => {
it('should return a 400 response', done => {
chai
.request(app)
.put('/passenger/junk')
.send({ id: '' })
.end((err, res) => {
expect(res).to.have.status(400);
done();
});
});
it('should return a 400 response', done => {
chai
.request(app)
.put('/passenger/junk')
.send({ passengerData: '' })
.end((err, res) => {
expect(res).to.have.status(400);
done();
});
});
it('should update street for anakin skywalker', done => {
chai
.request(app)
.put('/passenger/rec3Wv1VViXYv3t72')
.send({ Street: 'HELLOSTREET' })
.end((err, res) => {
expect(res).to.have.status(200);
done();
});
});
it('should update marital status for princess leia', done => {
chai
.request(app)
.put('/passenger/recaUmd14q3YOP3Uf')
.send({ 'Marital Status': 'Married' })
.end((err, res) => {
expect(res).to.have.status(200);
done();
});
});
it('should update household size for princess leia', done => {
chai
.request(app)
.put('/passenger/recaUmd14q3YOP3Uf')
.send({ 'Household Size': 3 })
.end((err, res) => {
expect(res).to.have.status(200);
done();
});
});
it('should return a 400 response', done => {
chai
.request(app)
.put('/passenger/recaUmd14q3YOP3Uf')
.send({ 'Household Size': 'test' })
.end((err, res) => {
expect(res).to.have.status(400);
done();
});
});
it('should update email for jefferson morales', done => {
chai
.request(app)
.put('/passenger/recLFdznCJOUPEx72')
.send({ Email: '[email protected]' })
.end((err, res) => {
expect(res).to.have.status(200);
done();
});
});
});

0 comments on commit f23bc00

Please sign in to comment.