feat: add global weather support
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
This commit is contained in:
@@ -16,25 +16,36 @@ function isCronAuthorized(request: Request) {
|
||||
return authHeader === `Bearer ${secret}` || secretHeader === secret;
|
||||
}
|
||||
|
||||
function getWarsawDateKey(date = new Date(), dayOffset = 0) {
|
||||
const DEFAULT_TOMORROW_BRIEF_TIME = "18:00";
|
||||
|
||||
function normalizeTime(value: string | undefined, fallback: string) {
|
||||
return value && /^\d{2}:\d{2}$/.test(value) ? value : fallback;
|
||||
}
|
||||
|
||||
function getLocalDateParts(timeZone: string | null, date = new Date(), dayOffset = 0) {
|
||||
const parts = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone: "Europe/Warsaw",
|
||||
timeZone: timeZone || "Europe/Warsaw",
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
hourCycle: "h23",
|
||||
}).formatToParts(date);
|
||||
const part = (type: Intl.DateTimeFormatPartTypes) => parts.find((entry) => entry.type === type)?.value ?? "";
|
||||
if (dayOffset === 0) return `${part("year")}-${part("month")}-${part("day")}`;
|
||||
const dateKey = `${part("year")}-${part("month")}-${part("day")}`;
|
||||
const time = `${part("hour")}:${part("minute")}`;
|
||||
if (dayOffset === 0) return { dateKey, time };
|
||||
|
||||
const shiftedDate = new Date(Date.UTC(Number(part("year")), Number(part("month")) - 1, Number(part("day")) + dayOffset, 12));
|
||||
const shiftedParts = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone: "Europe/Warsaw",
|
||||
timeZone: timeZone || "Europe/Warsaw",
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
}).formatToParts(shiftedDate);
|
||||
const shiftedPart = (type: Intl.DateTimeFormatPartTypes) => shiftedParts.find((entry) => entry.type === type)?.value ?? "";
|
||||
return `${shiftedPart("year")}-${shiftedPart("month")}-${shiftedPart("day")}`;
|
||||
return { dateKey: `${shiftedPart("year")}-${shiftedPart("month")}-${shiftedPart("day")}`, time };
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
@@ -46,35 +57,43 @@ export async function GET(request: Request) {
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const targetDateKey = getWarsawDateKey(now, 1);
|
||||
const briefTime = normalizeTime(process.env.NOTIFICATIONS_TOMORROW_BRIEF_TIME, DEFAULT_TOMORROW_BRIEF_TIME);
|
||||
const subscriptions = getPushSubscriptions().filter((subscription) => (
|
||||
subscription.enabled
|
||||
&& subscription.tomorrowBriefEnabled
|
||||
subscription.tomorrowBriefEnabled
|
||||
&& Number.isFinite(subscription.latitude)
|
||||
&& Number.isFinite(subscription.longitude)
|
||||
));
|
||||
let warningsUnavailable = false;
|
||||
const warnings = await fetchMeteoWarnings(AbortSignal.timeout(12_000)).catch(() => {
|
||||
warningsUnavailable = true;
|
||||
return [];
|
||||
});
|
||||
const hasPolishSubscriptions = subscriptions.some((subscription) => subscription.region === "PL");
|
||||
const warnings = hasPolishSubscriptions
|
||||
? await fetchMeteoWarnings(AbortSignal.timeout(12_000)).catch(() => {
|
||||
warningsUnavailable = true;
|
||||
return [];
|
||||
})
|
||||
: [];
|
||||
let sent = 0;
|
||||
let skipped = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (const subscription of subscriptions) {
|
||||
const localToday = getLocalDateParts(subscription.timezone, now);
|
||||
if (localToday.time < briefTime) {
|
||||
skipped += 1;
|
||||
continue;
|
||||
}
|
||||
const targetDateKey = getLocalDateParts(subscription.timezone, now, 1).dateKey;
|
||||
if (hasSentTomorrowBrief(subscription.endpoint, targetDateKey)) {
|
||||
skipped += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const forecast = await fetchServerForecast(subscription.latitude as number, subscription.longitude as number);
|
||||
const forecast = await fetchServerForecast(subscription.latitude as number, subscription.longitude as number, subscription.region);
|
||||
const brief = buildTomorrowWeatherBrief({
|
||||
forecast,
|
||||
warnings,
|
||||
warnings: subscription.region === "PL" ? warnings : [],
|
||||
province: subscription.province,
|
||||
countyTeryt: subscription.countyTeryt,
|
||||
countyTeryt: subscription.region === "PL" ? subscription.countyTeryt : null,
|
||||
locationName: subscription.locationName ?? "wtr.",
|
||||
language: subscription.language,
|
||||
temperatureUnit: subscription.temperatureUnit ?? DEFAULT_TEMPERATURE_UNIT,
|
||||
@@ -97,7 +116,7 @@ export async function GET(request: Request) {
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
date: targetDateKey,
|
||||
time: briefTime,
|
||||
subscriptions: subscriptions.length,
|
||||
warningsUnavailable,
|
||||
sent,
|
||||
|
||||
Reference in New Issue
Block a user