From c5fa7007815af033c15d4def5d86b8c154cad456 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Wed, 21 Feb 2024 18:27:47 -0600 Subject: [PATCH 1/3] retreive passenger update info from query and make call to airtable to update --- src/controllers/Passenger.controller.ts | 52 ++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/src/controllers/Passenger.controller.ts b/src/controllers/Passenger.controller.ts index 0ff316a..8f9b69f 100644 --- a/src/controllers/Passenger.controller.ts +++ b/src/controllers/Passenger.controller.ts @@ -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({ @@ -150,14 +151,53 @@ 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' }); + } + + const schema = Joi.object({ + street: Joi.string().optional(), + city: Joi.string().optional(), + country: Joi.string().optional(), + email: Joi.string().email().optional(), + cellPhone: Joi.string().optional(), + homePhone: Joi.string().optional(), + education: Joi.string().optional(), + householdIncome: Joi.number().optional(), + householdSize: Joi.number().optional(), + maritalStatus: Joi.string().optional(), + employment: Joi.string().optional, + militaryService: Joi.string().optional(), + militaryMember: Joi.array().optional(), + }); + + const base = new Airtable({ + apiKey: process.env.AIRTABLE_API_KEY || '', + }).base('appwPsfAb6U8CV3mf'); - // validate the passenger data using Joi - // ... + try { + // make a call to AirTable to update the passenger + const response = await base('Passengers').update( + [{ id, fields: passengerData }], + async (err, records) => { + if (err) { + console.error(err); + return; + } + res.status(200).send(records); + } + ); + } catch (err: any) { + // if that fails return a 500 + console.error(err); + return res.status(500).json({ error: 'Error updating' }); + } // create a fake passenger const passenger = createTestPassengerData(); From 6cac430c13077aba89fb29115e20af1985b5bfe5 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Fri, 23 Feb 2024 09:50:27 -0600 Subject: [PATCH 2/3] make tests for updating passenger fields --- src/controllers/FlightRequest.controller.ts | 5 +- src/controllers/Passenger.controller.ts | 45 +++++++------ src/tests/Passenger.tests.ts | 72 +++++++++++++++++++++ 3 files changed, 97 insertions(+), 25 deletions(-) diff --git a/src/controllers/FlightRequest.controller.ts b/src/controllers/FlightRequest.controller.ts index af1eecd..67776ae 100644 --- a/src/controllers/FlightRequest.controller.ts +++ b/src/controllers/FlightRequest.controller.ts @@ -1,5 +1,6 @@ /* eslint-disable no-irregular-whitespace */ import { createTestFlightLegData } from '../data/test-data'; +import logger from '../util/logger'; import Airtable from 'airtable'; import dotenv from 'dotenv'; import type { Request, Response } from 'express'; @@ -68,7 +69,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' }); } }; @@ -102,7 +103,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' }); } diff --git a/src/controllers/Passenger.controller.ts b/src/controllers/Passenger.controller.ts index 8f9b69f..fddd8a8 100644 --- a/src/controllers/Passenger.controller.ts +++ b/src/controllers/Passenger.controller.ts @@ -65,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' }); } }; @@ -162,32 +162,37 @@ export const updatePassenger = async (req: Request, res: Response) => { } const schema = Joi.object({ - street: Joi.string().optional(), - city: Joi.string().optional(), - country: Joi.string().optional(), - email: Joi.string().email().optional(), - cellPhone: Joi.string().optional(), - homePhone: Joi.string().optional(), - education: Joi.string().optional(), - householdIncome: Joi.number().optional(), - householdSize: Joi.number().optional(), - maritalStatus: Joi.string().optional(), - employment: Joi.string().optional, - militaryService: Joi.string().optional(), - militaryMember: Joi.array().optional(), + 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' }); + } + const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY || '', }).base('appwPsfAb6U8CV3mf'); try { // make a call to AirTable to update the passenger - const response = await base('Passengers').update( + await base('Passengers').update( [{ id, fields: passengerData }], async (err, records) => { if (err) { - console.error(err); + logger.error(err); return; } res.status(200).send(records); @@ -195,13 +200,7 @@ export const updatePassenger = 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 updating' }); } - - // create a fake passenger - const passenger = createTestPassengerData(); - - // return the updated passenger - res.status(200).send(passenger); }; diff --git a/src/tests/Passenger.tests.ts b/src/tests/Passenger.tests.ts index fc3a1be..71d724f 100644 --- a/src/tests/Passenger.tests.ts +++ b/src/tests/Passenger.tests.ts @@ -73,4 +73,76 @@ describe('GET /passenger', () => { done(); }); }); + 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: 'loser@weirdo.com' }) + .end((err, res) => { + expect(res).to.have.status(200); + done(); + }); + }); + }); }); From 27706384bcc47826597517787f7eb4306a37649e Mon Sep 17 00:00:00 2001 From: jacoblurie29 Date: Sat, 24 Feb 2024 21:28:49 -0500 Subject: [PATCH 3/3] Fixed test structure --- src/tests/Passenger.tests.ts | 142 +++++++++++++++++------------------ 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/src/tests/Passenger.tests.ts b/src/tests/Passenger.tests.ts index 71d724f..a1b6286 100644 --- a/src/tests/Passenger.tests.ts +++ b/src/tests/Passenger.tests.ts @@ -73,76 +73,76 @@ describe('GET /passenger', () => { done(); }); }); - 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: 'loser@weirdo.com' }) - .end((err, res) => { - expect(res).to.have.status(200); - done(); - }); - }); +}); +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: 'loser@weirdo.com' }) + .end((err, res) => { + expect(res).to.have.status(200); + done(); + }); }); });