From 1bec1b48526ce62d62c12859a6fafcfec23953a9 Mon Sep 17 00:00:00 2001 From: pedrofrxncx Date: Mon, 24 Aug 2026 18:48:54 -0300 Subject: [PATCH] refactor(storage): extract parseJsonColumn helper in organization-settings The get() method had the same 'string or already-parsed jsonb column' ternary inlined 7 times, one per settings field. Collapse it into one generic helper and call it per field with an explicit type argument (kysely's ColumnType select type doesn't infer cleanly through a bare generic, so the type argument is spelled out at each call site). --- apps/api/src/storage/organization-settings.ts | 63 +++++++++---------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/apps/api/src/storage/organization-settings.ts b/apps/api/src/storage/organization-settings.ts index b475276687..dff9bf2568 100644 --- a/apps/api/src/storage/organization-settings.ts +++ b/apps/api/src/storage/organization-settings.ts @@ -2,6 +2,15 @@ import { sql, type Kysely } from "kysely"; import type { Database, OrganizationSettings } from "./types"; import type { OrganizationSettingsStoragePort } from "./ports"; +/** + * jsonb columns come back as an already-parsed object from some drivers and + * as a raw JSON string from others — normalize both to the parsed shape. + */ +function parseJsonColumn(value: unknown): T | null { + if (!value) return null; + return (typeof value === "string" ? JSON.parse(value) : value) as T; +} + export class OrganizationSettingsStorage implements OrganizationSettingsStoragePort { @@ -20,42 +29,26 @@ export class OrganizationSettingsStorage return { organizationId: record.organizationId, - sidebar_items: record.sidebar_items - ? typeof record.sidebar_items === "string" - ? JSON.parse(record.sidebar_items) - : record.sidebar_items - : null, - enabled_plugins: record.enabled_plugins - ? typeof record.enabled_plugins === "string" - ? JSON.parse(record.enabled_plugins) - : record.enabled_plugins - : null, - registry_config: record.registry_config - ? typeof record.registry_config === "string" - ? JSON.parse(record.registry_config) - : record.registry_config - : null, - simple_mode: record.simple_mode - ? typeof record.simple_mode === "string" - ? JSON.parse(record.simple_mode) - : record.simple_mode - : null, - default_home_agents: record.default_home_agents - ? typeof record.default_home_agents === "string" - ? JSON.parse(record.default_home_agents) - : record.default_home_agents - : null, - flags: record.flags - ? typeof record.flags === "string" - ? JSON.parse(record.flags) - : record.flags - : null, + sidebar_items: parseJsonColumn( + record.sidebar_items, + ), + enabled_plugins: parseJsonColumn( + record.enabled_plugins, + ), + registry_config: parseJsonColumn( + record.registry_config, + ), + simple_mode: parseJsonColumn( + record.simple_mode, + ), + default_home_agents: parseJsonColumn< + OrganizationSettings["default_home_agents"] + >(record.default_home_agents), + flags: parseJsonColumn(record.flags), main_agent_id: record.main_agent_id ?? null, - sprint_config: record.sprint_config - ? typeof record.sprint_config === "string" - ? JSON.parse(record.sprint_config) - : record.sprint_config - : null, + sprint_config: parseJsonColumn( + record.sprint_config, + ), createdAt: record.createdAt, updatedAt: record.updatedAt, };