mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
add code for in-memory notification repository
This commit is contained in:
@@ -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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
const tuple = new TupleKey(shuttleId, stopId);
|
||||||
|
if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSecondsThresholdForNotificationIfExists(lookupArguments: NotificationLookupArguments) {
|
return Object.keys(this.deviceIdsToDeliverTo[tuple.toString()])
|
||||||
return 0;
|
.map((deviceId) => {
|
||||||
|
return {
|
||||||
|
shuttleId,
|
||||||
|
stopId,
|
||||||
|
deviceId,
|
||||||
|
secondsThreshold: this.deviceIdsToDeliverTo[tuple.toString()][deviceId]
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async addOrUpdateNotification(notification: ScheduledNotification) {
|
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 deleteNotificationIfExists(lookupArguments: NotificationLookupArguments) {
|
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({
|
||||||
|
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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user