diff --git a/src/services/NotificationService.ts b/src/services/NotificationService.ts index defd0f0..2ed9ae9 100644 --- a/src/services/NotificationService.ts +++ b/src/services/NotificationService.ts @@ -1,5 +1,4 @@ import { GetterRepository } from "../repositories/GetterRepository"; -import * as crypto from "node:crypto"; import jwt from "jsonwebtoken"; import fs from "fs"; import { TupleKey } from "../types/TupleKey"; @@ -140,11 +139,7 @@ export class NotificationService { const indicesToRemove = new Set(); await Promise.all(this.deviceIdsToDeliverTo[tuple.toString()].map(async (deviceId, index) => { - const deliveredSuccessfully = await this.sendEtaNotificationImmediately({ - deviceId, - shuttleId: eta.shuttleId, - stopId: eta.stopId, - }); + const deliveredSuccessfully = await this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId, eta); if (deliveredSuccessfully) { indicesToRemove.add(index); } @@ -153,6 +148,19 @@ export class NotificationService { this.deviceIdsToDeliverTo[tuple.toString()] = this.deviceIdsToDeliverTo[tuple.toString()].filter((_, index) => !indicesToRemove.has(index)); } + private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId: string, eta: IEta) { + const secondsThresholdForNotificationToFire = 300; + if (eta.secondsRemaining > secondsThresholdForNotificationToFire) { + return false; + } + + return await this.sendEtaNotificationImmediately({ + deviceId, + shuttleId: eta.shuttleId, + stopId: eta.stopId, + }); + } + /** * Queue a notification to be sent. * @param deviceId The device ID to send the notification to.