Add per-business modules and home chart slots.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
0632f67bda
commit
9ca6e2306f
@@ -3,11 +3,17 @@ import {
|
||||
normalizeBusinessPrimaryColorId,
|
||||
} from './business-primary-colors';
|
||||
import {
|
||||
BUSINESS_MODULE_IDS,
|
||||
BusinessModuleId,
|
||||
BusinessSettings,
|
||||
DEFAULT_BUSINESS_DASHBOARD_LOCALE,
|
||||
DEFAULT_BUSINESS_SETTINGS,
|
||||
DEFAULT_ENABLED_BUSINESS_MODULES,
|
||||
DEFAULT_HOME_CHARTS,
|
||||
DEFAULT_ORDER_PROCESS_STEPS,
|
||||
HOME_CHART_IDS,
|
||||
type DashboardLocale,
|
||||
type HomeChartId,
|
||||
OrderProcessStep,
|
||||
} from './business-settings.types';
|
||||
import {
|
||||
@@ -19,6 +25,45 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
const BUSINESS_MODULE_ID_SET = new Set<string>(BUSINESS_MODULE_IDS);
|
||||
const HOME_CHART_ID_SET = new Set<string>(HOME_CHART_IDS);
|
||||
|
||||
export function isBusinessModuleId(value: unknown): value is BusinessModuleId {
|
||||
return typeof value === 'string' && BUSINESS_MODULE_ID_SET.has(value);
|
||||
}
|
||||
|
||||
export function isHomeChartId(value: unknown): value is HomeChartId {
|
||||
return typeof value === 'string' && HOME_CHART_ID_SET.has(value);
|
||||
}
|
||||
|
||||
/** Dedupes and orders enabled modules by canonical BUSINESS_MODULE_IDS order. */
|
||||
export function normalizeEnabledBusinessModules(
|
||||
value: unknown,
|
||||
): BusinessModuleId[] {
|
||||
if (!Array.isArray(value)) {
|
||||
return [...DEFAULT_ENABLED_BUSINESS_MODULES];
|
||||
}
|
||||
|
||||
const selected = new Set<BusinessModuleId>();
|
||||
for (const item of value) {
|
||||
if (isBusinessModuleId(item)) {
|
||||
selected.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
return BUSINESS_MODULE_IDS.filter((id) => selected.has(id));
|
||||
}
|
||||
|
||||
export function normalizeHomeCharts(value: unknown): [HomeChartId, HomeChartId] {
|
||||
if (!Array.isArray(value)) {
|
||||
return [...DEFAULT_HOME_CHARTS];
|
||||
}
|
||||
|
||||
const first = isHomeChartId(value[0]) ? value[0] : DEFAULT_HOME_CHARTS[0];
|
||||
const second = isHomeChartId(value[1]) ? value[1] : DEFAULT_HOME_CHARTS[1];
|
||||
return [first, second];
|
||||
}
|
||||
|
||||
export function normalizeDashboardLocale(value: unknown): DashboardLocale {
|
||||
return value === 'en' || value === 'fa' ? value : DEFAULT_BUSINESS_DASHBOARD_LOCALE;
|
||||
}
|
||||
@@ -70,6 +115,8 @@ export function normalizeBusinessSettings(raw: unknown): BusinessSettings {
|
||||
? dashboard.expertReviews
|
||||
: {};
|
||||
const store = isRecord(source.store) ? source.store : {};
|
||||
const modules = isRecord(source.modules) ? source.modules : {};
|
||||
const hasModulesKey = Object.prototype.hasOwnProperty.call(source, 'modules');
|
||||
|
||||
return {
|
||||
branding: {
|
||||
@@ -97,6 +144,15 @@ export function normalizeBusinessSettings(raw: unknown): BusinessSettings {
|
||||
),
|
||||
orderProcessSteps: readOrderProcessSteps(store.orderProcessSteps),
|
||||
},
|
||||
modules: {
|
||||
// Missing `modules` → all enabled (legacy). Explicit `{ enabled: [] }` stays empty.
|
||||
enabled: hasModulesKey
|
||||
? Array.isArray(modules.enabled)
|
||||
? normalizeEnabledBusinessModules(modules.enabled)
|
||||
: []
|
||||
: [...DEFAULT_ENABLED_BUSINESS_MODULES],
|
||||
charts: normalizeHomeCharts(modules.charts),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -129,6 +185,10 @@ export function mergeBusinessSettings(
|
||||
orderProcessSteps:
|
||||
patch.store?.orderProcessSteps ?? current.store.orderProcessSteps,
|
||||
},
|
||||
modules: {
|
||||
enabled: patch.modules?.enabled ?? current.modules.enabled,
|
||||
charts: patch.modules?.charts ?? current.modules.charts,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user