Files
project-inter-server/src/resolvers/MutationResolvers.ts

95 lines
3.1 KiB
TypeScript

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<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> = {
// TODO: Require system ID on these endpoints
Mutation: {
scheduleNotification: async (_parent, args, context, _info) => {
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 matchingSystem.shuttleRepository.getStopById(args.input.stopId);
if (!stop) {
return {
message: "Stop ID doesn't exist",
success: false,
}
}
const notificationData: ScheduledNotification = {
...args.input,
secondsThreshold: typeof args.input.secondsThreshold === 'number'
? args.input.secondsThreshold
: ETANotificationScheduler.defaultSecondsThresholdForNotificationToFire,
}
await matchingSystem.notificationRepository.addOrUpdateNotification(notificationData);
const response: NotificationResponse = {
message: "Notification scheduled",
success: true,
data: args.input,
}
return response;
},
cancelNotification: async (_parent, args, context, _info) => {
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 matchingSystem.notificationRepository.deleteNotificationIfExists(args.input);
return {
success: true,
message: "Notification cancelled",
data: args.input,
}
}
return {
success: false,
message: "Notification doesn't exist"
}
},
},
}