add seconds threshold for notification

This commit is contained in:
2025-02-03 22:47:17 -08:00
parent d357af7509
commit 16d2d4b4ef

View File

@@ -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.