From 09be37cedb9b5632633268b26d63967f438b6b7a Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 27 Mar 2025 09:57:49 -0700 Subject: [PATCH] move arguments and notification interfaces to notification repository file --- .../schedulers/ETANotificationScheduler.ts | 30 +++++------------ src/repositories/NotificationRepository.ts | 33 +++++++++++++++++++ src/resolvers/MutationResolvers.ts | 2 +- test/resolvers/QueryResolverTests.test.ts | 2 +- 4 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 src/repositories/NotificationRepository.ts diff --git a/src/notifications/schedulers/ETANotificationScheduler.ts b/src/notifications/schedulers/ETANotificationScheduler.ts index 3d270dd..85e911f 100644 --- a/src/notifications/schedulers/ETANotificationScheduler.ts +++ b/src/notifications/schedulers/ETANotificationScheduler.ts @@ -2,22 +2,10 @@ import { ShuttleGetterRepository } from "../../repositories/ShuttleGetterReposit import { TupleKey } from "../../types/TupleKey"; import { IEta } from "../../entities/entities"; import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender"; - -export interface NotificationLookupArguments { - deviceId: string; - shuttleId: string; - stopId: string; -} - -export interface NotificationSchedulingArguments extends NotificationLookupArguments { - /** - * Value which specifies the ETA of the shuttle for when - * the notification should fire. - * For example, a secondsThreshold of 180 would mean that the notification - * fires when the ETA drops below 3 minutes. - */ - secondsThreshold: number; -} +import { + NotificationLookupArguments, + ScheduledNotification +} from "../../repositories/NotificationRepository"; type DeviceIdSecondsThresholdAssociation = { [key: string]: number }; @@ -47,7 +35,7 @@ export class ETANotificationScheduler { */ private deviceIdsToDeliverTo: { [key: string]: DeviceIdSecondsThresholdAssociation } = {} - private async sendEtaNotificationImmediately(notificationData: NotificationSchedulingArguments): Promise { + private async sendEtaNotificationImmediately(notificationData: ScheduledNotification): Promise { const { deviceId, shuttleId, stopId } = notificationData; const shuttle = await this.shuttleRepository.getShuttleById(shuttleId); @@ -85,7 +73,7 @@ export class ETANotificationScheduler { const deviceIdsToRemove = new Set(); for (let deviceId of Object.keys(this.deviceIdsToDeliverTo[tupleKey])) { - const scheduledNotificationData: NotificationSchedulingArguments = { + const scheduledNotificationData: ScheduledNotification = { deviceId, secondsThreshold: this.deviceIdsToDeliverTo[tupleKey][deviceId], shuttleId: eta.shuttleId, @@ -103,7 +91,7 @@ export class ETANotificationScheduler { }); } - private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(notificationObject: NotificationSchedulingArguments, etaSecondsRemaining: number) { + private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(notificationObject: ScheduledNotification, etaSecondsRemaining: number) { if (etaSecondsRemaining > notificationObject.secondsThreshold) { return false; } @@ -119,7 +107,7 @@ export class ETANotificationScheduler { * @param secondsThreshold Value which specifies the ETA of the shuttle for when * the notification should fire. */ - public async scheduleNotification({ deviceId, shuttleId, stopId, secondsThreshold }: NotificationSchedulingArguments) { + public async scheduleNotification({ deviceId, shuttleId, stopId, secondsThreshold }: ScheduledNotification) { const tuple = new TupleKey(shuttleId, stopId); if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { this.deviceIdsToDeliverTo[tuple.toString()] = {}; @@ -169,7 +157,7 @@ export class ETANotificationScheduler { * @param deviceId */ public async getAllScheduledNotificationsForDevice(deviceId: string): Promise { - const scheduledNotifications: NotificationSchedulingArguments[] = []; + const scheduledNotifications: ScheduledNotification[] = []; for (const key of Object.keys(this.deviceIdsToDeliverTo)) { if (deviceId in this.deviceIdsToDeliverTo[key]) { diff --git a/src/repositories/NotificationRepository.ts b/src/repositories/NotificationRepository.ts new file mode 100644 index 0000000..e37256e --- /dev/null +++ b/src/repositories/NotificationRepository.ts @@ -0,0 +1,33 @@ +export interface NotificationLookupArguments { + deviceId: string; + shuttleId: string; + stopId: string; +} + +export interface ScheduledNotification extends NotificationLookupArguments { + /** + * Value which specifies the ETA of the shuttle for when + * the notification should fire. + * For example, a secondsThreshold of 180 would mean that the notification + * fires when the ETA drops below 3 minutes. + */ + secondsThreshold: number; +} + +export class NotificationRepository { + public async getAllNotificationsForShuttleAndStopId(shuttleId: string, stopId: string) { + + } + + public async getSecondsThresholdForNotificationIfExists(lookupArguments: NotificationLookupArguments) { + + } + + public async addNotification(notification: ScheduledNotification) { + + } + + public async deleteNotification(lookupArguments: NotificationLookupArguments) { + + } +} diff --git a/src/resolvers/MutationResolvers.ts b/src/resolvers/MutationResolvers.ts index ac8e1ea..f512ed5 100644 --- a/src/resolvers/MutationResolvers.ts +++ b/src/resolvers/MutationResolvers.ts @@ -2,7 +2,7 @@ import { NotificationResponse, Resolvers } from "../generated/graphql"; import { ServerContext } from "../ServerContext"; import { ETANotificationScheduler, - NotificationSchedulingArguments + ScheduledNotification } from "../notifications/schedulers/ETANotificationScheduler"; export const MutationResolvers: Resolvers = { diff --git a/test/resolvers/QueryResolverTests.test.ts b/test/resolvers/QueryResolverTests.test.ts index 00007a7..dc36f79 100644 --- a/test/resolvers/QueryResolverTests.test.ts +++ b/test/resolvers/QueryResolverTests.test.ts @@ -2,7 +2,7 @@ import { describe, expect, it } from "@jest/globals"; import { generateMockSystems } from "../testHelpers/mockDataGenerators"; import { setupTestServerContext, setupTestServerHolder } from "../testHelpers/apolloTestServerHelpers"; import assert = require("node:assert"); -import { NotificationSchedulingArguments } from "../../src/notifications/schedulers/ETANotificationScheduler"; +import { ScheduledNotification } from "../../src/notifications/schedulers/ETANotificationScheduler"; import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers"; // See Apollo documentation for integration test guide