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:
@@ -5,6 +5,7 @@ import { normalizeProvinceName } from "@/lib/provinces";
|
||||
import { DEFAULT_TEMPERATURE_UNIT, DEFAULT_WIND_SPEED_UNIT, isTemperatureUnit, isWindSpeedUnit } from "@/lib/weather-utils";
|
||||
import type { Language } from "@/lib/i18n";
|
||||
import type { BrowserPushSubscription } from "@/types/notifications";
|
||||
import type { WeatherRegion } from "@/types/weather-region";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
@@ -25,6 +26,7 @@ export async function POST(request: Request) {
|
||||
const body = await request.json().catch(() => null) as {
|
||||
subscription?: unknown;
|
||||
province?: unknown;
|
||||
region?: unknown;
|
||||
language?: unknown;
|
||||
enabled?: unknown;
|
||||
morningBriefEnabled?: unknown;
|
||||
@@ -32,17 +34,20 @@ export async function POST(request: Request) {
|
||||
latitude?: unknown;
|
||||
longitude?: unknown;
|
||||
locationName?: unknown;
|
||||
timezone?: unknown;
|
||||
countyTeryt?: unknown;
|
||||
temperatureUnit?: unknown;
|
||||
windSpeedUnit?: unknown;
|
||||
} | null;
|
||||
const subscription = body?.subscription;
|
||||
const region: WeatherRegion = body?.region === "GLOBAL" ? "GLOBAL" : "PL";
|
||||
const province = normalizeProvinceName(typeof body?.province === "string" ? body.province : null);
|
||||
const language: Language = body?.language === "en" ? "en" : "pl";
|
||||
const rawTemperatureUnit = body?.temperatureUnit;
|
||||
const rawWindSpeedUnit = body?.windSpeedUnit;
|
||||
|
||||
if (!isBrowserPushSubscription(subscription) || !province) {
|
||||
const enabled = body?.enabled !== false && region === "PL";
|
||||
if (!isBrowserPushSubscription(subscription) || (enabled && !province)) {
|
||||
return NextResponse.json({ error: "Invalid push subscription." }, { status: 400 });
|
||||
}
|
||||
|
||||
@@ -51,13 +56,15 @@ export async function POST(request: Request) {
|
||||
endpoint: subscription.endpoint,
|
||||
subscription,
|
||||
province,
|
||||
region,
|
||||
language,
|
||||
enabled: body?.enabled !== false,
|
||||
enabled,
|
||||
morningBriefEnabled: body?.morningBriefEnabled === true,
|
||||
tomorrowBriefEnabled: body?.tomorrowBriefEnabled === true,
|
||||
latitude: typeof body?.latitude === "number" && Number.isFinite(body.latitude) ? body.latitude : null,
|
||||
longitude: typeof body?.longitude === "number" && Number.isFinite(body.longitude) ? body.longitude : null,
|
||||
locationName: typeof body?.locationName === "string" && body.locationName.trim() ? body.locationName.trim().slice(0, 80) : null,
|
||||
timezone: typeof body?.timezone === "string" && body.timezone.trim() ? body.timezone.trim().slice(0, 80) : null,
|
||||
countyTeryt: typeof body?.countyTeryt === "string" && /^\d{4}$/.test(body.countyTeryt) ? body.countyTeryt : null,
|
||||
temperatureUnit: isTemperatureUnit(rawTemperatureUnit) ? rawTemperatureUnit : DEFAULT_TEMPERATURE_UNIT,
|
||||
windSpeedUnit: isWindSpeedUnit(rawWindSpeedUnit) ? rawWindSpeedUnit : DEFAULT_WIND_SPEED_UNIT,
|
||||
|
||||
Reference in New Issue
Block a user