Skip to content

Commit

Permalink
Fixed endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblurie29 committed Apr 12, 2024
1 parent 5893fe8 commit 5619385
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions src/controllers/FlightRequest.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"`,
Expand All @@ -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)),
};
});

Expand Down

0 comments on commit 5619385

Please sign in to comment.