From bba00eb0672cc8b7b2d36fd81c8f7e57a5bb8f8a Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 27 Mar 2025 10:30:12 -0700 Subject: [PATCH] remove everything from the scheduler that's in the repository already --- .../schedulers/ETANotificationScheduler.ts | 120 ++---------------- 1 file changed, 12 insertions(+), 108 deletions(-) diff --git a/src/notifications/schedulers/ETANotificationScheduler.ts b/src/notifications/schedulers/ETANotificationScheduler.ts index 0f875af..bb820b9 100644 --- a/src/notifications/schedulers/ETANotificationScheduler.ts +++ b/src/notifications/schedulers/ETANotificationScheduler.ts @@ -1,16 +1,12 @@ import { ShuttleGetterRepository } from "../../repositories/ShuttleGetterRepository"; -import { TupleKey } from "../../types/TupleKey"; import { IEta } from "../../entities/entities"; import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender"; import { - NotificationLookupArguments, NotificationRepository, ScheduledNotification } from "../../repositories/NotificationRepository"; import { InMemoryNotificationRepository } from "../../repositories/InMemoryNotificationRepository"; -type DeviceIdSecondsThresholdAssociation = { [key: string]: number }; - export class ETANotificationScheduler { public static readonly defaultSecondsThresholdForNotificationToFire = 180; @@ -25,15 +21,6 @@ export class ETANotificationScheduler { this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold = this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold.bind(this); } - /** - * An object of device ID arrays to deliver notifications to. - * The key should be a combination of the shuttle ID and - * stop ID, which can be generated using `TupleKey`. - * The value is a dictionary of the device ID to the stored seconds threshold. - * @private - */ - private deviceIdsToDeliverTo: { [key: string]: DeviceIdSecondsThresholdAssociation } = {} - private async sendEtaNotificationImmediately(notificationData: ScheduledNotification): Promise { const { deviceId, shuttleId, stopId } = notificationData; @@ -64,29 +51,25 @@ export class ETANotificationScheduler { } private async etaSubscriberCallback(eta: IEta) { - const tuple = new TupleKey(eta.shuttleId, eta.stopId); - const tupleKey = tuple.toString(); - if (this.deviceIdsToDeliverTo[tupleKey] === undefined) { - return; - } - const deviceIdsToRemove = new Set(); - for (let deviceId of Object.keys(this.deviceIdsToDeliverTo[tupleKey])) { - const scheduledNotificationData: ScheduledNotification = { - deviceId, - secondsThreshold: this.deviceIdsToDeliverTo[tupleKey][deviceId], - shuttleId: eta.shuttleId, - stopId: eta.stopId, - } + const notifications = await this.notificationRepository.getAllNotificationsForShuttleAndStopId( + eta.shuttleId, + eta.stopId + ) - const deliveredSuccessfully = await this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(scheduledNotificationData, eta.secondsRemaining); + for (let notification of notifications) { + const deliveredSuccessfully = await this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(notification, eta.secondsRemaining); if (deliveredSuccessfully) { - deviceIdsToRemove.add(deviceId); + deviceIdsToRemove.add(notification.deviceId); } } deviceIdsToRemove.forEach((deviceId) => { - delete this.deviceIdsToDeliverTo[tupleKey][deviceId] + this.notificationRepository.deleteNotificationIfExists({ + shuttleId: eta.shuttleId, + stopId: eta.stopId, + deviceId, + }) }); } @@ -97,83 +80,4 @@ export class ETANotificationScheduler { return await this.sendEtaNotificationImmediately(notificationObject); } - - /** - * Queue a notification to be sent. - * @param deviceId The device ID to send the notification to. - * @param shuttleId Shuttle ID of ETA object to check. - * @param stopId Stop ID of ETA object to check. - * @param secondsThreshold Value which specifies the ETA of the shuttle for when - * the notification should fire. - */ - public async scheduleNotification({ deviceId, shuttleId, stopId, secondsThreshold }: ScheduledNotification) { - const tuple = new TupleKey(shuttleId, stopId); - if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { - this.deviceIdsToDeliverTo[tuple.toString()] = {}; - } - - this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = secondsThreshold; - - this.shuttleRepository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback); - this.shuttleRepository.subscribeToEtaUpdates(this.etaSubscriberCallback); - } - - /** - * Cancel a pending notification. - * @param deviceId The device ID of the notification. - * @param shuttleId Shuttle ID of the ETA object. - * @param stopId Stop ID of the ETA object. - */ - public async cancelNotificationIfExists({ deviceId, shuttleId, stopId }: NotificationLookupArguments) { - const tupleKey = new TupleKey(shuttleId, stopId); - if ( - this.deviceIdsToDeliverTo[tupleKey.toString()] === undefined - || !(deviceId in this.deviceIdsToDeliverTo[tupleKey.toString()]) - ) { - return; - } - - delete this.deviceIdsToDeliverTo[tupleKey.toString()][deviceId]; - } - - /** - * Check whether the notification is scheduled. - */ - public isNotificationScheduled(lookupArguments: NotificationLookupArguments): boolean { - return this.getSecondsThresholdForScheduledNotification(lookupArguments) != null; - } - - public getSecondsThresholdForScheduledNotification({ deviceId, shuttleId, stopId }: NotificationLookupArguments): number | null { - const tuple = new TupleKey(shuttleId, stopId); - if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { - return null; - } - return this.deviceIdsToDeliverTo[tuple.toString()][deviceId]; - } - - /** - * Return all scheduled notification for the given device ID. - * @param deviceId - */ - public async getAllScheduledNotificationsForDevice(deviceId: string): Promise { - const scheduledNotifications: ScheduledNotification[] = []; - - for (const key of Object.keys(this.deviceIdsToDeliverTo)) { - if (deviceId in this.deviceIdsToDeliverTo[key]) { - const tupleKey = TupleKey.fromExistingStringKey(key); - const shuttleId = tupleKey.tuple[0] - const stopId = tupleKey.tuple[1]; - const secondsThreshold = this.deviceIdsToDeliverTo[key][deviceId]; - - scheduledNotifications.push({ - shuttleId, - stopId, - deviceId, - secondsThreshold, - }); - } - } - - return scheduledNotifications; - } }