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:
@@ -4,6 +4,7 @@ import path from "node:path";
|
||||
import { DEFAULT_TEMPERATURE_UNIT, DEFAULT_WIND_SPEED_UNIT, isTemperatureUnit, isWindSpeedUnit } from "@/lib/weather-utils";
|
||||
import type { WarningPushSubscription } from "@/types/notifications";
|
||||
import type { Province } from "@/types/province";
|
||||
import type { WeatherRegion } from "@/types/weather-region";
|
||||
|
||||
interface PushDatabase {
|
||||
database: Database.Database;
|
||||
@@ -13,6 +14,7 @@ interface PushSubscriptionRow {
|
||||
endpoint: string;
|
||||
subscription_json: string;
|
||||
province: string;
|
||||
region?: string;
|
||||
language: string;
|
||||
enabled: number;
|
||||
morning_brief_enabled: number;
|
||||
@@ -20,6 +22,7 @@ interface PushSubscriptionRow {
|
||||
latitude: number | null;
|
||||
longitude: number | null;
|
||||
location_name: string | null;
|
||||
timezone: string | null;
|
||||
county_teryt: string | null;
|
||||
temperature_unit: string | null;
|
||||
wind_speed_unit: string | null;
|
||||
@@ -50,7 +53,8 @@ function initializeDatabase(database: Database.Database) {
|
||||
CREATE TABLE IF NOT EXISTS push_subscriptions (
|
||||
endpoint TEXT PRIMARY KEY,
|
||||
subscription_json TEXT NOT NULL,
|
||||
province TEXT NOT NULL,
|
||||
province TEXT NOT NULL DEFAULT 'global',
|
||||
region TEXT NOT NULL DEFAULT 'PL',
|
||||
language TEXT NOT NULL,
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
morning_brief_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
@@ -58,6 +62,7 @@ function initializeDatabase(database: Database.Database) {
|
||||
latitude REAL,
|
||||
longitude REAL,
|
||||
location_name TEXT,
|
||||
timezone TEXT,
|
||||
county_teryt TEXT,
|
||||
temperature_unit TEXT NOT NULL DEFAULT '${DEFAULT_TEMPERATURE_UNIT}',
|
||||
wind_speed_unit TEXT NOT NULL DEFAULT '${DEFAULT_WIND_SPEED_UNIT}',
|
||||
@@ -89,6 +94,13 @@ function initializeDatabase(database: Database.Database) {
|
||||
FOREIGN KEY (endpoint) REFERENCES push_subscriptions(endpoint) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
const columns = database.prepare("PRAGMA table_info(push_subscriptions)").all() as Array<{ name: string }>;
|
||||
if (!columns.some((column) => column.name === "region")) {
|
||||
database.prepare("ALTER TABLE push_subscriptions ADD COLUMN region TEXT NOT NULL DEFAULT 'PL'").run();
|
||||
}
|
||||
if (!columns.some((column) => column.name === "timezone")) {
|
||||
database.prepare("ALTER TABLE push_subscriptions ADD COLUMN timezone TEXT").run();
|
||||
}
|
||||
}
|
||||
|
||||
function getDatabase() {
|
||||
@@ -105,10 +117,12 @@ function getDatabase() {
|
||||
function parseSubscription(row: PushSubscriptionRow): WarningPushSubscription | null {
|
||||
try {
|
||||
const subscription = JSON.parse(row.subscription_json) as WarningPushSubscription["subscription"];
|
||||
const region: WeatherRegion = row.region === "GLOBAL" || row.province === "global" ? "GLOBAL" : "PL";
|
||||
return {
|
||||
endpoint: row.endpoint,
|
||||
subscription,
|
||||
province: row.province as Province,
|
||||
province: row.province === "global" ? null : row.province as Province,
|
||||
region,
|
||||
language: row.language === "en" ? "en" : "pl",
|
||||
enabled: row.enabled === 1,
|
||||
morningBriefEnabled: row.morning_brief_enabled === 1,
|
||||
@@ -116,6 +130,7 @@ function parseSubscription(row: PushSubscriptionRow): WarningPushSubscription |
|
||||
latitude: row.latitude,
|
||||
longitude: row.longitude,
|
||||
locationName: row.location_name,
|
||||
timezone: row.timezone,
|
||||
countyTeryt: row.county_teryt,
|
||||
temperatureUnit: isTemperatureUnit(row.temperature_unit) ? row.temperature_unit : DEFAULT_TEMPERATURE_UNIT,
|
||||
windSpeedUnit: isWindSpeedUnit(row.wind_speed_unit) ? row.wind_speed_unit : DEFAULT_WIND_SPEED_UNIT,
|
||||
@@ -133,6 +148,7 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
endpoint,
|
||||
subscription_json,
|
||||
province,
|
||||
region,
|
||||
language,
|
||||
enabled,
|
||||
morning_brief_enabled,
|
||||
@@ -140,6 +156,7 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
latitude,
|
||||
longitude,
|
||||
location_name,
|
||||
timezone,
|
||||
county_teryt,
|
||||
temperature_unit,
|
||||
wind_speed_unit,
|
||||
@@ -149,6 +166,7 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
@endpoint,
|
||||
@subscriptionJson,
|
||||
@province,
|
||||
@region,
|
||||
@language,
|
||||
@enabled,
|
||||
@morningBriefEnabled,
|
||||
@@ -156,6 +174,7 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
@latitude,
|
||||
@longitude,
|
||||
@locationName,
|
||||
@timezone,
|
||||
@countyTeryt,
|
||||
@temperatureUnit,
|
||||
@windSpeedUnit,
|
||||
@@ -165,6 +184,7 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
ON CONFLICT(endpoint) DO UPDATE SET
|
||||
subscription_json = excluded.subscription_json,
|
||||
province = excluded.province,
|
||||
region = excluded.region,
|
||||
language = excluded.language,
|
||||
enabled = excluded.enabled,
|
||||
morning_brief_enabled = excluded.morning_brief_enabled,
|
||||
@@ -172,6 +192,7 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
latitude = excluded.latitude,
|
||||
longitude = excluded.longitude,
|
||||
location_name = excluded.location_name,
|
||||
timezone = excluded.timezone,
|
||||
county_teryt = excluded.county_teryt,
|
||||
temperature_unit = excluded.temperature_unit,
|
||||
wind_speed_unit = excluded.wind_speed_unit,
|
||||
@@ -179,7 +200,8 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
`).run({
|
||||
endpoint: subscription.endpoint,
|
||||
subscriptionJson: JSON.stringify(subscription.subscription),
|
||||
province: subscription.province,
|
||||
province: subscription.province ?? "global",
|
||||
region: subscription.region,
|
||||
language: subscription.language,
|
||||
enabled: subscription.enabled ? 1 : 0,
|
||||
morningBriefEnabled: subscription.morningBriefEnabled ? 1 : 0,
|
||||
@@ -187,6 +209,7 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
latitude: subscription.latitude,
|
||||
longitude: subscription.longitude,
|
||||
locationName: subscription.locationName,
|
||||
timezone: subscription.timezone,
|
||||
countyTeryt: subscription.countyTeryt,
|
||||
temperatureUnit: subscription.temperatureUnit,
|
||||
windSpeedUnit: subscription.windSpeedUnit,
|
||||
|
||||
Reference in New Issue
Block a user