From e96316aa1e83824197eb2e28e4d2bcf8beec5908 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 3 Feb 2025 21:37:20 -0800 Subject: [PATCH] add eta subscriber callback code --- src/services/NotificationService.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/services/NotificationService.ts b/src/services/NotificationService.ts index ec388c6..3613803 100644 --- a/src/services/NotificationService.ts +++ b/src/services/NotificationService.ts @@ -3,6 +3,7 @@ import * as crypto from "node:crypto"; import jwt from "jsonwebtoken"; import fs from "fs"; import { TupleKey } from "../types/TupleKey"; +import { IEta } from "../entities/entities"; interface ScheduledNotificationData { deviceId: string; @@ -62,13 +63,29 @@ export class NotificationService { return this._lastRefreshedTimeMs && Date.now() - this._lastRefreshedTimeMs < thirtyMinutesMs; } - private sendEtaNotificationImmediately({ deviceId, shuttleId, stopId }: ScheduledNotificationData) { + private async sendEtaNotificationImmediately({ deviceId, shuttleId, stopId }: ScheduledNotificationData): Promise { // Construct the fetch request // Send the fetch request // Check whether it was successful // 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. */ public async scheduleNotification({ deviceId, shuttleId, stopId }: ScheduledNotificationData) { - // Associate TupleKey(shuttleId, stopId) with array of device IDs const tuple = new TupleKey(shuttleId, stopId); if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { this.deviceIdsToDeliverTo[tuple.toString()] = [deviceId]; @@ -87,6 +103,8 @@ export class NotificationService { } // Refresh the subscriber with the updated array if needed + this.repository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback); + this.repository.subscribeToEtaUpdates(this.etaSubscriberCallback); // In the subscriber callback: // If the ETA matches, call sendNotification with the necessary parameters