feat: add weather unit preferences
Some checks failed
CI / Lint, test, typecheck and build (push) Has been cancelled
Some checks failed
CI / Lint, test, typecheck and build (push) Has been cancelled
This commit is contained in:
@@ -2,8 +2,14 @@ import Database from "better-sqlite3";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import {
|
||||
DEFAULT_DISTANCE_UNIT,
|
||||
DEFAULT_PRECIPITATION_UNIT,
|
||||
DEFAULT_PRESSURE_UNIT,
|
||||
DEFAULT_TEMPERATURE_UNIT,
|
||||
DEFAULT_WIND_SPEED_UNIT,
|
||||
isDistanceUnit,
|
||||
isPrecipitationUnit,
|
||||
isPressureUnit,
|
||||
isTemperatureUnit,
|
||||
isWindSpeedUnit,
|
||||
} from "@/lib/weather-utils";
|
||||
@@ -31,6 +37,9 @@ interface PushSubscriptionRow {
|
||||
county_teryt: string | null;
|
||||
temperature_unit: string | null;
|
||||
wind_speed_unit: string | null;
|
||||
precipitation_unit: string | null;
|
||||
pressure_unit: string | null;
|
||||
distance_unit: string | null;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
@@ -71,6 +80,9 @@ function initializeDatabase(database: Database.Database) {
|
||||
county_teryt TEXT,
|
||||
temperature_unit TEXT NOT NULL DEFAULT '${DEFAULT_TEMPERATURE_UNIT}',
|
||||
wind_speed_unit TEXT NOT NULL DEFAULT '${DEFAULT_WIND_SPEED_UNIT}',
|
||||
precipitation_unit TEXT NOT NULL DEFAULT '${DEFAULT_PRECIPITATION_UNIT}',
|
||||
pressure_unit TEXT NOT NULL DEFAULT '${DEFAULT_PRESSURE_UNIT}',
|
||||
distance_unit TEXT NOT NULL DEFAULT '${DEFAULT_DISTANCE_UNIT}',
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
@@ -106,6 +118,27 @@ function initializeDatabase(database: Database.Database) {
|
||||
if (!columns.some((column) => column.name === "timezone")) {
|
||||
database.prepare("ALTER TABLE push_subscriptions ADD COLUMN timezone TEXT").run();
|
||||
}
|
||||
if (!columns.some((column) => column.name === "precipitation_unit")) {
|
||||
database
|
||||
.prepare(
|
||||
`ALTER TABLE push_subscriptions ADD COLUMN precipitation_unit TEXT NOT NULL DEFAULT '${DEFAULT_PRECIPITATION_UNIT}'`,
|
||||
)
|
||||
.run();
|
||||
}
|
||||
if (!columns.some((column) => column.name === "pressure_unit")) {
|
||||
database
|
||||
.prepare(
|
||||
`ALTER TABLE push_subscriptions ADD COLUMN pressure_unit TEXT NOT NULL DEFAULT '${DEFAULT_PRESSURE_UNIT}'`,
|
||||
)
|
||||
.run();
|
||||
}
|
||||
if (!columns.some((column) => column.name === "distance_unit")) {
|
||||
database
|
||||
.prepare(
|
||||
`ALTER TABLE push_subscriptions ADD COLUMN distance_unit TEXT NOT NULL DEFAULT '${DEFAULT_DISTANCE_UNIT}'`,
|
||||
)
|
||||
.run();
|
||||
}
|
||||
}
|
||||
|
||||
function getDatabase() {
|
||||
@@ -139,6 +172,11 @@ function parseSubscription(row: PushSubscriptionRow): WarningPushSubscription |
|
||||
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,
|
||||
precipitationUnit: isPrecipitationUnit(row.precipitation_unit)
|
||||
? row.precipitation_unit
|
||||
: DEFAULT_PRECIPITATION_UNIT,
|
||||
pressureUnit: isPressureUnit(row.pressure_unit) ? row.pressure_unit : DEFAULT_PRESSURE_UNIT,
|
||||
distanceUnit: isDistanceUnit(row.distance_unit) ? row.distance_unit : DEFAULT_DISTANCE_UNIT,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
};
|
||||
@@ -167,6 +205,9 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
county_teryt,
|
||||
temperature_unit,
|
||||
wind_speed_unit,
|
||||
precipitation_unit,
|
||||
pressure_unit,
|
||||
distance_unit,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
@@ -185,6 +226,9 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
@countyTeryt,
|
||||
@temperatureUnit,
|
||||
@windSpeedUnit,
|
||||
@precipitationUnit,
|
||||
@pressureUnit,
|
||||
@distanceUnit,
|
||||
@createdAt,
|
||||
@updatedAt
|
||||
)
|
||||
@@ -203,6 +247,9 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
county_teryt = excluded.county_teryt,
|
||||
temperature_unit = excluded.temperature_unit,
|
||||
wind_speed_unit = excluded.wind_speed_unit,
|
||||
precipitation_unit = excluded.precipitation_unit,
|
||||
pressure_unit = excluded.pressure_unit,
|
||||
distance_unit = excluded.distance_unit,
|
||||
updated_at = excluded.updated_at
|
||||
`,
|
||||
)
|
||||
@@ -222,6 +269,9 @@ export function upsertPushSubscription(subscription: WarningPushSubscription) {
|
||||
countyTeryt: subscription.countyTeryt,
|
||||
temperatureUnit: subscription.temperatureUnit,
|
||||
windSpeedUnit: subscription.windSpeedUnit,
|
||||
precipitationUnit: subscription.precipitationUnit,
|
||||
pressureUnit: subscription.pressureUnit,
|
||||
distanceUnit: subscription.distanceUnit,
|
||||
createdAt: subscription.createdAt,
|
||||
updatedAt: subscription.updatedAt,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user