add eta subscriber callback code

This commit is contained in:
2025-02-03 21:37:20 -08:00
parent fba021d921
commit e96316aa1e

View File

@@ -3,6 +3,7 @@ import * as crypto from "node:crypto";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import fs from "fs"; import fs from "fs";
import { TupleKey } from "../types/TupleKey"; import { TupleKey } from "../types/TupleKey";
import { IEta } from "../entities/entities";
interface ScheduledNotificationData { interface ScheduledNotificationData {
deviceId: string; deviceId: string;
@@ -62,13 +63,29 @@ export class NotificationService {
return this._lastRefreshedTimeMs && Date.now() - this._lastRefreshedTimeMs < thirtyMinutesMs; return this._lastRefreshedTimeMs && Date.now() - this._lastRefreshedTimeMs < thirtyMinutesMs;
} }
private sendEtaNotificationImmediately({ deviceId, shuttleId, stopId }: ScheduledNotificationData) { private async sendEtaNotificationImmediately({ deviceId, shuttleId, stopId }: ScheduledNotificationData): Promise<boolean> {
// Construct the fetch request // Construct the fetch request
// Send the fetch request // Send the fetch request
// Check whether it was successful // Check whether it was successful
// Return the result // Return the result
return false;
}
private async etaSubscriberCallback(eta: IEta) {
const tuple = new TupleKey(eta.shuttleId, eta.stopId);
if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) {
return;
}
await Promise.all(this.deviceIdsToDeliverTo[tuple.toString()].map(async (deviceId) => {
await this.sendEtaNotificationImmediately({
deviceId,
shuttleId: eta.shuttleId,
stopId: eta.stopId,
});
}));
} }
/** /**
@@ -78,7 +95,6 @@ export class NotificationService {
* @param stopId Stop ID of ETA object to check. * @param stopId Stop ID of ETA object to check.
*/ */
public async scheduleNotification({ deviceId, shuttleId, stopId }: ScheduledNotificationData) { public async scheduleNotification({ deviceId, shuttleId, stopId }: ScheduledNotificationData) {
// Associate TupleKey(shuttleId, stopId) with array of device IDs
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()] = [deviceId]; this.deviceIdsToDeliverTo[tuple.toString()] = [deviceId];
@@ -87,6 +103,8 @@ export class NotificationService {
} }
// Refresh the subscriber with the updated array if needed // Refresh the subscriber with the updated array if needed
this.repository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback);
this.repository.subscribeToEtaUpdates(this.etaSubscriberCallback);
// In the subscriber callback: // In the subscriber callback:
// If the ETA matches, call sendNotification with the necessary parameters // If the ETA matches, call sendNotification with the necessary parameters