Files
project-inter-server/src/resolvers/SystemResolvers.ts
Brendan Chen 415b461308 Add sorting to all data types
- Add name-based sorting for entities with names
- Add order-based sorting for ordered stops
- Add "seconds remaining" based sorting for ETAs
- Add tests to check sorting
2025-09-30 15:24:22 -07:00

100 lines
3.0 KiB
TypeScript

import { Coordinates, Resolvers } from "../generated/graphql";
import { ServerContext } from "../ServerContext";
export const SystemResolvers: Resolvers<ServerContext> = {
System: {
routes: async (parent, _args, contextValue, _info) => {
const system = contextValue.findSystemById(parent.id);
if (!system) {
return [];
}
const routes = await system.shuttleRepository.getRoutes();
return routes.slice().sort((a, b) => a.name.localeCompare(b.name));
},
stops: async (parent, _args, contextValue, _info) => {
const system = contextValue.findSystemById(parent.id);
if (!system) {
return [];
}
const stops = await system.shuttleRepository.getStops();
return stops.slice().sort((a, b) => a.name.localeCompare(b.name));
},
stop: async (parent, args, contextValue, _info) => {
if (!args.id) return null;
const system = contextValue.findSystemById(parent.id);
if (!system) {
return null;
}
const stop = await system.shuttleRepository.getStopById(args.id);
if (stop === null) return null;
if (stop.systemId !== parent.id) return null;
return {
id: stop.id,
name: stop.name,
coordinates: stop.coordinates as Coordinates,
systemId: parent.id,
updatedTimeMs: stop.updatedTime,
};
},
route: async (parent, args, contextValue, _info) => {
if (!args.id) return null;
const system = contextValue.findSystemById(parent.id);
if (!system) {
return null;
}
const route = await system.shuttleRepository.getRouteById(args.id);
if (route === null) return null;
if (route.systemId !== parent.id) return null;
return {
color: route.color,
id: route.id,
name: route.name,
polylineCoordinates: route.polylineCoordinates as Coordinates[],
systemId: parent.id,
updatedTimeMs: route.updatedTime,
};
},
shuttle: async (parent, args, contextValue, _info) => {
if (!args.id) return null;
const system = contextValue.findSystemById(parent.id);
if (!system) {
return null;
}
const shuttle = await system.shuttleRepository.getShuttleById(args.id);
if (shuttle === null) return null;
if (shuttle.systemId !== parent.id) return null;
return shuttle;
},
shuttles: async (parent, _args, contextValue, _info) => {
const system = contextValue.findSystemById(parent.id);
if (!system) {
return [];
}
const shuttles = await system.shuttleRepository.getShuttles();
return shuttles.slice().sort((a, b) => a.name.localeCompare(b.name));
},
parkingSystem: async (parent, _args, contextValue, _info) => {
const system = contextValue.findSystemById(parent.id);
if (!system) {
return null;
}
if (!system.parkingRepository) return null;
return {
systemId: parent.id,
};
},
},
}