diff --git a/src/resolvers/MutationResolvers.ts b/src/resolvers/MutationResolvers.ts index 9b48a7d..8d266c8 100644 --- a/src/resolvers/MutationResolvers.ts +++ b/src/resolvers/MutationResolvers.ts @@ -1,21 +1,47 @@ -import { NotificationResponse, Resolvers } from "../generated/graphql"; +import { MutationScheduleNotificationArgs, NotificationResponse, Resolvers } from "../generated/graphql"; import { ServerContext } from "../ServerContext"; import { ETANotificationScheduler, } from "../notifications/schedulers/ETANotificationScheduler"; import { ScheduledNotification } from "../repositories/NotificationRepository"; +import { InterchangeSystem } from "../entities/InterchangeSystem"; + +async function temp_findMatchingSystemBasedOnShuttleId(context: ServerContext, args: Omit & { + input: NonNullable +}) { + let matchingSystem: InterchangeSystem | undefined; + await Promise.all(context.systems.map(async (system) => { + const shuttle = await system.shuttleRepository.getShuttleById(args.input.shuttleId); + // Theoretically, there should only be one + if (shuttle !== null) { + matchingSystem = system; + } + return shuttle; + })); + return matchingSystem; +} export const MutationResolvers: Resolvers = { + // TODO: Require system ID on these endpoints Mutation: { scheduleNotification: async (_parent, args, context, _info) => { - const shuttle = await context.shuttleRepository.getShuttleById(args.input.shuttleId); + let matchingSystem = await temp_findMatchingSystemBasedOnShuttleId(context, args); + + if (!matchingSystem) { + return { + message: "Shuttle ID doesn't exist", + success: false, + } + } + + const shuttle = await matchingSystem.shuttleRepository.getShuttleById(args.input.shuttleId); if (!shuttle) { return { message: "Shuttle ID doesn't exist", success: false, } } - const stop = await context.shuttleRepository.getStopById(args.input.stopId); + const stop = await matchingSystem.shuttleRepository.getStopById(args.input.stopId); if (!stop) { return { message: "Stop ID doesn't exist", @@ -30,7 +56,7 @@ export const MutationResolvers: Resolvers = { : ETANotificationScheduler.defaultSecondsThresholdForNotificationToFire, } - await context.notificationRepository.addOrUpdateNotification(notificationData); + await matchingSystem.notificationRepository.addOrUpdateNotification(notificationData); const response: NotificationResponse = { message: "Notification scheduled", @@ -40,9 +66,18 @@ export const MutationResolvers: Resolvers = { return response; }, cancelNotification: async (_parent, args, context, _info) => { - const isScheduled = await context.notificationRepository.isNotificationScheduled(args.input) + const matchingSystem = await temp_findMatchingSystemBasedOnShuttleId(context, args); + if (!matchingSystem) { + return { + success: false, + message: "Unable to find correct system", + data: args.input, + } + } + + const isScheduled = await matchingSystem.notificationRepository.isNotificationScheduled(args.input) if (isScheduled) { - await context.notificationRepository.deleteNotificationIfExists(args.input); + await matchingSystem.notificationRepository.deleteNotificationIfExists(args.input); return { success: true, message: "Notification cancelled",