add code for in-memory notification repository

This commit is contained in:
2025-03-27 10:19:00 -07:00
parent ead401a3b1
commit f007b72d94
3 changed files with 61 additions and 11 deletions

View File

@@ -16,11 +16,6 @@ export class ETANotificationScheduler {
this.sendEtaNotificationImmediately = this.sendEtaNotificationImmediately.bind(this); this.sendEtaNotificationImmediately = this.sendEtaNotificationImmediately.bind(this);
this.etaSubscriberCallback = this.etaSubscriberCallback.bind(this); this.etaSubscriberCallback = this.etaSubscriberCallback.bind(this);
this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold = this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold.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);
} }
/** /**

View File

@@ -1,19 +1,74 @@
import { NotificationLookupArguments, NotificationRepository, ScheduledNotification } from "./NotificationRepository"; import { NotificationLookupArguments, NotificationRepository, ScheduledNotification } from "./NotificationRepository";
import { TupleKey } from "../types/TupleKey";
type DeviceIdSecondsThresholdAssociation = { [key: string]: number };
export class InMemoryNotificationRepository implements NotificationRepository { 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) { 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) { async getSecondsThresholdForNotificationIfExists({
return 0; 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];
} }
} }

View File

@@ -16,7 +16,7 @@ export interface ScheduledNotification extends NotificationLookupArguments {
export interface NotificationRepository { export interface NotificationRepository {
getAllNotificationsForShuttleAndStopId(shuttleId: string, stopId: string): Promise<ScheduledNotification[]>; getAllNotificationsForShuttleAndStopId(shuttleId: string, stopId: string): Promise<ScheduledNotification[]>;
getSecondsThresholdForNotificationIfExists(lookupArguments: NotificationLookupArguments): Promise<number>; getSecondsThresholdForNotificationIfExists(lookupArguments: NotificationLookupArguments): Promise<number | null>;
addOrUpdateNotification(notification: ScheduledNotification): Promise<void>; addOrUpdateNotification(notification: ScheduledNotification): Promise<void>;
deleteNotificationIfExists(lookupArguments: NotificationLookupArguments): Promise<void>; deleteNotificationIfExists(lookupArguments: NotificationLookupArguments): Promise<void>;
} }