| 
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -123,6 +123,8 @@ export async function retrieveSettingsForActiveAccount(): Promise<Settings> { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					let lastCleanupDate: string | null = null; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					/** | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * Logs a message to the database with proper handling of concurrent writes | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * @param message - The message to log | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -135,65 +137,29 @@ export async function logToDb(message: string): Promise<void> { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  try { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    // Try to insert first, if it fails due to UNIQUE constraint, update instead
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    try { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      todayKey, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      fullMessage, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    ]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } catch (error) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      // If insert fails due to UNIQUE constraint, update instead
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      if ( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        error instanceof Error && | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        error.message.includes("UNIQUE constraint failed") | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      ) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        const result = await platform.dbQuery( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					          "SELECT message FROM logs WHERE date = ?", | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					          [todayKey], | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        ); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        if (result && result.values.length > 0) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					          const prevMessages = result.values[0][0] as string; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					          const updatedMessage = `${prevMessages}\n${fullMessage}`; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					          await platform.dbExec("UPDATE logs SET message = ? WHERE date = ?", [ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            updatedMessage, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            todayKey, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					          ]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      } else { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        // If it's a different error, rethrow it
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        throw error; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    // Clean up old logs (keep only last 7 days) - do this less frequently
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    // Only clean up if the date is different from the last cleanup
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    const lastCleanupKey = "last_log_cleanup"; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    const result = await platform.dbQuery( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      "SELECT value FROM settings WHERE key = ?", | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      [lastCleanupKey], | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    ); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    const lastCleanup = result?.values[0]?.[0] as string; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if (!lastCleanup || lastCleanup !== todayKey) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if (!lastCleanupDate || lastCleanupDate !== todayKey) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      const sevenDaysAgo = new Date(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      await platform.dbExec("DELETE FROM logs WHERE date < ?", [ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        sevenDaysAgo.toDateString(), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      ]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      // Update last cleanup date
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      await platform.dbExec( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        "INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)", | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        [lastCleanupKey, todayKey], | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      ); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  } catch (error) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    // Log to console as fallback
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    // eslint-disable-next-line no-console
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    console.error("Failed to log to database:", error); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    // eslint-disable-next-line no-console
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    console.error("Original message:", message); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    console.error( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      "Error logging to database:", | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      error, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      " ... for original message:", | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      message, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    ); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
	
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
				
				 | 
				
					
  |