mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
59 lines
1.8 KiB
TypeScript
59 lines
1.8 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.repository.getShuttleById(args.input.shuttleId);
|
|
if (!shuttle) {
|
|
return {
|
|
message: "Shuttle ID doesn't exist",
|
|
success: false,
|
|
}
|
|
}
|
|
const stop = await context.repository.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.notificationService.scheduleNotification(notificationData);
|
|
|
|
const response: NotificationResponse = {
|
|
message: "Notification scheduled",
|
|
success: true,
|
|
data: args.input,
|
|
}
|
|
return response;
|
|
},
|
|
cancelNotification: async (_parent, args, context, _info) => {
|
|
if (context.notificationService.isNotificationScheduled(args.input)) {
|
|
await context.notificationService.cancelNotificationIfExists(args.input);
|
|
return {
|
|
success: true,
|
|
message: "Notification cancelled",
|
|
data: args.input,
|
|
}
|
|
}
|
|
|
|
return {
|
|
success: false,
|
|
message: "Notification doesn't exist"
|
|
}
|
|
},
|
|
},
|
|
}
|