add logic to send notification based on a provided threshold

This commit is contained in:
2025-03-25 15:19:09 -07:00
parent 1f8d4bfbd4
commit fb49577cf6
3 changed files with 37 additions and 28 deletions

View File

@@ -3,27 +3,26 @@ import { TupleKey } from "../../types/TupleKey";
import { IEta } from "../../entities/entities"; import { IEta } from "../../entities/entities";
import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender"; import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender";
export interface ScheduledNotificationData { export interface NotificationLookupArguments {
deviceId: string; deviceId: string;
shuttleId: string; shuttleId: string;
stopId: string; stopId: string;
}
export interface NotificationSchedulingArguments extends NotificationLookupArguments {
/** /**
* Value which specifies the ETA of the shuttle for when * Value which specifies the ETA of the shuttle for when
* the notification should fire. * the notification should fire.
* For example, a secondsThreshold of 180 would mean that the notification * For example, a secondsThreshold of 180 would mean that the notification
* fires when the ETA drops below 3 minutes. * fires when the ETA drops below 3 minutes.
*
* Optional to retain backwards compatibility with devices on the
* older API version.
*/ */
secondsThreshold?: number; secondsThreshold: number;
} }
type DeviceIdSecondsThresholdAssociation = { [key: string]: number }; type DeviceIdSecondsThresholdAssociation = { [key: string]: number };
export class ETANotificationScheduler { export class ETANotificationScheduler {
public readonly defaultSecondsThresholdForNotificationToFire = 180; public static readonly defaultSecondsThresholdForNotificationToFire = 180;
constructor(private repository: GetterRepository, constructor(private repository: GetterRepository,
private appleNotificationSender = new AppleNotificationSender() private appleNotificationSender = new AppleNotificationSender()
@@ -44,7 +43,7 @@ export class ETANotificationScheduler {
*/ */
private deviceIdsToDeliverTo: { [key: string]: DeviceIdSecondsThresholdAssociation } = {} private deviceIdsToDeliverTo: { [key: string]: DeviceIdSecondsThresholdAssociation } = {}
private async sendEtaNotificationImmediately(notificationData: ScheduledNotificationData): Promise<boolean> { private async sendEtaNotificationImmediately(notificationData: NotificationSchedulingArguments): Promise<boolean> {
const { deviceId, shuttleId, stopId } = notificationData; const { deviceId, shuttleId, stopId } = notificationData;
const shuttle = await this.repository.getShuttleById(shuttleId); const shuttle = await this.repository.getShuttleById(shuttleId);
@@ -82,7 +81,14 @@ export class ETANotificationScheduler {
const deviceIdsToRemove = new Set<string>(); const deviceIdsToRemove = new Set<string>();
for (let deviceId of Object.keys(this.deviceIdsToDeliverTo[tupleKey])) { for (let deviceId of Object.keys(this.deviceIdsToDeliverTo[tupleKey])) {
const deliveredSuccessfully = await this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId, eta); const scheduledNotificationData: NotificationSchedulingArguments = {
deviceId,
secondsThreshold: this.deviceIdsToDeliverTo[tupleKey][deviceId],
shuttleId: eta.shuttleId,
stopId: eta.stopId,
}
const deliveredSuccessfully = await this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(scheduledNotificationData, eta.secondsRemaining);
if (deliveredSuccessfully) { if (deliveredSuccessfully) {
deviceIdsToRemove.add(deviceId); deviceIdsToRemove.add(deviceId);
} }
@@ -93,16 +99,12 @@ export class ETANotificationScheduler {
}); });
} }
private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId: string, eta: IEta) { private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(notificationObject: NotificationSchedulingArguments, etaSecondsRemaining: number) {
if (eta.secondsRemaining > this.defaultSecondsThresholdForNotificationToFire) { if (etaSecondsRemaining > notificationObject.secondsThreshold) {
return false; return false;
} }
return await this.sendEtaNotificationImmediately({ return await this.sendEtaNotificationImmediately(notificationObject);
deviceId,
shuttleId: eta.shuttleId,
stopId: eta.stopId,
});
} }
/** /**
@@ -113,17 +115,13 @@ export class ETANotificationScheduler {
* @param secondsThreshold Value which specifies the ETA of the shuttle for when * @param secondsThreshold Value which specifies the ETA of the shuttle for when
* the notification should fire. * the notification should fire.
*/ */
public async scheduleNotification({ deviceId, shuttleId, stopId, secondsThreshold }: ScheduledNotificationData) { public async scheduleNotification({ deviceId, shuttleId, stopId, secondsThreshold }: NotificationSchedulingArguments) {
const tuple = new TupleKey(shuttleId, stopId); const tuple = new TupleKey(shuttleId, stopId);
if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) {
this.deviceIdsToDeliverTo[tuple.toString()] = {}; this.deviceIdsToDeliverTo[tuple.toString()] = {};
} }
if (secondsThreshold !== undefined) {
this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = secondsThreshold; this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = secondsThreshold;
} else {
this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = this.defaultSecondsThresholdForNotificationToFire;
}
this.repository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback); this.repository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback);
this.repository.subscribeToEtaUpdates(this.etaSubscriberCallback); this.repository.subscribeToEtaUpdates(this.etaSubscriberCallback);
@@ -135,7 +133,7 @@ export class ETANotificationScheduler {
* @param shuttleId Shuttle ID of the ETA object. * @param shuttleId Shuttle ID of the ETA object.
* @param stopId Stop ID of the ETA object. * @param stopId Stop ID of the ETA object.
*/ */
public async cancelNotificationIfExists({ deviceId, shuttleId, stopId }: ScheduledNotificationData) { public async cancelNotificationIfExists({ deviceId, shuttleId, stopId }: NotificationLookupArguments) {
const tupleKey = new TupleKey(shuttleId, stopId); const tupleKey = new TupleKey(shuttleId, stopId);
if ( if (
this.deviceIdsToDeliverTo[tupleKey.toString()] === undefined this.deviceIdsToDeliverTo[tupleKey.toString()] === undefined
@@ -153,7 +151,7 @@ export class ETANotificationScheduler {
* @param shuttleId * @param shuttleId
* @param stopId * @param stopId
*/ */
public isNotificationScheduled({ deviceId, shuttleId, stopId }: ScheduledNotificationData): boolean { public isNotificationScheduled({ deviceId, shuttleId, stopId }: NotificationLookupArguments): boolean {
const tuple = new TupleKey(shuttleId, stopId); const tuple = new TupleKey(shuttleId, stopId);
if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) {
return false; return false;
@@ -165,19 +163,21 @@ export class ETANotificationScheduler {
* Return all scheduled notification for the given device ID. * Return all scheduled notification for the given device ID.
* @param deviceId * @param deviceId
*/ */
public async getAllScheduledNotificationsForDevice(deviceId: string): Promise<ScheduledNotificationData[]> { public async getAllScheduledNotificationsForDevice(deviceId: string): Promise<NotificationLookupArguments[]> {
const scheduledNotifications: ScheduledNotificationData[] = []; const scheduledNotifications: NotificationSchedulingArguments[] = [];
for (const key of Object.keys(this.deviceIdsToDeliverTo)) { for (const key of Object.keys(this.deviceIdsToDeliverTo)) {
if (deviceId in this.deviceIdsToDeliverTo[key]) { if (deviceId in this.deviceIdsToDeliverTo[key]) {
const tupleKey = TupleKey.fromExistingStringKey(key); const tupleKey = TupleKey.fromExistingStringKey(key);
const shuttleId = tupleKey.tuple[0] const shuttleId = tupleKey.tuple[0]
const stopId = tupleKey.tuple[1]; const stopId = tupleKey.tuple[1];
const secondsThreshold = this.deviceIdsToDeliverTo[key][deviceId];
scheduledNotifications.push({ scheduledNotifications.push({
shuttleId, shuttleId,
stopId, stopId,
deviceId, deviceId,
secondsThreshold,
}); });
} }
} }

View File

@@ -1,5 +1,9 @@
import { NotificationInput, NotificationResponse, Resolvers } from "../generated/graphql"; import { NotificationInput, NotificationResponse, Resolvers } from "../generated/graphql";
import { ServerContext } from "../ServerContext"; import { ServerContext } from "../ServerContext";
import {
ETANotificationScheduler,
NotificationSchedulingArguments
} from "../notifications/schedulers/ETANotificationScheduler";
export const MutationResolvers: Resolvers<ServerContext> = { export const MutationResolvers: Resolvers<ServerContext> = {
Mutation: { Mutation: {
@@ -19,7 +23,12 @@ export const MutationResolvers: Resolvers<ServerContext> = {
} }
} }
await context.notificationService.scheduleNotification(args.input); const notificationData: NotificationSchedulingArguments = {
...args.input,
secondsThreshold: ETANotificationScheduler.defaultSecondsThresholdForNotificationToFire,
}
await context.notificationService.scheduleNotification(notificationData);
const response: NotificationResponse = { const response: NotificationResponse = {
message: "Notification scheduled", message: "Notification scheduled",

View File

@@ -2,7 +2,7 @@ import { describe, expect, it } from "@jest/globals";
import { generateMockSystems } from "../testHelpers/mockDataGenerators"; import { generateMockSystems } from "../testHelpers/mockDataGenerators";
import { setupTestServerContext, setupTestServerHolder } from "../testHelpers/apolloTestServerHelpers"; import { setupTestServerContext, setupTestServerHolder } from "../testHelpers/apolloTestServerHelpers";
import assert = require("node:assert"); import assert = require("node:assert");
import { ScheduledNotificationData } from "../../src/notifications/schedulers/ETANotificationScheduler"; import { NotificationSchedulingArguments } from "../../src/notifications/schedulers/ETANotificationScheduler";
import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers"; import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers";
// See Apollo documentation for integration test guide // See Apollo documentation for integration test guide
@@ -108,7 +108,7 @@ describe("QueryResolvers", () => {
const shuttle = await addMockShuttleToRepository(context.repository, "1"); const shuttle = await addMockShuttleToRepository(context.repository, "1");
const stop = await addMockStopToRepository(context.repository, "1") const stop = await addMockStopToRepository(context.repository, "1")
const notification: ScheduledNotificationData = { const notification: NotificationSchedulingArguments = {
shuttleId: shuttle.id, shuttleId: shuttle.id,
stopId: stop.id, stopId: stop.id,
deviceId: "1", deviceId: "1",