some improvements

This commit is contained in:
Fran Jurmanović
2025-06-25 22:38:01 +02:00
parent 77a41bf4ef
commit 47a72c82f4
7 changed files with 128 additions and 80 deletions

View File

@@ -2,15 +2,14 @@ import {
updateConfig,
getConfigFiles,
getServerById,
getStateHistory,
getStateHistoryStats
} from '$api/serverService';
import type { Actions } from './$types';
import { checkAuth } from '$api/authService';
import { getTracks } from '$api/lookupService';
import { redirect } from '@sveltejs/kit';
import { fail, redirect } from '@sveltejs/kit';
import type { RequestEvent } from '@sveltejs/kit';
import { configFile, type Config, type Session } from '$models/config';
import { configFile, type Config } from '$models/config';
import { set } from 'lodash-es';
import { subDays, formatISO } from 'date-fns';
import { UTCDate } from '@date-fns/utc';
@@ -38,20 +37,16 @@ export const load = async (event: RequestEvent) => {
};
};
type SessionField =
| 'sessionDurationMinutes'
| 'sessionType'
| 'timeMultiplier'
| 'dayOfWeekend'
| 'hourOfDay';
export const actions = {
update: async (event: RequestEvent) => {
const { id, restart, file, data } = await destructureFormData(event);
const sessions: Array<Record<SessionField, string | number>> = [];
await updateConfig(event, id, file, data, true, restart);
try {
const { id, restart, file, data } = await destructureFormData(event);
await updateConfig(event, id, file, data, true, restart);
return { success: true, message: `Configuration file '${file}' updated successfully.` };
} catch (err) {
const message = err instanceof Error ? err.message : 'An unknown error occurred';
return fail(500, { message: `Failed to update configuration: ${message}` });
}
}
} satisfies Actions;
@@ -59,36 +54,46 @@ async function destructureFormData(
event: RequestEvent
): Promise<{ id: string; restart: boolean; data: Config; file: configFile }> {
const formData = await event.request.formData();
const id = formData.get('id') as string;
const id = formData.get('id');
if (!id || typeof id !== 'string') {
throw new Error('Server ID is missing or invalid.');
}
const file = formData.get('file');
if (!file || typeof file !== 'string') {
throw new Error('Config file name is missing or invalid.');
}
const restart = formData.get('restart');
const file = formData.get('file') as configFile;
const object: any = {};
const object: Record<string, unknown> = {};
formData.forEach((value, key) => {
switch (key) {
case 'id':
case 'restart':
case 'file':
return;
default:
set(object, key, parseFormField(value));
// Exclude metadata fields from the dynamic object
if (key === 'id' || key === 'restart' || key === 'file') {
return;
}
set(object, key, parseFormField(value));
});
return {
id,
restart: restart == 'on' || restart == 'true',
data: object,
file
restart: restart === 'on' || restart === 'true',
data: object as unknown as Config,
file: file as configFile
};
}
/**
* Parses a form field value. If the value can be cleanly converted to a number,
* it returns a number; otherwise, it returns the original string.
*/
function parseFormField(value: FormDataEntryValue): string | number {
return value !== '' && !Number.isNaN(+value) ? +value : (value as string);
}
function tryParse(str: string) {
try {
return JSON.parse(str);
} catch {
return str;
// Avoid converting empty strings to 0
if (value === '' || typeof value !== 'string') {
return value as string;
}
// Check if string is a valid number without being too aggressive
const num = Number(value);
return !Number.isNaN(num) && value.trim() !== '' ? num : value;
}