diff --git a/src/controllers/FlightRequest.controller.ts b/src/controllers/FlightRequest.controller.ts index 8b704ee..3c97a1e 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 { trimFlightLeg, trimRequest } from '../util/trim'; import Airtable from 'airtable'; import dotenv from 'dotenv'; @@ -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' }); } }; @@ -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' }); } diff --git a/src/controllers/Passenger.controller.ts b/src/controllers/Passenger.controller.ts index 0ff316a..fddd8a8 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({ @@ -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' }); } }; @@ -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' }); + } }; diff --git a/src/tests/Passenger.tests.ts b/src/tests/Passenger.tests.ts index fc3a1be..a1b6286 100644 --- a/src/tests/Passenger.tests.ts +++ b/src/tests/Passenger.tests.ts @@ -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: 'loser@weirdo.com' }) + .end((err, res) => { + expect(res).to.have.status(200); + done(); + }); + }); +});