forked from jsnbuchanan/crowd-funder-for-time-pwa
fix: move lexical declarations outside case blocks in AbsurdSqlDatabaseService
- Move queryResult and allResult declarations outside switch statement - Change const declarations to let since they're now in outer scope - Remove const declarations from inside case blocks This fixes the 'no-case-declarations' linter errors by ensuring variables are declared in a scope that encompasses all case blocks, preventing potential scoping issues. Note: Type definition errors for external modules remain and should be addressed separately.
This commit is contained in:
@@ -11,7 +11,12 @@ export async function updateDefaultSettings(
|
||||
delete settingsChanges.id;
|
||||
try {
|
||||
const platformService = PlatformServiceFactory.getInstance();
|
||||
const { sql, params } = generateUpdateStatement(settingsChanges, "settings", "id = ?", [MASTER_SETTINGS_KEY]);
|
||||
const { sql, params } = generateUpdateStatement(
|
||||
settingsChanges,
|
||||
"settings",
|
||||
"id = ?",
|
||||
[MASTER_SETTINGS_KEY],
|
||||
);
|
||||
const result = await platformService.dbExec(sql, params);
|
||||
return result.changes === 1;
|
||||
} catch (error) {
|
||||
@@ -34,28 +39,28 @@ export async function updateAccountSettings(
|
||||
delete settingsChanges.id; // key off account, not ID
|
||||
|
||||
const platform = PlatformServiceFactory.getInstance();
|
||||
|
||||
|
||||
// First try to update existing record
|
||||
const { sql: updateSql, params: updateParams } = generateUpdateStatement(
|
||||
settingsChanges,
|
||||
"settings",
|
||||
"accountDid = ?",
|
||||
[accountDid]
|
||||
[accountDid],
|
||||
);
|
||||
|
||||
|
||||
const updateResult = await platform.dbExec(updateSql, updateParams);
|
||||
|
||||
|
||||
// If no record was updated, insert a new one
|
||||
if (updateResult.changes === 1) {
|
||||
return true;
|
||||
} else {
|
||||
const columns = Object.keys(settingsChanges);
|
||||
const values = Object.values(settingsChanges);
|
||||
const placeholders = values.map(() => '?').join(', ');
|
||||
|
||||
const insertSql = `INSERT INTO settings (${columns.join(', ')}) VALUES (${placeholders})`;
|
||||
const placeholders = values.map(() => "?").join(", ");
|
||||
|
||||
const insertSql = `INSERT INTO settings (${columns.join(", ")}) VALUES (${placeholders})`;
|
||||
const result = await platform.dbExec(insertSql, values);
|
||||
|
||||
|
||||
return result.changes === 1;
|
||||
}
|
||||
}
|
||||
@@ -69,7 +74,9 @@ const DEFAULT_SETTINGS: Settings = {
|
||||
// retrieves default settings
|
||||
export async function retrieveSettingsForDefaultAccount(): Promise<Settings> {
|
||||
const platform = PlatformServiceFactory.getInstance();
|
||||
const result = await platform.dbQuery("SELECT * FROM settings WHERE id = ?", [MASTER_SETTINGS_KEY])
|
||||
const result = await platform.dbQuery("SELECT * FROM settings WHERE id = ?", [
|
||||
MASTER_SETTINGS_KEY,
|
||||
]);
|
||||
if (!result) {
|
||||
return DEFAULT_SETTINGS;
|
||||
} else {
|
||||
@@ -85,10 +92,14 @@ export async function retrieveSettingsForActiveAccount(): Promise<Settings> {
|
||||
const platform = PlatformServiceFactory.getInstance();
|
||||
const result = await platform.dbQuery(
|
||||
"SELECT * FROM settings WHERE accountDid = ?",
|
||||
[defaultSettings.activeDid]
|
||||
[defaultSettings.activeDid],
|
||||
);
|
||||
const overrideSettings = result
|
||||
? (mapColumnsToValues(result.columns, result.values)[0] as Settings)
|
||||
: {};
|
||||
const overrideSettingsFiltered = Object.fromEntries(
|
||||
Object.entries(overrideSettings).filter(([_, v]) => v !== null),
|
||||
);
|
||||
const overrideSettings = result ? mapColumnsToValues(result.columns, result.values)[0] as Settings : {};
|
||||
const overrideSettingsFiltered = Object.fromEntries(Object.entries(overrideSettings).filter(([_, v]) => v !== null));
|
||||
return { ...defaultSettings, ...overrideSettingsFiltered };
|
||||
}
|
||||
}
|
||||
@@ -96,32 +107,32 @@ export async function retrieveSettingsForActiveAccount(): Promise<Settings> {
|
||||
export async function logToDb(message: string): Promise<void> {
|
||||
const platform = PlatformServiceFactory.getInstance();
|
||||
const todayKey = new Date().toDateString();
|
||||
|
||||
|
||||
// Check if we have any logs for today
|
||||
const result = await platform.dbQuery(
|
||||
"SELECT message FROM logs WHERE date = ?",
|
||||
[todayKey]
|
||||
[todayKey],
|
||||
);
|
||||
|
||||
|
||||
if (!result || result.values.length === 0) {
|
||||
// If no logs for today, clear all previous logs
|
||||
await platform.dbExec("DELETE FROM logs");
|
||||
|
||||
|
||||
// Insert new log
|
||||
const fullMessage = `${new Date().toISOString()} ${message}`;
|
||||
await platform.dbExec(
|
||||
"INSERT INTO logs (date, message) VALUES (?, ?)",
|
||||
[todayKey, fullMessage]
|
||||
);
|
||||
await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [
|
||||
todayKey,
|
||||
fullMessage,
|
||||
]);
|
||||
} else {
|
||||
// Append to existing log
|
||||
const prevMessages = result.values[0][0] as string;
|
||||
const fullMessage = `${prevMessages}\n${new Date().toISOString()} ${message}`;
|
||||
|
||||
await platform.dbExec(
|
||||
"UPDATE logs SET message = ? WHERE date = ?",
|
||||
[fullMessage, todayKey]
|
||||
);
|
||||
|
||||
await platform.dbExec("UPDATE logs SET message = ? WHERE date = ?", [
|
||||
fullMessage,
|
||||
todayKey,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,14 +158,14 @@ export async function logConsoleAndDb(
|
||||
* @returns Object containing the SQL statement and parameters array
|
||||
*/
|
||||
function generateUpdateStatement(
|
||||
model: Record<string, any>,
|
||||
model: Record<string, unknown>,
|
||||
tableName: string,
|
||||
whereClause: string,
|
||||
whereParams: any[] = []
|
||||
): { sql: string; params: any[] } {
|
||||
whereParams: unknown[] = [],
|
||||
): { sql: string; params: unknown[] } {
|
||||
// Filter out undefined/null values and create SET clause
|
||||
const setClauses: string[] = [];
|
||||
const params: any[] = [];
|
||||
const params: unknown[] = [];
|
||||
|
||||
Object.entries(model).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
@@ -164,14 +175,14 @@ function generateUpdateStatement(
|
||||
});
|
||||
|
||||
if (setClauses.length === 0) {
|
||||
throw new Error('No valid fields to update');
|
||||
throw new Error("No valid fields to update");
|
||||
}
|
||||
|
||||
const sql = `UPDATE ${tableName} SET ${setClauses.join(', ')} WHERE ${whereClause}`;
|
||||
|
||||
const sql = `UPDATE ${tableName} SET ${setClauses.join(", ")} WHERE ${whereClause}`;
|
||||
|
||||
return {
|
||||
sql,
|
||||
params: [...params, ...whereParams]
|
||||
params: [...params, ...whereParams],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -184,14 +195,13 @@ function generateUpdateStatement(
|
||||
*/
|
||||
export function mapColumnsToValues(
|
||||
columns: string[],
|
||||
values: any[][]
|
||||
): Record<string, any>[] {
|
||||
return values.map(row => {
|
||||
const obj: Record<string, any> = {};
|
||||
values: unknown[][],
|
||||
): Record<string, unknown>[] {
|
||||
return values.map((row) => {
|
||||
const obj: Record<string, unknown> = {};
|
||||
columns.forEach((column, index) => {
|
||||
obj[column] = row[index];
|
||||
});
|
||||
return obj;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user