fix: Convert searchBoxes arrays to JSON strings in $saveSettings and $updateSettings

- Add _convertSettingsForStorage helper method to handle Settings → SettingsWithJsonStrings conversion
- Fix $saveSettings and $saveUserSettings to properly convert searchBoxes arrays to JSON strings before database storage
- Update SearchAreaView.vue to use array format instead of manual JSON.stringify conversion
- Add comprehensive test UI in PlatformServiceMixinTest.vue with visual feedback and clear demonstration of conversion process
- Document migration strategy for consolidating $updateSettings into $saveSettings to reduce code duplication
- Add deprecation notices to $updateSettings method with clear migration guidance

The fix ensures that searchBoxes arrays are properly converted to JSON strings before database storage, preventing data corruption and maintaining consistency with the SettingsWithJsonStrings type definition. The enhanced test interface provides clear visualization of the conversion process and database storage format.

Migration Strategy:
- $saveSettings:  KEEP (will be primary method after consolidation)
- $updateSettings: ⚠️ DEPRECATED (will be removed in favor of $saveSettings)
- Future: Consolidate to single $saveSettings(changes, did?) method

Files changed:
- src/utils/PlatformServiceMixin.ts: Add conversion helper, fix save methods, add deprecation notices
- src/views/SearchAreaView.vue: Remove manual JSON conversion
- src/test/PlatformServiceMixinTest.vue: Add comprehensive test UI with highlighting
- docs/migration-templates/updateSettings-consolidation-plan.md: Document future consolidation strategy
This commit is contained in:
Matthew Raymer
2025-07-30 09:48:52 +00:00
parent 118e93b85a
commit 9067bec54a
4 changed files with 359 additions and 13 deletions

View File

@@ -44,7 +44,11 @@ import type {
PlatformService,
PlatformCapabilities,
} from "@/services/PlatformService";
import { MASTER_SETTINGS_KEY, type Settings } from "@/db/tables/settings";
import {
MASTER_SETTINGS_KEY,
type Settings,
type SettingsWithJsonStrings,
} from "@/db/tables/settings";
import { logger } from "@/utils/logger";
import { Contact } from "@/db/tables/contacts";
import { Account } from "@/db/tables/accounts";
@@ -265,6 +269,27 @@ export const PlatformServiceMixin = {
return (value as T) || defaultValue;
},
/**
* Convert Settings object to SettingsWithJsonStrings for database storage
* Handles conversion of complex objects like searchBoxes to JSON strings
* @param settings Settings object to convert
* @returns SettingsWithJsonStrings object ready for database storage
*/
_convertSettingsForStorage(
settings: Partial<Settings>,
): Partial<SettingsWithJsonStrings> {
const converted = { ...settings } as Partial<SettingsWithJsonStrings>;
// Convert searchBoxes array to JSON string if present
if (settings.searchBoxes !== undefined) {
(converted as any).searchBoxes = Array.isArray(settings.searchBoxes)
? JSON.stringify(settings.searchBoxes)
: String(settings.searchBoxes);
}
return converted;
},
// // =================================================
// // CACHING UTILITY METHODS
// // =================================================
@@ -755,6 +780,9 @@ export const PlatformServiceMixin = {
/**
* Save default settings - $saveSettings()
* Ultra-concise shortcut for updateDefaultSettings
*
* ✅ KEEP: This method will be the primary settings save method after consolidation
*
* @param changes Settings changes to save
* @returns Promise<boolean> Success status
*/
@@ -769,10 +797,13 @@ export const PlatformServiceMixin = {
if (Object.keys(safeChanges).length === 0) return true;
// Convert settings for database storage (handles searchBoxes conversion)
const convertedChanges = this._convertSettingsForStorage(safeChanges);
const setParts: string[] = [];
const params: unknown[] = [];
Object.entries(safeChanges).forEach(([key, value]) => {
Object.entries(convertedChanges).forEach(([key, value]) => {
if (value !== undefined) {
setParts.push(`${key} = ?`);
params.push(value);
@@ -819,10 +850,13 @@ export const PlatformServiceMixin = {
if (Object.keys(safeChanges).length === 0) return true;
// Convert settings for database storage (handles searchBoxes conversion)
const convertedChanges = this._convertSettingsForStorage(safeChanges);
const setParts: string[] = [];
const params: unknown[] = [];
Object.entries(safeChanges).forEach(([key, value]) => {
Object.entries(convertedChanges).forEach(([key, value]) => {
if (value !== undefined) {
setParts.push(`${key} = ?`);
params.push(value);
@@ -1193,6 +1227,17 @@ export const PlatformServiceMixin = {
* @param did Optional DID for user-specific settings
* @returns Promise<boolean> Success status
*/
/**
* Update settings - $updateSettings()
* Ultra-concise shortcut for updating settings (default or user-specific)
*
* ⚠️ DEPRECATED: This method will be removed in favor of $saveSettings()
* Use $saveSettings(changes, did?) instead for better consistency
*
* @param changes Settings changes to save
* @param did Optional DID for user-specific settings
* @returns Promise<boolean> Success status
*/
async $updateSettings(
changes: Partial<Settings>,
did?: string,