🐛 Fix issue with old migration and add migration for location

This commit is contained in:
Meier Lukas
2023-06-11 00:33:28 +02:00
parent f033697579
commit c7e066392f
6 changed files with 458 additions and 399 deletions

View File

@@ -316,7 +316,11 @@
"type": "weather",
"properties": {
"displayInFahrenheit": false,
"location": "Paris"
"location": {
"name": "Paris",
"latitude": 48.85341,
"longitude": 2.3488
}
},
"area": {
"type": "category",

View File

@@ -38,7 +38,7 @@ export async function getServerSideProps({
};
}
const config = getFrontendConfig(configName as string);
const config = await getFrontendConfig(configName as string);
setCookie('config-name', configName, {
req,
res,

View File

@@ -47,7 +47,7 @@ export async function getServerSideProps({
}
const translations = await getServerSideTranslations(dashboardNamespaces, locale, req, res);
const config = getFrontendConfig(configName as string);
const config = await getFrontendConfig(configName as string);
return {
props: {

View File

@@ -34,10 +34,7 @@ export const weatherRouter = createTRPCRouter({
results: z.array(citySchema),
})
)
.query(async ({ input }) => {
const res = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${input.query}`);
return res.json();
}),
.query(async ({ input }) => fetchCity(input.query)),
at: publicProcedure
.input(
z.object({
@@ -56,3 +53,12 @@ export const weatherRouter = createTRPCRouter({
export type City = z.infer<typeof citySchema>;
export type Weather = z.infer<typeof weatherSchema>;
const outputSchema = z.object({
results: z.array(citySchema),
});
export const fetchCity = async (query: string) => {
const res = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${query}`);
return outputSchema.parse(await res.json());
};

View File

@@ -1,10 +1,19 @@
import Consola from 'consola';
import { ConfigType } from '../../types/config';
import fs from 'fs';
import { BackendConfigType, ConfigType } from '../../types/config';
import { getConfig } from './getConfig';
import { fetchCity } from '~/server/api/routers/weather';
export const getFrontendConfig = (name: string): ConfigType => {
const config = getConfig(name);
export const getFrontendConfig = async (name: string): Promise<ConfigType> => {
let config = getConfig(name);
const anyWeatherWidgetWithStringLocation = config.widgets.some(
(widget) => widget.type === 'weather' && typeof widget.properties.location === 'string'
);
if (anyWeatherWidgetWithStringLocation) {
config = await migrateLocation(config);
}
Consola.info(`Requested frontend content of configuration '${name}'`);
// If not, return the config
@@ -41,3 +50,39 @@ export const getFrontendConfig = (name: string): ConfigType => {
})),
};
};
const migrateLocation = async (config: BackendConfigType) => {
Consola.log('Migrating config file to new location schema...', config.configProperties.name);
const configName = config.configProperties.name;
const migratedConfig = {
...config,
widgets: await Promise.all(
config.widgets.map(async (widget) =>
widget.type !== 'weather' || typeof widget.properties.location !== 'string'
? widget
: {
...widget,
properties: {
...widget.properties,
location: await fetchCity(widget.properties.location)
.then(({ results }) => ({
name: results[0].name,
latitude: results[0].latitude,
longitude: results[0].longitude,
}))
.catch(() => ({
name: '',
latitude: 0,
longitude: 0,
})),
},
}
)
),
};
fs.writeFileSync(`./data/configs/${configName}.json`, JSON.stringify(migratedConfig, null, 2));
return migratedConfig;
};

View File

@@ -2,13 +2,11 @@ import Consola from 'consola';
import { v4 as uuidv4 } from 'uuid';
import { Config, serviceItem } from '../types';
import { ConfigAppIntegrationType, ConfigAppType, IntegrationType } from '../../types/app';
import { AreaType } from '../../types/area';
import { CategoryType } from '../../types/category';
import { BackendConfigType } from '../../types/config';
import { SearchEngineCommonSettingsType } from '../../types/settings';
import { IWidget } from '../../widgets/widgets';
import { ICalendarWidget } from '../../widgets/calendar/CalendarTile';
import { IDashDotTile } from '../../widgets/dashDot/DashDotTile';
import { IDateWidget } from '../../widgets/date/DateTile';
@@ -16,6 +14,8 @@ import { ITorrentNetworkTraffic } from '../../widgets/download-speed/TorrentNetw
import { ITorrent } from '../../widgets/torrent/TorrentTile';
import { IUsenetWidget } from '../../widgets/useNet/UseNetTile';
import { IWeatherWidget } from '../../widgets/weather/WeatherTile';
import { IWidget } from '../../widgets/widgets';
import { Config, serviceItem } from '../types';
export function migrateConfig(config: Config): BackendConfigType {
const newConfig: BackendConfigType = {
@@ -208,7 +208,11 @@ const migrateModules = (config: Config): IWidget<string, any>[] => {
type: 'weather',
properties: {
displayInFahrenheit: oldModule.options?.freedomunit?.value ?? false,
location: oldModule.options?.location?.value ?? 'Paris',
location: {
name: oldModule.options?.location?.value ?? '',
latitude: 0,
longitude: 0,
},
},
area: {
type: 'wrapper',