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

59 lines
1.9 KiB
TypeScript

import { NotificationResponse, Resolvers } from "../generated/graphql";
import { ServerContext } from "../ServerContext";
import {
ETANotificationScheduler,
} from "../notifications/schedulers/ETANotificationScheduler";
import { ScheduledNotification } from "../repositories/NotificationRepository";
export const MutationResolvers: Resolvers<ServerContext> = {
Mutation: {
scheduleNotification: async (_parent, args, context, _info) => {
const shuttle = await context.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);
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 context.notificationRepository.scheduleNotification(notificationData);
const response: NotificationResponse = {
message: "Notification scheduled",
success: true,
data: args.input,
}
return response;
},
cancelNotification: async (_parent, args, context, _info) => {
if (context.notificationRepository.isNotificationScheduled(args.input)) {
await context.notificationRepository.cancelNotificationIfExists(args.input);
return {
success: true,
message: "Notification cancelled",
data: args.input,
}
}
return {
success: false,
message: "Notification doesn't exist"
}
},
},
}