From f007b72d94563be1803c97cfcb717bfe65a8e5cf Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 27 Mar 2025 10:19:00 -0700 Subject: [PATCH] add code for in-memory notification repository --- .../schedulers/ETANotificationScheduler.ts | 5 -- .../InMemoryNotificationRepository.ts | 65 +++++++++++++++++-- src/repositories/NotificationRepository.ts | 2 +- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/notifications/schedulers/ETANotificationScheduler.ts b/src/notifications/schedulers/ETANotificationScheduler.ts index 0319cf7..385d350 100644 --- a/src/notifications/schedulers/ETANotificationScheduler.ts +++ b/src/notifications/schedulers/ETANotificationScheduler.ts @@ -16,11 +16,6 @@ export class ETANotificationScheduler { this.sendEtaNotificationImmediately = this.sendEtaNotificationImmediately.bind(this); this.etaSubscriberCallback = this.etaSubscriberCallback.bind(this); this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold = this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold.bind(this); - this.scheduleNotification = this.scheduleNotification.bind(this); - this.cancelNotificationIfExists = this.cancelNotificationIfExists.bind(this); - this.isNotificationScheduled = this.isNotificationScheduled.bind(this); - this.getSecondsThresholdForScheduledNotification = this.getSecondsThresholdForScheduledNotification.bind(this); - this.getAllScheduledNotificationsForDevice = this.getAllScheduledNotificationsForDevice.bind(this); } /** diff --git a/src/repositories/InMemoryNotificationRepository.ts b/src/repositories/InMemoryNotificationRepository.ts index c660ca7..db6e9bc 100644 --- a/src/repositories/InMemoryNotificationRepository.ts +++ b/src/repositories/InMemoryNotificationRepository.ts @@ -1,19 +1,74 @@ import { NotificationLookupArguments, NotificationRepository, ScheduledNotification } from "./NotificationRepository"; +import { TupleKey } from "../types/TupleKey"; + +type DeviceIdSecondsThresholdAssociation = { [key: string]: number }; export class InMemoryNotificationRepository implements NotificationRepository { + /** + * 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 } = {} + async getAllNotificationsForShuttleAndStopId(shuttleId: string, stopId: string) { - return []; + const tuple = new TupleKey(shuttleId, stopId); + if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { + return []; + } + + return Object.keys(this.deviceIdsToDeliverTo[tuple.toString()]) + .map((deviceId) => { + return { + shuttleId, + stopId, + deviceId, + secondsThreshold: this.deviceIdsToDeliverTo[tuple.toString()][deviceId] + } + }); } - async getSecondsThresholdForNotificationIfExists(lookupArguments: NotificationLookupArguments) { - return 0; + async getSecondsThresholdForNotificationIfExists({ + shuttleId, + stopId, + deviceId + }: NotificationLookupArguments) { + const tuple = new TupleKey(shuttleId, stopId); + if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { + return null; + } + return this.deviceIdsToDeliverTo[tuple.toString()][deviceId]; } - async addOrUpdateNotification(notification: ScheduledNotification) { + async addOrUpdateNotification({ + shuttleId, + stopId, + deviceId, + secondsThreshold + }: ScheduledNotification) { + const tuple = new TupleKey(shuttleId, stopId); + if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { + this.deviceIdsToDeliverTo[tuple.toString()] = {}; + } + this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = secondsThreshold; } - async deleteNotificationIfExists(lookupArguments: NotificationLookupArguments) { + async deleteNotificationIfExists({ + 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]; } } diff --git a/src/repositories/NotificationRepository.ts b/src/repositories/NotificationRepository.ts index 3787f3d..343497e 100644 --- a/src/repositories/NotificationRepository.ts +++ b/src/repositories/NotificationRepository.ts @@ -16,7 +16,7 @@ export interface ScheduledNotification extends NotificationLookupArguments { export interface NotificationRepository { getAllNotificationsForShuttleAndStopId(shuttleId: string, stopId: string): Promise; - getSecondsThresholdForNotificationIfExists(lookupArguments: NotificationLookupArguments): Promise; + getSecondsThresholdForNotificationIfExists(lookupArguments: NotificationLookupArguments): Promise; addOrUpdateNotification(notification: ScheduledNotification): Promise; deleteNotificationIfExists(lookupArguments: NotificationLookupArguments): Promise; }