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 { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender";
export interface ScheduledNotificationData {
export interface NotificationLookupArguments {
deviceId: string;
shuttleId: string;
stopId: string;
}
export interface NotificationSchedulingArguments extends NotificationLookupArguments {
/**
* Value which specifies the ETA of the shuttle for when
* the notification should fire.
* For example, a secondsThreshold of 180 would mean that the notification
* 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 };
export class ETANotificationScheduler {
public readonly defaultSecondsThresholdForNotificationToFire = 180;
public static readonly defaultSecondsThresholdForNotificationToFire = 180;
constructor(private repository: GetterRepository,
private appleNotificationSender = new AppleNotificationSender()
@@ -44,7 +43,7 @@ export class ETANotificationScheduler {
*/
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 shuttle = await this.repository.getShuttleById(shuttleId);
@@ -82,7 +81,14 @@ export class ETANotificationScheduler {
const deviceIdsToRemove = new Set<string>();
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) {
deviceIdsToRemove.add(deviceId);
}
@@ -93,16 +99,12 @@ export class ETANotificationScheduler {
});
}
private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId: string, eta: IEta) {
if (eta.secondsRemaining > this.defaultSecondsThresholdForNotificationToFire) {
private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(notificationObject: NotificationSchedulingArguments, etaSecondsRemaining: number) {
if (etaSecondsRemaining > notificationObject.secondsThreshold) {
return false;
}
return await this.sendEtaNotificationImmediately({
deviceId,
shuttleId: eta.shuttleId,
stopId: eta.stopId,
});
return await this.sendEtaNotificationImmediately(notificationObject);
}
/**
@@ -113,17 +115,13 @@ export class ETANotificationScheduler {
* @param secondsThreshold Value which specifies the ETA of the shuttle for when
* 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);
if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) {
this.deviceIdsToDeliverTo[tuple.toString()] = {};
}
if (secondsThreshold !== undefined) {
this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = secondsThreshold;
} else {
this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = this.defaultSecondsThresholdForNotificationToFire;
}
this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = secondsThreshold;
this.repository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback);
this.repository.subscribeToEtaUpdates(this.etaSubscriberCallback);
@@ -135,7 +133,7 @@ export class ETANotificationScheduler {
* @param shuttleId Shuttle 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);
if (
this.deviceIdsToDeliverTo[tupleKey.toString()] === undefined
@@ -153,7 +151,7 @@ export class ETANotificationScheduler {
* @param shuttleId
* @param stopId
*/
public isNotificationScheduled({ deviceId, shuttleId, stopId }: ScheduledNotificationData): boolean {
public isNotificationScheduled({ deviceId, shuttleId, stopId }: NotificationLookupArguments): boolean {
const tuple = new TupleKey(shuttleId, stopId);
if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) {
return false;
@@ -165,19 +163,21 @@ export class ETANotificationScheduler {
* Return all scheduled notification for the given device ID.
* @param deviceId
*/
public async getAllScheduledNotificationsForDevice(deviceId: string): Promise<ScheduledNotificationData[]> {
const scheduledNotifications: ScheduledNotificationData[] = [];
public async getAllScheduledNotificationsForDevice(deviceId: string): Promise<NotificationLookupArguments[]> {
const scheduledNotifications: NotificationSchedulingArguments[] = [];
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,
});
}
}

View File

@@ -1,5 +1,9 @@
import { NotificationInput, NotificationResponse, Resolvers } from "../generated/graphql";
import { ServerContext } from "../ServerContext";
import {
ETANotificationScheduler,
NotificationSchedulingArguments
} from "../notifications/schedulers/ETANotificationScheduler";
export const MutationResolvers: Resolvers<ServerContext> = {
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 = {
message: "Notification scheduled",