diff --git a/src/resolvers/OrderedStopResolvers.ts b/src/resolvers/OrderedStopResolvers.ts index fe047aa..6190814 100644 --- a/src/resolvers/OrderedStopResolvers.ts +++ b/src/resolvers/OrderedStopResolvers.ts @@ -3,17 +3,21 @@ import { ServerContext } from "../ServerContext"; export const OrderedStopResolvers: Resolvers = { OrderedStop: { - nextStop: async (parent, args, contextValue, info): Promise => { + nextStop: async (parent, args, contextValue, _info): Promise => { const routeId = parent.routeId; const stopId = parent.stopId; - const currentOrderedStop = await contextValue.shuttleRepository.getOrderedStopByRouteAndStopId(routeId, stopId); + if (!parent.stop) return null; + const system = contextValue.findSystemById(parent.stop.systemId); + if (!system) return null; + + const currentOrderedStop = await system.shuttleRepository.getOrderedStopByRouteAndStopId(routeId, stopId); if (!currentOrderedStop) return null; const nextOrderedStop = currentOrderedStop.nextStop; if (!nextOrderedStop) return null; - const nextOrderedStopObject = await contextValue.shuttleRepository.getStopById(nextOrderedStop.stopId); + const nextOrderedStopObject = await system.shuttleRepository.getStopById(nextOrderedStop.stopId); if (!nextOrderedStopObject) return null; return { @@ -22,17 +26,21 @@ export const OrderedStopResolvers: Resolvers = { stopId: nextOrderedStopObject.id, } }, - previousStop: async (parent, args, contextValue, info): Promise => { + previousStop: async (parent, args, contextValue, _info): Promise => { const routeId = parent.routeId; const stopId = parent.stopId; - const currentOrderedStop = await contextValue.shuttleRepository.getOrderedStopByRouteAndStopId(routeId, stopId); + if (!parent.stop) return null; + const system = contextValue.findSystemById(parent.stop.systemId); + if (!system) return null; + + const currentOrderedStop = await system.shuttleRepository.getOrderedStopByRouteAndStopId(routeId, stopId); if (!currentOrderedStop) return null; const previousOrderedStop = currentOrderedStop.previousStop; if (!previousOrderedStop) return null; - const previousOrderedStopObject = await contextValue.shuttleRepository.getStopById(previousOrderedStop.stopId); + const previousOrderedStopObject = await system.shuttleRepository.getStopById(previousOrderedStop.stopId); if (!previousOrderedStopObject) return null; return { @@ -41,11 +49,19 @@ export const OrderedStopResolvers: Resolvers = { stopId: previousOrderedStopObject.id, } }, - stop: async (parent, args, contextValue, info) => { - return await contextValue.shuttleRepository.getStopById(parent.stopId); + stop: async (parent, args, contextValue, _info) => { + if (!parent.stop) return null; + const system = contextValue.findSystemById(parent.stop.systemId); + if (!system) return null; + + return await system.shuttleRepository.getStopById(parent.stopId); }, - route: async (parent, args, contextValue, info) => { - return await contextValue.shuttleRepository.getRouteById(parent.routeId); + route: async (parent, args, contextValue, _info) => { + if (!parent.stop) return null; + const system = contextValue.findSystemById(parent.stop.systemId); + if (!system) return null; + + return await system.shuttleRepository.getRouteById(parent.routeId); }, },