mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-19 08:50:29 +00:00
Update interface and implementation of ETANotificationScheduler
Rely on both the ETA repository and the shuttle repository
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
import { ShuttleGetterRepository, ShuttleRepositoryEvent } from "../../repositories/shuttle/ShuttleGetterRepository";
|
|
||||||
import { IEta } from "../../entities/ShuttleRepositoryEntities";
|
import { IEta } from "../../entities/ShuttleRepositoryEntities";
|
||||||
import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender";
|
import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender";
|
||||||
import {
|
import {
|
||||||
@@ -6,11 +5,14 @@ import {
|
|||||||
ScheduledNotification
|
ScheduledNotification
|
||||||
} from "../../repositories/notifications/NotificationRepository";
|
} from "../../repositories/notifications/NotificationRepository";
|
||||||
import { InMemoryNotificationRepository } from "../../repositories/notifications/InMemoryNotificationRepository";
|
import { InMemoryNotificationRepository } from "../../repositories/notifications/InMemoryNotificationRepository";
|
||||||
|
import { ETAGetterRepository, ETARepositoryEvent } from "../../repositories/shuttle/eta/ETAGetterRepository";
|
||||||
|
import { ShuttleGetterRepository } from "../../repositories/shuttle/ShuttleGetterRepository";
|
||||||
|
|
||||||
export class ETANotificationScheduler {
|
export class ETANotificationScheduler {
|
||||||
public static readonly defaultSecondsThresholdForNotificationToFire = 180;
|
public static readonly defaultSecondsThresholdForNotificationToFire = 180;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
|
private etaRepository: ETAGetterRepository,
|
||||||
private shuttleRepository: ShuttleGetterRepository,
|
private shuttleRepository: ShuttleGetterRepository,
|
||||||
private notificationRepository: NotificationRepository = new InMemoryNotificationRepository(),
|
private notificationRepository: NotificationRepository = new InMemoryNotificationRepository(),
|
||||||
private appleNotificationSender = new AppleNotificationSender(),
|
private appleNotificationSender = new AppleNotificationSender(),
|
||||||
@@ -26,7 +28,7 @@ export class ETANotificationScheduler {
|
|||||||
|
|
||||||
const shuttle = await this.shuttleRepository.getShuttleById(shuttleId);
|
const shuttle = await this.shuttleRepository.getShuttleById(shuttleId);
|
||||||
const stop = await this.shuttleRepository.getStopById(stopId);
|
const stop = await this.shuttleRepository.getStopById(stopId);
|
||||||
const eta = await this.shuttleRepository.getEtaForShuttleAndStopId(shuttleId, stopId);
|
const eta = await this.etaRepository.getEtaForShuttleAndStopId(shuttleId, stopId);
|
||||||
if (!shuttle) {
|
if (!shuttle) {
|
||||||
console.warn(`Notification ${notificationData} fell through; no associated shuttle`);
|
console.warn(`Notification ${notificationData} fell through; no associated shuttle`);
|
||||||
return false;
|
return false;
|
||||||
@@ -90,10 +92,10 @@ export class ETANotificationScheduler {
|
|||||||
|
|
||||||
// The following is a workaround for the constructor being called twice
|
// The following is a workaround for the constructor being called twice
|
||||||
public startListeningForUpdates() {
|
public startListeningForUpdates() {
|
||||||
this.shuttleRepository.on(ShuttleRepositoryEvent.ETA_UPDATED, this.etaSubscriberCallback);
|
this.etaRepository.on(ETARepositoryEvent.ETA_UPDATED, this.etaSubscriberCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public stopListeningForUpdates() {
|
public stopListeningForUpdates() {
|
||||||
this.shuttleRepository.off(ShuttleRepositoryEvent.ETA_UPDATED, this.etaSubscriberCallback);
|
this.etaRepository.off(ETARepositoryEvent.ETA_UPDATED, this.etaSubscriberCallback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
||||||
import { ETANotificationScheduler } from "../ETANotificationScheduler";
|
import { ETANotificationScheduler } from "../ETANotificationScheduler";
|
||||||
import { UnoptimizedInMemoryShuttleRepository } from "../../../repositories/shuttle/UnoptimizedInMemoryShuttleRepository";
|
import { UnoptimizedInMemoryShuttleRepository } from "../../../repositories/shuttle/UnoptimizedInMemoryShuttleRepository";
|
||||||
|
import { InMemoryExternalSourceETARepository } from "../../../repositories/shuttle/eta/InMemoryExternalSourceETARepository";
|
||||||
import { IEta, IShuttle, IStop } from "../../../entities/ShuttleRepositoryEntities";
|
import { IEta, IShuttle, IStop } from "../../../entities/ShuttleRepositoryEntities";
|
||||||
import { addMockShuttleToRepository, addMockStopToRepository } from "../../../../testHelpers/repositorySetupHelpers";
|
import { addMockShuttleToRepository, addMockStopToRepository } from "../../../../testHelpers/repositorySetupHelpers";
|
||||||
import { AppleNotificationSender } from "../../senders/AppleNotificationSender";
|
import { AppleNotificationSender } from "../../senders/AppleNotificationSender";
|
||||||
@@ -26,18 +27,21 @@ async function waitForMilliseconds(ms: number): Promise<void> {
|
|||||||
|
|
||||||
|
|
||||||
describe("ETANotificationScheduler", () => {
|
describe("ETANotificationScheduler", () => {
|
||||||
let shuttleRepository: UnoptimizedInMemoryShuttleRepository
|
let shuttleRepository: UnoptimizedInMemoryShuttleRepository;
|
||||||
|
let etaRepository: InMemoryExternalSourceETARepository;
|
||||||
let notificationService: ETANotificationScheduler;
|
let notificationService: ETANotificationScheduler;
|
||||||
let notificationRepository: NotificationRepository;
|
let notificationRepository: NotificationRepository;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
shuttleRepository = new UnoptimizedInMemoryShuttleRepository();
|
shuttleRepository = new UnoptimizedInMemoryShuttleRepository();
|
||||||
notificationRepository = new InMemoryNotificationRepository();
|
notificationRepository = new InMemoryNotificationRepository();
|
||||||
|
etaRepository = new InMemoryExternalSourceETARepository();
|
||||||
|
|
||||||
mockNotificationSenderMethods(true);
|
mockNotificationSenderMethods(true);
|
||||||
|
|
||||||
const appleNotificationSender = new MockAppleNotificationSender(false);
|
const appleNotificationSender = new MockAppleNotificationSender(false);
|
||||||
notificationService = new ETANotificationScheduler(
|
notificationService = new ETANotificationScheduler(
|
||||||
|
etaRepository,
|
||||||
shuttleRepository,
|
shuttleRepository,
|
||||||
notificationRepository,
|
notificationRepository,
|
||||||
appleNotificationSender,
|
appleNotificationSender,
|
||||||
@@ -127,6 +131,7 @@ describe("ETANotificationScheduler", () => {
|
|||||||
mockNotificationSenderMethods(false);
|
mockNotificationSenderMethods(false);
|
||||||
const updatedNotificationSender = new MockAppleNotificationSender(false);
|
const updatedNotificationSender = new MockAppleNotificationSender(false);
|
||||||
notificationService = new ETANotificationScheduler(
|
notificationService = new ETANotificationScheduler(
|
||||||
|
etaRepository,
|
||||||
shuttleRepository,
|
shuttleRepository,
|
||||||
notificationRepository,
|
notificationRepository,
|
||||||
updatedNotificationSender,
|
updatedNotificationSender,
|
||||||
|
|||||||
Reference in New Issue
Block a user