diff --git a/src/index.ts b/src/index.ts index fb42543..2f0f6bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { TimedApiBasedRepositoryLoader } from "./loaders/TimedApiBasedRepository import { ETANotificationScheduler } from "./notifications/schedulers/ETANotificationScheduler"; import { configDotenv } from "dotenv"; import { loadTestData } from "./loaders/loadTestData"; +import { AppleNotificationSender } from "./notifications/senders/AppleNotificationSender"; configDotenv(); @@ -24,7 +25,8 @@ async function main() { let notificationService: ETANotificationScheduler; if (process.argv.length > 2 && process.argv[2] == "integration-testing") { await loadTestData(repository); - notificationService = new ETANotificationScheduler(repository, false); + const appleNotificationSender = new AppleNotificationSender(false); + notificationService = new ETANotificationScheduler(repository, appleNotificationSender); } else { const repositoryDataUpdater = new TimedApiBasedRepositoryLoader( repository @@ -32,7 +34,6 @@ async function main() { await repositoryDataUpdater.start(); notificationService = new ETANotificationScheduler(repository); } - notificationService.reloadAPNsTokenIfTimePassed(); const { url } = await startStandaloneServer(server, { listen: { diff --git a/src/notifications/schedulers/ETANotificationScheduler.ts b/src/notifications/schedulers/ETANotificationScheduler.ts index ac63f5c..4ccf110 100644 --- a/src/notifications/schedulers/ETANotificationScheduler.ts +++ b/src/notifications/schedulers/ETANotificationScheduler.ts @@ -1,9 +1,6 @@ import { GetterRepository } from "../../repositories/GetterRepository"; -import jwt from "jsonwebtoken"; -import fs from "fs"; import { TupleKey } from "../../types/TupleKey"; import { IEta } from "../../entities/entities"; -import http2 from "http2"; import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender"; export interface ScheduledNotificationData { @@ -16,7 +13,6 @@ export class ETANotificationScheduler { public readonly secondsThresholdForNotificationToFire = 180; constructor(private repository: GetterRepository, - private shouldActuallySendNotifications = true, private appleNotificationSender = new AppleNotificationSender() ) { this.etaSubscriberCallback = this.etaSubscriberCallback.bind(this); @@ -35,10 +31,6 @@ export class ETANotificationScheduler { private deviceIdsToDeliverTo: { [key: string]: Set } = {} private async sendEtaNotificationImmediately(notificationData: ScheduledNotificationData): Promise { - if (!this.shouldActuallySendNotifications) { - return true; // pretend that the notification sent - } - const { deviceId, shuttleId, stopId } = notificationData; const shuttle = await this.repository.getShuttleById(shuttleId); diff --git a/src/notifications/senders/AppleNotificationSender.ts b/src/notifications/senders/AppleNotificationSender.ts index 7d358b1..80c6f2a 100644 --- a/src/notifications/senders/AppleNotificationSender.ts +++ b/src/notifications/senders/AppleNotificationSender.ts @@ -16,6 +16,9 @@ export class AppleNotificationSender { private apnsToken: string | undefined = undefined; private _lastRefreshedTimeMs: number | undefined = undefined; + constructor(private shouldActuallySendNotifications = true) { + } + get lastRefreshedTimeMs(): number | undefined { return this._lastRefreshedTimeMs; } @@ -64,6 +67,11 @@ export class AppleNotificationSender { * notification was sent successfully. */ public async sendNotificationImmediately(deviceId: string, notificationAlertArguments: NotificationAlertArguments) { + if (!this.shouldActuallySendNotifications) { + // pretend that the notification sent + return true; + } + this.reloadAPNsTokenIfTimePassed(); const bundleId = process.env.APNS_BUNDLE_ID;