add request code

This commit is contained in:
2025-02-03 22:16:53 -08:00
parent f602aa6c2c
commit c5c0208371

View File

@@ -69,8 +69,47 @@ export class NotificationService {
this.reloadAPNsTokenIfTimePassed();
const url = this.getAPNsFullUrlToUse(deviceId);
// Send the fetch request
const shuttle = await this.repository.getShuttleById(shuttleId);
const stop = await this.repository.getStopById(stopId);
const eta = await this.repository.getEtaForShuttleAndStopId(shuttleId, stopId);
// TODO: add more specific errors
if (!shuttle) {
throw new Error("The shuttle given by the provided shuttleID doesn't exist.");
}
if (!stop) {
throw new Error("The shuttle given by the provided stopId doesn't exist.");
}
// TODO: account for cases where ETA may not exist due to "data race" with ApiBasedRepositoryLoader
if (!eta) {
throw new Error("There is no ETA for this shuttle/stop.");
}
// Send the fetch request
const bundleId = process.env.APNS_BUNDLE_ID;
if (typeof bundleId !== "string") {
throw new Error("APNS_BUNDLE_ID environment variable is not set correctly");
}
const headers = {
authorization: `bearer ${this.apnsToken}`,
"apns-push-type": "alert",
"apns-expiration": "0",
"apns-priority": "10",
"apns-topic": bundleId,
};
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify({
aps: {
alert: {
title: "Shuttle is arriving",
body: `Shuttle is approaching ${stop.name} in ${Math.ceil(eta.secondsRemaining / 60)} minutes.`
}
}
}),
});
const json = await response.json();
// Check whether it was successful
// Return the result