mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
69 lines
1.6 KiB
TypeScript
69 lines
1.6 KiB
TypeScript
import { GetterRepository } from "../repositories/GetterRepository";
|
|
import * as crypto from "node:crypto";
|
|
import jwt from "jsonwebtoken";
|
|
import * as fs from "node:fs";
|
|
|
|
interface ScheduledNotificationData {
|
|
deviceId: string;
|
|
shuttleId: string;
|
|
stopId: string;
|
|
}
|
|
|
|
export class NotificationService {
|
|
private token: string | undefined = undefined;
|
|
private lastRefreshedTimeMs: number | undefined = undefined;
|
|
|
|
constructor(private repository: GetterRepository) {}
|
|
|
|
private encryptionKey = crypto.randomBytes(32);
|
|
private iv = crypto.randomBytes(16);
|
|
|
|
public async reloadAPNsTokenIfTimePassed() {
|
|
if (this.lastReloadedTimeForAPNsIsTooRecent()) {
|
|
return;
|
|
}
|
|
|
|
const keyId = process.env.APNS_KEY_ID;
|
|
const teamId = process.env.APNS_TEAM_ID;
|
|
const privateKeyPath = process.env.APNS_KEY_PATH;
|
|
if (!privateKeyPath) return;
|
|
const privateKey = fs.readFileSync(privateKeyPath);
|
|
|
|
const tokenHeader = {
|
|
alg: "ES256",
|
|
"kid": keyId,
|
|
};
|
|
|
|
const claimsPayload = {
|
|
"iss": teamId,
|
|
"iat": Date.now(),
|
|
};
|
|
|
|
this.token = jwt.sign(claimsPayload, privateKey, {
|
|
algorithm: "ES256",
|
|
header: tokenHeader
|
|
});
|
|
}
|
|
|
|
private lastReloadedTimeForAPNsIsTooRecent() {
|
|
const thirtyMinutesMs = 1800000;
|
|
return this.lastRefreshedTimeMs && Date.now() - this.lastRefreshedTimeMs < thirtyMinutesMs;
|
|
}
|
|
|
|
public async scheduleNotification({ deviceId, shuttleId, stopId }: ScheduledNotificationData) {
|
|
|
|
}
|
|
|
|
public async cancelNotification({ deviceId, shuttleId, stopId }: ScheduledNotificationData) {
|
|
|
|
}
|
|
|
|
public cancelAllNotifications() {
|
|
|
|
}
|
|
|
|
public startReloadingNotificationTokens() {
|
|
|
|
}
|
|
}
|