mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
import { Eta, Resolvers } from "../generated/graphql";
|
|
import { ServerContext } from "../ServerContext";
|
|
|
|
export const ShuttleResolvers: Resolvers<ServerContext> = {
|
|
Shuttle: {
|
|
eta: async (parent, args, contextValue, info) => {
|
|
if (!args.forStopId) return null;
|
|
|
|
const system = contextValue.findSystemById(parent.systemId);
|
|
if (!system) return null;
|
|
|
|
const etaForStopId = await system.shuttleRepository.getEtaForShuttleAndStopId(parent.id, args.forStopId);
|
|
if (etaForStopId === null) return null;
|
|
|
|
return {
|
|
stopId: args.forStopId,
|
|
secondsRemaining: etaForStopId.secondsRemaining,
|
|
shuttleId: parent.id,
|
|
shuttle: parent,
|
|
systemId: system.id,
|
|
updatedTimeMs: etaForStopId.updatedTime,
|
|
};
|
|
},
|
|
etas: async (parent, args, contextValue, info) => {
|
|
const system = contextValue.findSystemById(parent.systemId);
|
|
if (!system) return null;
|
|
|
|
const etasForShuttle = await system.shuttleRepository.getEtasForShuttleId(parent.id);
|
|
if (!etasForShuttle) return null;
|
|
|
|
const computedEtas = await Promise.all(
|
|
etasForShuttle.map(async ({
|
|
secondsRemaining,
|
|
stopId,
|
|
updatedTime
|
|
}): Promise<Eta | null> => {
|
|
return {
|
|
secondsRemaining,
|
|
stopId,
|
|
shuttle: parent,
|
|
shuttleId: parent.id,
|
|
systemId: system.id,
|
|
updatedTime: updatedTime,
|
|
}
|
|
}));
|
|
|
|
if (computedEtas.every((eta) => eta !== null)) {
|
|
return computedEtas;
|
|
}
|
|
|
|
return [];
|
|
},
|
|
route: async (parent, args, contextValue, info) => {
|
|
const system = contextValue.findSystemById(parent.systemId);
|
|
if (!system) return null;
|
|
|
|
const route = await system.shuttleRepository.getRouteById(parent.routeId);
|
|
if (route === null) return null;
|
|
|
|
return {
|
|
color: route.color,
|
|
id: route.id,
|
|
name: route.name,
|
|
polylineCoordinates: route.polylineCoordinates,
|
|
systemId: system.id,
|
|
updatedTimeMs: route.updatedTime
|
|
}
|
|
}
|
|
},
|
|
}
|