@ -11,7 +11,12 @@ export async function updateDefaultSettings(
delete settingsChanges . id ;
delete settingsChanges . id ;
try {
try {
const platformService = PlatformServiceFactory . getInstance ( ) ;
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 ) ;
const result = await platformService . dbExec ( sql , params ) ;
return result . changes === 1 ;
return result . changes === 1 ;
} catch ( error ) {
} catch ( error ) {
@ -40,7 +45,7 @@ export async function updateAccountSettings(
settingsChanges ,
settingsChanges ,
"settings" ,
"settings" ,
"accountDid = ?" ,
"accountDid = ?" ,
[ accountDid ]
[ accountDid ] ,
) ;
) ;
const updateResult = await platform . dbExec ( updateSql , updateParams ) ;
const updateResult = await platform . dbExec ( updateSql , updateParams ) ;
@ -51,9 +56,9 @@ export async function updateAccountSettings(
} else {
} else {
const columns = Object . keys ( settingsChanges ) ;
const columns = Object . keys ( settingsChanges ) ;
const values = Object . values ( settingsChanges ) ;
const values = Object . values ( settingsChanges ) ;
const placeholders = values . map ( ( ) = > '?' ) . join ( ', ' ) ;
const placeholders = values . map ( ( ) = > "?" ) . join ( ", " ) ;
const insertSql = ` INSERT INTO settings ( ${ columns . join ( ', ' ) } ) VALUES ( ${ placeholders } ) ` ;
const insertSql = ` INSERT INTO settings ( ${ columns . join ( ", " ) } ) VALUES ( ${ placeholders } ) ` ;
const result = await platform . dbExec ( insertSql , values ) ;
const result = await platform . dbExec ( insertSql , values ) ;
return result . changes === 1 ;
return result . changes === 1 ;
@ -69,7 +74,9 @@ const DEFAULT_SETTINGS: Settings = {
// retrieves default settings
// retrieves default settings
export async function retrieveSettingsForDefaultAccount ( ) : Promise < Settings > {
export async function retrieveSettingsForDefaultAccount ( ) : Promise < Settings > {
const platform = PlatformServiceFactory . getInstance ( ) ;
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 ) {
if ( ! result ) {
return DEFAULT_SETTINGS ;
return DEFAULT_SETTINGS ;
} else {
} else {
@ -85,10 +92,14 @@ export async function retrieveSettingsForActiveAccount(): Promise<Settings> {
const platform = PlatformServiceFactory . getInstance ( ) ;
const platform = PlatformServiceFactory . getInstance ( ) ;
const result = await platform . dbQuery (
const result = await platform . dbQuery (
"SELECT * FROM settings WHERE accountDid = ?" ,
"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 } ;
return { . . . defaultSettings , . . . overrideSettingsFiltered } ;
}
}
}
}
@ -100,7 +111,7 @@ export async function logToDb(message: string): Promise<void> {
// Check if we have any logs for today
// Check if we have any logs for today
const result = await platform . dbQuery (
const result = await platform . dbQuery (
"SELECT message FROM logs WHERE date = ?" ,
"SELECT message FROM logs WHERE date = ?" ,
[ todayKey ]
[ todayKey ] ,
) ;
) ;
if ( ! result || result . values . length === 0 ) {
if ( ! result || result . values . length === 0 ) {
@ -109,19 +120,19 @@ export async function logToDb(message: string): Promise<void> {
// Insert new log
// Insert new log
const fullMessage = ` ${ new Date ( ) . toISOString ( ) } ${ message } ` ;
const fullMessage = ` ${ new Date ( ) . toISOString ( ) } ${ message } ` ;
await platform . dbExec (
await platform . dbExec ( "INSERT INTO logs (date, message) VALUES (?, ?)" , [
"INSERT INTO logs (date, message) VALUES (?, ?)" ,
todayKey ,
[ todayKey , fullMessage ]
fullMessage ,
) ;
] ) ;
} else {
} else {
// Append to existing log
// Append to existing log
const prevMessages = result . values [ 0 ] [ 0 ] as string ;
const prevMessages = result . values [ 0 ] [ 0 ] as string ;
const fullMessage = ` ${ prevMessages } \ n ${ new Date ( ) . toISOString ( ) } ${ message } ` ;
const fullMessage = ` ${ prevMessages } \ n ${ new Date ( ) . toISOString ( ) } ${ message } ` ;
await platform . dbExec (
await platform . dbExec ( "UPDATE logs SET message = ? WHERE date = ?" , [
"UPDATE logs SET message = ? WHERE date = ?" ,
fullMessage ,
[ fullMessage , todayKey ]
todayKey ,
) ;
] ) ;
}
}
}
}
@ -147,14 +158,14 @@ export async function logConsoleAndDb(
* @returns Object containing the SQL statement and parameters array
* @returns Object containing the SQL statement and parameters array
* /
* /
function generateUpdateStatement (
function generateUpdateStatement (
model : Record < string , any > ,
model : Record < string , unknown > ,
tableName : string ,
tableName : string ,
whereClause : string ,
whereClause : string ,
whereParams : any [ ] = [ ]
whereParams : unknown [ ] = [ ] ,
) : { sql : string ; params : any [ ] } {
) : { sql : string ; params : unknown [ ] } {
// Filter out undefined/null values and create SET clause
// Filter out undefined/null values and create SET clause
const setClauses : string [ ] = [ ] ;
const setClauses : string [ ] = [ ] ;
const params : any [ ] = [ ] ;
const params : unknown [ ] = [ ] ;
Object . entries ( model ) . forEach ( ( [ key , value ] ) = > {
Object . entries ( model ) . forEach ( ( [ key , value ] ) = > {
if ( value !== undefined ) {
if ( value !== undefined ) {
@ -164,14 +175,14 @@ function generateUpdateStatement(
} ) ;
} ) ;
if ( setClauses . length === 0 ) {
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 {
return {
sql ,
sql ,
params : [ . . . params , . . . whereParams ]
params : [ . . . params , . . . whereParams ] ,
} ;
} ;
}
}
@ -184,14 +195,13 @@ function generateUpdateStatement(
* /
* /
export function mapColumnsToValues (
export function mapColumnsToValues (
columns : string [ ] ,
columns : string [ ] ,
values : any [ ] [ ]
values : unknown [ ] [ ] ,
) : Record < string , any > [ ] {
) : Record < string , unknown > [ ] {
return values . map ( row = > {
return values . map ( ( row ) = > {
const obj : Record < string , any > = { } ;
const obj : Record < string , unknown > = { } ;
columns . forEach ( ( column , index ) = > {
columns . forEach ( ( column , index ) = > {
obj [ column ] = row [ index ] ;
obj [ column ] = row [ index ] ;
} ) ;
} ) ;
return obj ;
return obj ;
} ) ;
} ) ;
}
}