add etas resolver on shuttle object

This commit is contained in:
2024-12-22 20:17:00 -08:00
parent 0133acd900
commit 4f0a10d0c4

View File

@@ -112,6 +112,34 @@ export const resolvers: Resolvers<ServerContext> = {
return etaForNextStop; return etaForNextStop;
}, },
etas: async (parent, args, contextValue, info) => {
const etasForShuttle = await contextValue.repository.getEtasForShuttleId(parent.id);
if (!etasForShuttle) return null;
const computedEtas = await Promise.all(etasForShuttle.map(async ({
secondsRemaining,
stopId,
}): Promise<Eta | null> => {
const stop = await contextValue.repository.getStopById(stopId);
if (stop === null) return null;
return {
secondsRemaining,
stop: {
coordinates: stop.coordinates,
id: stop.id,
name: stop.name,
},
shuttle: parent,
}
}));
if (computedEtas.every((eta) => eta !== null)) {
return computedEtas;
}
return [];
}
}, },
Stop: { Stop: {
orderedStops: async (parent, args, contextValue, info) => { orderedStops: async (parent, args, contextValue, info) => {
@@ -140,11 +168,11 @@ export const resolvers: Resolvers<ServerContext> = {
}; };
})); }));
if (!computedOrderedStops.every((value) => value !== null)) { if (computedOrderedStops.every((value) => value !== null)) {
return []; return computedOrderedStops as OrderedStop[];
} }
return computedOrderedStops as OrderedStop[]; return [];
} }
}, },
OrderedStop: { OrderedStop: {
@@ -192,5 +220,5 @@ export const resolvers: Resolvers<ServerContext> = {
} }
} }
}, },
} },
}; };