diff --git a/src/controllers/Passenger.controller.ts b/src/controllers/Passenger.controller.ts index bb54c62..1b461d6 100644 --- a/src/controllers/Passenger.controller.ts +++ b/src/controllers/Passenger.controller.ts @@ -55,8 +55,6 @@ export const getAllPassengersForUser = async (req: Request, res: Response) => { // Remove any unnecessary data from the passengers const passengers = await Promise.all(accompPassengersPromise); - console.log('HEERE', passengers); - const trimmedPassengers = passengers?.map((passenger: Record
) => trimPassenger(passenger._rawJson as unknown as PassengerData) @@ -129,46 +127,139 @@ export const getPassengerById = async (req: Request, res: Response) => { * 2. Get the passenger data from the request body, if it doesn't exist return a 400 * 3. Make a call to AirTable to create the passenger, if that fails return a 500 (hint, use try/catch) * 4. Return the created passenger + * 5. add the passenger to the accompanying passengers of the user (req.params) sending the request * * @param req - the request object * @param res - the response object */ + export const createPassenger = async (req: Request, res: Response) => { - const userId = req.query.userId; + //gets userId and passengerData from parameters + const { id } = req.params; const passengerData = req.body; - if (!userId) { - return res.status(400).send({ error: 'User ID is required' }); + if (!id) { + return res.status(400).send('AirTable Record ID is required'); } + if (!passengerData) { - return res.status(400).send({ error: 'Passenger data is required' }); + return res.status(400).send('Passenger data is required.'); } - const newRecord = { - fields: { - 'First Name': passengerData.firstName, - 'Middle Name': passengerData.middleName || '', - 'Last Name': passengerData.lastName, - Email: passengerData.email, - Type: passengerData.type, - '# of Booked Flight Requests': passengerData.numBookedFlightRequests, - 'Multi-patient family?': passengerData.multiPatientFamily, - Relationship: passengerData.relationship, - Birthday: passengerData.birthday, - 'Day Before Birthday': passengerData.dayBeforeBirthday, - 'Day After Birthday': passengerData.dayAfterBirthday, - - UserId: userId, - }, - typecast: true, - }; + try { + const passenger = await base('Passengers').find(id); + } catch (err: any) { + return res.status(400).send('User does not exist'); + } + + //joi stuff to validate whether the passengerData is of the right format + + const passengerSchema = Joi.object({ + fields: Joi.object({ + Relationship: Joi.string() + .valid( + 'Mother', + 'Father', + 'Step-mother', + 'Step-father', + 'Legal Guardian', + 'Spouse', + 'Family Member', + 'Other Caregiver' + ) + .required(), + 'First Name': Joi.string().required(), + 'Last Name': Joi.string().required(), + 'Date of Birth': Joi.date().required(), + Diagnoses: Joi.array().items(Joi.string()), + Gender: Joi.string().valid('Male', 'Female', 'Other').required(), + Street: Joi.string().required(), + City: Joi.string().required(), + State: Joi.string().required(), + Zip: Joi.string().required(), + Country: Joi.string().required(), + 'Cell Phone': Joi.string() + .regex(/^\(\d{3}\) \d{3}-\d{4}$/) + .required(), + Email: Joi.string().email().required(), + Waiver: Joi.boolean().required(), + }).required(), + }); + + const { error, value } = passengerSchema.validate(passengerData); + if (error) { + return res.status(400).json(error.details); + } + //if validation is successful try to create the passenger try { - // const createdRecords = await base('Passengers').create([newRecord]); - // const createdPassenger = createdRecords[0]; - // res.status(200).send({ createdPassenger }); - } catch (error) { - logger.info(error); - res.status(500).send({ error: 'Failed to create passenger' }); + await base('Passengers').create( + [ + { + fields: { + Type: 'Accompanying Passenger', + Relationship: passengerData.fields.Relationship, + 'First Name': passengerData.fields['First Name'], + 'Last Name': passengerData.fields['Last Name'], + 'Date of Birth': passengerData.fields['Date of Birth'], + Diagnoses: passengerData.fields.Diagnoses || [], + Gender: passengerData.fields.Gender, + Street: passengerData.fields.Street, + City: passengerData.fields.City, + State: passengerData.fields.State, + Zip: passengerData.fields.Zip, + Country: passengerData.fields.Country, + 'Cell Phone': passengerData.fields['Cell Phone'], + Email: passengerData.fields.Email, + Waiver: passengerData.fields.Waiver, + 'All Flight Requests (Pass 2)': [], + 'All Flight Legs': [], + 'Related Patient(s)': [], + }, + }, + ], + async (err, records) => { + if (err) { + console.error(err); + return res.status(500).json({ error: 'Error creating passenger' }); + } + + //passenger was created successfully, we need to add them to the relatedPassenger of the user + try { + let relatedPassengers = [] as string[]; + + //find the current related passengers of the user id + base('Passengers').find(id, async (err, record) => { + if (err) { + console.error(err); + return; + } + + relatedPassengers = record?.fields[ + 'Related Accompanying Passenger(s)' + ] as string[]; + + //add the passenger we just created to the user's previous related passengers + const newRelatedPassengers = [ + ...(relatedPassengers || []), + records?.[0].id, + ]; + + // Update the user record with the new list of related passengers + await base('Passengers').update(id, { + 'Related Accompanying Passenger(s)': newRelatedPassengers, + }); + + return res.status(200).send(trimPassenger(records?.[0]._rawJson)); + }); + } catch (err2: any) { + console.error(err2); + return res.status(500).json({ error: 'Error updating user' }); + } + } + ); + } catch (err: any) { + console.error(err); + return res.status(500).json({ error: 'Error creating passenger' }); } }; diff --git a/src/routes/routes.ts b/src/routes/routes.ts index bf00071..c3a4bc2 100644 --- a/src/routes/routes.ts +++ b/src/routes/routes.ts @@ -34,7 +34,7 @@ const routes = (app: Express) => { /* Passenger Controller Routes */ app.get('/passenger/accompanying', getAllPassengersForUser); app.get('/passenger/:id', getPassengerById); - app.post('/passenger/', createPassenger); + app.post('/passenger/:id', createPassenger); app.put('/passenger/:id', updatePassenger); /* Flight Request Controller Routes */ diff --git a/src/tests/Passenger.tests.ts b/src/tests/Passenger.tests.ts index 56807a5..a9e2fdc 100644 --- a/src/tests/Passenger.tests.ts +++ b/src/tests/Passenger.tests.ts @@ -49,6 +49,7 @@ describe('GET /passenger/accompanying', () => { expect(res.body[0]['First Name']).to.be.oneOf([ 'Anakin', 'Bail', + 'Jefferson', 'Jefferson ', ]); expect(res).to.have.status(200); @@ -79,101 +80,185 @@ describe('GET /passenger/accompanying', () => { }); }); -describe('GET /passenger/:id', () => { - it('should return a 400 response', done => { - chai - .request(app) - .get('/passenger/blahblahblah') +//Test createPassenger - .end((err, res) => { - expect(res).to.have.status(400); - done(); - }); - }); - it('should return the correct passenger', done => { +describe('POST /createPassenger', () => { + it('should return 400 if userId is not provided', done => { chai .request(app) - .get('/passenger/rec3Wv1VViXYv3t72') + .post('/passenger/') + .send({}) .end((err, res) => { - expect(res).to.have.status(200); - expect(res.body['First Name'].toString()).to.equal('Anakin'); - expect(res.body['Last Name'].toString()).to.equal('Skywalker '); - expect(res.body['Email']).to.equal('zachmcmullen04@gmail.com'); + expect(res).to.have.status(404); done(); }); }); -}); -describe('PUT passenger/:id', () => { - it('should return a 400 response', done => { + it('should return 400 if passenger data is not provided', done => { chai .request(app) - .put('/passenger/junk') - .send({ id: '' }) + .post('/passenger/rec3Wv1VViXYv3t72') + .send({ id: 'rec3Wv1VViXYv3t72' }) .end((err, res) => { expect(res).to.have.status(400); + expect(res.body[0].message).to.equal('"fields" is required'); done(); }); }); - it('should return a 400 response', done => { + it('should return 400 if user does not exist in AirTable', done => { chai .request(app) - .put('/passenger/junk') - .send({ passengerData: '' }) + .post('/passenger/thisIsBad') + .send({ + fields: { + Relationship: 'Father', + 'First Name': 'Testman', + 'Last Name': 'tester', + 'Date of Birth': '1989-01-05', + Gender: 'Male', + Street: '123 Main St', + City: 'Anytown', + State: 'TN', + Zip: '12345', + Country: 'United States', + 'Cell Phone': '(123) 456-7890', + Email: 'jane.doe@example.com', + Waiver: true, + }, + }) .end((err, res) => { expect(res).to.have.status(400); + expect(res.text).to.contain('User does not exist'); 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 => { + + it('should return 200 and create a passenger when provided valid data', done => { chai .request(app) - .put('/passenger/recaUmd14q3YOP3Uf') - .send({ 'Household Size': 3 }) + .post('/passenger/rec3Wv1VViXYv3t72') + .send({ + fields: { + Relationship: 'Father', + 'First Name': 'Testman', + 'Last Name': 'tester', + 'Date of Birth': '1989-01-05', + Gender: 'Male', + Street: '123 Main St', + City: 'Anytown', + State: 'TN', + Zip: '12345', + Country: 'United States', + 'Cell Phone': '(123) 456-7890', + Email: 'jane.doe@example.com', + Waiver: true, + }, + }) .end((err, res) => { expect(res).to.have.status(200); + expect(res.body['First Name']).to.be.oneOf(['Testman']); + 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(); - }); + + describe('GET /passenger/:id', () => { + it('should return a 400 response', done => { + chai + .request(app) + .get('/passenger/blahblahblah') + + .end((err, res) => { + expect(res).to.have.status(400); + done(); + }); + }); + it('should return the correct passenger', done => { + chai + .request(app) + .get('/passenger/rec3Wv1VViXYv3t72') + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body['First Name'].toString()).to.equal('Anakin'); + expect(res.body['Last Name'].toString()).to.equal('Skywalker '); + expect(res.body['Email']).to.equal('zachmcmullen04@gmail.com'); + 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(); + }); + }); }); });