mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
update import for testing
This commit is contained in:
@@ -3,7 +3,7 @@ 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";
|
import { IEta } from "../entities/entities";
|
||||||
import * as http2 from "node:http2";
|
import http2 from "http2";
|
||||||
|
|
||||||
export interface ScheduledNotificationData {
|
export interface ScheduledNotificationData {
|
||||||
deviceId: string;
|
deviceId: string;
|
||||||
@@ -43,7 +43,7 @@ export class NotificationService {
|
|||||||
* stop ID, which can be generated using `TupleKey`.
|
* stop ID, which can be generated using `TupleKey`.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private deviceIdsToDeliverTo: { [key: string]: string[] } = {}
|
private deviceIdsToDeliverTo: { [key: string]: Set<string> } = {}
|
||||||
|
|
||||||
public reloadAPNsTokenIfTimePassed() {
|
public reloadAPNsTokenIfTimePassed() {
|
||||||
if (this.lastReloadedTimeForAPNsIsTooRecent()) {
|
if (this.lastReloadedTimeForAPNsIsTooRecent()) {
|
||||||
@@ -176,15 +176,17 @@ export class NotificationService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const indicesToRemove = new Set();
|
const deviceIdsToRemove = new Set<string>();
|
||||||
await Promise.all(this.deviceIdsToDeliverTo[tuple.toString()].map(async (deviceId, index) => {
|
for (let deviceId of this.deviceIdsToDeliverTo[tuple.toString()].values()) {
|
||||||
const deliveredSuccessfully = await this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId, eta);
|
const deliveredSuccessfully = await this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId, eta);
|
||||||
if (deliveredSuccessfully) {
|
if (deliveredSuccessfully) {
|
||||||
indicesToRemove.add(index);
|
deviceIdsToRemove.add(deviceId);
|
||||||
}
|
}
|
||||||
}));
|
}
|
||||||
|
|
||||||
this.deviceIdsToDeliverTo[tuple.toString()] = this.deviceIdsToDeliverTo[tuple.toString()].filter((_, index) => !indicesToRemove.has(index));
|
deviceIdsToRemove.forEach((deviceId) => {
|
||||||
|
this.deviceIdsToDeliverTo[tuple.toString()].delete(deviceId);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId: string, eta: IEta) {
|
private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId: string, eta: IEta) {
|
||||||
@@ -208,10 +210,9 @@ export class NotificationService {
|
|||||||
public async scheduleNotification({ deviceId, shuttleId, stopId }: ScheduledNotificationData) {
|
public async scheduleNotification({ deviceId, shuttleId, stopId }: ScheduledNotificationData) {
|
||||||
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()] = new Set();
|
||||||
} else {
|
|
||||||
this.deviceIdsToDeliverTo[tuple.toString()].push(deviceId);
|
|
||||||
}
|
}
|
||||||
|
this.deviceIdsToDeliverTo[tuple.toString()].add(deviceId);
|
||||||
|
|
||||||
this.repository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback);
|
this.repository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback);
|
||||||
this.repository.subscribeToEtaUpdates(this.etaSubscriberCallback);
|
this.repository.subscribeToEtaUpdates(this.etaSubscriberCallback);
|
||||||
@@ -227,15 +228,12 @@ export class NotificationService {
|
|||||||
const tupleKey = new TupleKey(shuttleId, stopId);
|
const tupleKey = new TupleKey(shuttleId, stopId);
|
||||||
if (
|
if (
|
||||||
this.deviceIdsToDeliverTo[tupleKey.toString()] === undefined
|
this.deviceIdsToDeliverTo[tupleKey.toString()] === undefined
|
||||||
|| !this.deviceIdsToDeliverTo[tupleKey.toString()].includes(deviceId)
|
|| !this.deviceIdsToDeliverTo[tupleKey.toString()].has(deviceId)
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const index = this.deviceIdsToDeliverTo[tupleKey.toString()].findIndex(id => id === deviceId);
|
this.deviceIdsToDeliverTo[tupleKey.toString()].delete(deviceId);
|
||||||
if (index !== -1) {
|
|
||||||
this.deviceIdsToDeliverTo[tupleKey.toString()].splice(index, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -249,6 +247,6 @@ export class NotificationService {
|
|||||||
if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) {
|
if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return this.deviceIdsToDeliverTo[tuple.toString()].includes(deviceId);
|
return this.deviceIdsToDeliverTo[tuple.toString()].has(deviceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user