update mutation resolvers with temporary method to locate system

This commit is contained in:
2025-04-06 12:54:22 -07:00
parent f113dc1ec2
commit af57063b15

View File

@@ -1,21 +1,47 @@
import { NotificationResponse, Resolvers } from "../generated/graphql"; import { MutationScheduleNotificationArgs, NotificationResponse, Resolvers } from "../generated/graphql";
import { ServerContext } from "../ServerContext"; import { ServerContext } from "../ServerContext";
import { import {
ETANotificationScheduler, ETANotificationScheduler,
} from "../notifications/schedulers/ETANotificationScheduler"; } from "../notifications/schedulers/ETANotificationScheduler";
import { ScheduledNotification } from "../repositories/NotificationRepository"; import { ScheduledNotification } from "../repositories/NotificationRepository";
import { InterchangeSystem } from "../entities/InterchangeSystem";
async function temp_findMatchingSystemBasedOnShuttleId(context: ServerContext, args: Omit<MutationScheduleNotificationArgs, "input"> & {
input: NonNullable<MutationScheduleNotificationArgs["input"]>
}) {
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<ServerContext> = { export const MutationResolvers: Resolvers<ServerContext> = {
// TODO: Require system ID on these endpoints
Mutation: { Mutation: {
scheduleNotification: async (_parent, args, context, _info) => { 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) { if (!shuttle) {
return { return {
message: "Shuttle ID doesn't exist", message: "Shuttle ID doesn't exist",
success: false, success: false,
} }
} }
const stop = await context.shuttleRepository.getStopById(args.input.stopId); const stop = await matchingSystem.shuttleRepository.getStopById(args.input.stopId);
if (!stop) { if (!stop) {
return { return {
message: "Stop ID doesn't exist", message: "Stop ID doesn't exist",
@@ -30,7 +56,7 @@ export const MutationResolvers: Resolvers<ServerContext> = {
: ETANotificationScheduler.defaultSecondsThresholdForNotificationToFire, : ETANotificationScheduler.defaultSecondsThresholdForNotificationToFire,
} }
await context.notificationRepository.addOrUpdateNotification(notificationData); await matchingSystem.notificationRepository.addOrUpdateNotification(notificationData);
const response: NotificationResponse = { const response: NotificationResponse = {
message: "Notification scheduled", message: "Notification scheduled",
@@ -40,9 +66,18 @@ export const MutationResolvers: Resolvers<ServerContext> = {
return response; return response;
}, },
cancelNotification: async (_parent, args, context, _info) => { 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) { if (isScheduled) {
await context.notificationRepository.deleteNotificationIfExists(args.input); await matchingSystem.notificationRepository.deleteNotificationIfExists(args.input);
return { return {
success: true, success: true,
message: "Notification cancelled", message: "Notification cancelled",