Skip to content

Commit

Permalink
Merge branch 'main' into app-91-jotform
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobjdavey committed Apr 15, 2024
2 parents 0ce841b + 17b1dd8 commit ef151b5
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/controllers/FlightRequest.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,38 @@ export const getAllFlightRequestsForUser = async (
.map(request => trimRequest(request as unknown as FlightRequestData));

// Get the flight leg IDs from the trimmed flight requests
const flightLegIds = trimmedFlightRequests
.map(request => request['Flight Legs'])
.flatMap(leg => leg);
const flightLegIds =
trimmedFlightRequests?.map(request => request['Flight Legs']) || [];

// 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();
const flightLegsData = await Promise.all(
flightLegIds.map(async (idSection: string[]) =>
idSection && idSection.length > 0
? await base('Flight Legs')
.select({
filterByFormula: `OR(${idSection
.map(id => `RECORD_ID() = "${id}"`)
.join(',')})`,
})
.all()
: []
)
);

const formattedFlightLegs = flightLegsData.map(leg =>
leg?.map(l =>
l?._rawJson ? trimFlightLeg(l?._rawJson as unknown as FlightLegData) : []
)
);

// Get the flight legs data
const flightLegsData = flightLegs.map(leg => leg._rawJson);
// formatted flight legs is an array of arrays, so we need to match the flight leg IDs to the flight requests

// 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'])
);

return {
...request,
'Flight Legs': flightLegs.map(leg => trimFlightLeg(leg as FlightLegData)),
};
});
const formattedFlightRequests = trimmedFlightRequests.map(request => ({
...request,
'Flight Legs':
formattedFlightLegs[trimmedFlightRequests.indexOf(request)] || [],
}));

return res.status(200).json(formattedFlightRequests);
} catch (error) {
Expand Down

0 comments on commit ef151b5

Please sign in to comment.