From 5619385b5cc4a5b67e5ff3b2cc11071c8b3514dd Mon Sep 17 00:00:00 2001 From: jacoblurie29 Date: Fri, 12 Apr 2024 09:35:42 -0500 Subject: [PATCH] Fixed endpoint --- src/controllers/FlightRequest.controller.ts | 65 +++++++++++++-------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/src/controllers/FlightRequest.controller.ts b/src/controllers/FlightRequest.controller.ts index 66fd963..ef483f1 100644 --- a/src/controllers/FlightRequest.controller.ts +++ b/src/controllers/FlightRequest.controller.ts @@ -27,6 +27,20 @@ const base = new Airtable({ * @param req - the request object * @param res - the response object */ +/** + * Get all flight requests for a given user + * + * Steps to complete: + * 1. Get the userId from the query parameters, if it doesn't exist return a 400 + * 2. Make a call to AirTable to get all flight requests for the user, if that fails return a 500 (hint, use try/catch) + * If there are no flight requests for the user return a 400. (hint: use the AirTable API, see TestControllers/retrievePassengers.ts for an example) + * Another hint - we will be filtering by the "Patient AirTable Record ID" field in the AirTable + * 3. Remove any unnecessary data from the flight requests (there is a lot of data in the AirTable response we don't need) + * 4. Return the flight requests for the user + * + * @param req - the request object + * @param res - the response object + */ export const getAllFlightRequestsForUser = async ( req: Request, res: Response @@ -38,7 +52,7 @@ export const getAllFlightRequestsForUser = async ( } try { - // query Airtable for flight requests for the user ID + // Query Airtable for flight requests for the user ID const flightRequests = await base('Flight Requests (Trips)') .select({ filterByFormula: `{Patient AirTable Record ID} = "${userId}"`, @@ -51,36 +65,37 @@ export const getAllFlightRequestsForUser = async ( .json({ error: 'No flight requests found for this user' }); } - const trimmedFlightRequests = flightRequests.map(request => - trimRequest(request as unknown as FlightRequestData) - ); + // Trim unnecessary data from the flight requests + const trimmedFlightRequests = flightRequests + .map(request => request._rawJson) + .map(request => trimRequest(request as unknown as FlightRequestData)); - // Retrieve flight legs for each flight request and format the data - - const promises = trimmedFlightRequests.map(async request => { - return (request['Flight Legs'].map( - async flightLegId => - await base('Flight Legs') - .select({ - filterByFormula: `{AirTable Record ID} = "${flightLegId}"`, - }) - .all() - )) - } - ); + // Get the flight leg IDs from the trimmed flight requests + const flightLegIds = trimmedFlightRequests + .map(request => request['Flight Legs']) + .flatMap(leg => leg); - const flightLegs = flightLegsDetails.map( - request => (request as any)._rawJson - ); + // Query Airtable for the flight legs using the flight leg IDs + const flightLegs = await base('Flight Legs') + .select({ + filterByFormula: `OR(${flightLegIds + .map(id => `RECORD_ID() = "${id}"`) + .join(',')})`, + }) + .all(); - console.log(flightLegs); + // Get the flight legs data + const flightLegsData = flightLegs.map(leg => leg._rawJson); + + // Format the flight requests by replacing flight leg IDs with actual flight leg data + const formattedFlightRequests = trimmedFlightRequests.map(request => { + const flightLegs = flightLegsData.filter(leg => + request['Flight Legs'].includes(leg.fields['AirTable Record ID']) + ); - // Format the flight request data and include the corresponding flight legs return { ...request, - flightLegs: flightLegsDetails.map(leg => - trimFlightLeg(leg as unknown as FlightLegData) - ), + 'Flight Legs': flightLegs.map(leg => trimFlightLeg(leg as FlightLegData)), }; });