mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
move arguments and notification interfaces to notification repository file
This commit is contained in:
@@ -2,22 +2,10 @@ import { ShuttleGetterRepository } from "../../repositories/ShuttleGetterReposit
|
|||||||
import { TupleKey } from "../../types/TupleKey";
|
import { TupleKey } from "../../types/TupleKey";
|
||||||
import { IEta } from "../../entities/entities";
|
import { IEta } from "../../entities/entities";
|
||||||
import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender";
|
import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender";
|
||||||
|
import {
|
||||||
export interface NotificationLookupArguments {
|
NotificationLookupArguments,
|
||||||
deviceId: string;
|
ScheduledNotification
|
||||||
shuttleId: string;
|
} from "../../repositories/NotificationRepository";
|
||||||
stopId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface NotificationSchedulingArguments extends NotificationLookupArguments {
|
|
||||||
/**
|
|
||||||
* Value which specifies the ETA of the shuttle for when
|
|
||||||
* the notification should fire.
|
|
||||||
* For example, a secondsThreshold of 180 would mean that the notification
|
|
||||||
* fires when the ETA drops below 3 minutes.
|
|
||||||
*/
|
|
||||||
secondsThreshold: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
type DeviceIdSecondsThresholdAssociation = { [key: string]: number };
|
type DeviceIdSecondsThresholdAssociation = { [key: string]: number };
|
||||||
|
|
||||||
@@ -47,7 +35,7 @@ export class ETANotificationScheduler {
|
|||||||
*/
|
*/
|
||||||
private deviceIdsToDeliverTo: { [key: string]: DeviceIdSecondsThresholdAssociation } = {}
|
private deviceIdsToDeliverTo: { [key: string]: DeviceIdSecondsThresholdAssociation } = {}
|
||||||
|
|
||||||
private async sendEtaNotificationImmediately(notificationData: NotificationSchedulingArguments): Promise<boolean> {
|
private async sendEtaNotificationImmediately(notificationData: ScheduledNotification): Promise<boolean> {
|
||||||
const { deviceId, shuttleId, stopId } = notificationData;
|
const { deviceId, shuttleId, stopId } = notificationData;
|
||||||
|
|
||||||
const shuttle = await this.shuttleRepository.getShuttleById(shuttleId);
|
const shuttle = await this.shuttleRepository.getShuttleById(shuttleId);
|
||||||
@@ -85,7 +73,7 @@ export class ETANotificationScheduler {
|
|||||||
|
|
||||||
const deviceIdsToRemove = new Set<string>();
|
const deviceIdsToRemove = new Set<string>();
|
||||||
for (let deviceId of Object.keys(this.deviceIdsToDeliverTo[tupleKey])) {
|
for (let deviceId of Object.keys(this.deviceIdsToDeliverTo[tupleKey])) {
|
||||||
const scheduledNotificationData: NotificationSchedulingArguments = {
|
const scheduledNotificationData: ScheduledNotification = {
|
||||||
deviceId,
|
deviceId,
|
||||||
secondsThreshold: this.deviceIdsToDeliverTo[tupleKey][deviceId],
|
secondsThreshold: this.deviceIdsToDeliverTo[tupleKey][deviceId],
|
||||||
shuttleId: eta.shuttleId,
|
shuttleId: eta.shuttleId,
|
||||||
@@ -103,7 +91,7 @@ export class ETANotificationScheduler {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(notificationObject: NotificationSchedulingArguments, etaSecondsRemaining: number) {
|
private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(notificationObject: ScheduledNotification, etaSecondsRemaining: number) {
|
||||||
if (etaSecondsRemaining > notificationObject.secondsThreshold) {
|
if (etaSecondsRemaining > notificationObject.secondsThreshold) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -119,7 +107,7 @@ export class ETANotificationScheduler {
|
|||||||
* @param secondsThreshold Value which specifies the ETA of the shuttle for when
|
* @param secondsThreshold Value which specifies the ETA of the shuttle for when
|
||||||
* the notification should fire.
|
* the notification should fire.
|
||||||
*/
|
*/
|
||||||
public async scheduleNotification({ deviceId, shuttleId, stopId, secondsThreshold }: NotificationSchedulingArguments) {
|
public async scheduleNotification({ deviceId, shuttleId, stopId, secondsThreshold }: ScheduledNotification) {
|
||||||
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()] = {};
|
this.deviceIdsToDeliverTo[tuple.toString()] = {};
|
||||||
@@ -169,7 +157,7 @@ export class ETANotificationScheduler {
|
|||||||
* @param deviceId
|
* @param deviceId
|
||||||
*/
|
*/
|
||||||
public async getAllScheduledNotificationsForDevice(deviceId: string): Promise<NotificationLookupArguments[]> {
|
public async getAllScheduledNotificationsForDevice(deviceId: string): Promise<NotificationLookupArguments[]> {
|
||||||
const scheduledNotifications: NotificationSchedulingArguments[] = [];
|
const scheduledNotifications: ScheduledNotification[] = [];
|
||||||
|
|
||||||
for (const key of Object.keys(this.deviceIdsToDeliverTo)) {
|
for (const key of Object.keys(this.deviceIdsToDeliverTo)) {
|
||||||
if (deviceId in this.deviceIdsToDeliverTo[key]) {
|
if (deviceId in this.deviceIdsToDeliverTo[key]) {
|
||||||
|
|||||||
33
src/repositories/NotificationRepository.ts
Normal file
33
src/repositories/NotificationRepository.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
export interface NotificationLookupArguments {
|
||||||
|
deviceId: string;
|
||||||
|
shuttleId: string;
|
||||||
|
stopId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ScheduledNotification extends NotificationLookupArguments {
|
||||||
|
/**
|
||||||
|
* Value which specifies the ETA of the shuttle for when
|
||||||
|
* the notification should fire.
|
||||||
|
* For example, a secondsThreshold of 180 would mean that the notification
|
||||||
|
* fires when the ETA drops below 3 minutes.
|
||||||
|
*/
|
||||||
|
secondsThreshold: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NotificationRepository {
|
||||||
|
public async getAllNotificationsForShuttleAndStopId(shuttleId: string, stopId: string) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getSecondsThresholdForNotificationIfExists(lookupArguments: NotificationLookupArguments) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async addNotification(notification: ScheduledNotification) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async deleteNotification(lookupArguments: NotificationLookupArguments) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ import { NotificationResponse, Resolvers } from "../generated/graphql";
|
|||||||
import { ServerContext } from "../ServerContext";
|
import { ServerContext } from "../ServerContext";
|
||||||
import {
|
import {
|
||||||
ETANotificationScheduler,
|
ETANotificationScheduler,
|
||||||
NotificationSchedulingArguments
|
ScheduledNotification
|
||||||
} from "../notifications/schedulers/ETANotificationScheduler";
|
} from "../notifications/schedulers/ETANotificationScheduler";
|
||||||
|
|
||||||
export const MutationResolvers: Resolvers<ServerContext> = {
|
export const MutationResolvers: Resolvers<ServerContext> = {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { describe, expect, it } from "@jest/globals";
|
|||||||
import { generateMockSystems } from "../testHelpers/mockDataGenerators";
|
import { generateMockSystems } from "../testHelpers/mockDataGenerators";
|
||||||
import { setupTestServerContext, setupTestServerHolder } from "../testHelpers/apolloTestServerHelpers";
|
import { setupTestServerContext, setupTestServerHolder } from "../testHelpers/apolloTestServerHelpers";
|
||||||
import assert = require("node:assert");
|
import assert = require("node:assert");
|
||||||
import { NotificationSchedulingArguments } from "../../src/notifications/schedulers/ETANotificationScheduler";
|
import { ScheduledNotification } from "../../src/notifications/schedulers/ETANotificationScheduler";
|
||||||
import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers";
|
import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers";
|
||||||
|
|
||||||
// See Apollo documentation for integration test guide
|
// See Apollo documentation for integration test guide
|
||||||
|
|||||||
Reference in New Issue
Block a user