feat: add settings module (#13175)

* feat: add view_configurations feature flag

  - Add feature flag provider and hooks to admin dashboard
  - Add backend API endpoint for feature flags
  - Create view_configurations feature flag (disabled by default)
  - Update order list table to use legacy version when flag is disabled
  - Can be enabled with MEDUSA_FF_VIEW_CONFIGURATIONS=true env var

* fix: naming

* fix: feature flags unauthenticated

* fix: add test

* feat: add settings module

* fix: deps

* fix: cleanup

* fix: add more tetsts

* fix: rm changelog

* fix: deps

* fix: add settings module to default modules list

* fix: pr comments

* fix: deps,build

* fix: alias

* fix: tests

* fix: update snapshots
This commit is contained in:
Sebastian Rindom
2025-08-15 10:59:54 +02:00
committed by GitHub
parent 9b37a2c9f4
commit f7fc05307f
28 changed files with 1736 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
import { SettingsModuleService } from "@/services"
import { Module } from "@medusajs/framework/utils"
import { Modules } from "@medusajs/utils"
export default Module(Modules.SETTINGS, {
service: SettingsModuleService,
})
@@ -0,0 +1,269 @@
{
"namespaces": [
"public"
],
"name": "public",
"tables": [
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"user_id": {
"name": "user_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"key": {
"name": "key",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"value": {
"name": "value",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "json"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "user_preference",
"schema": "public",
"indexes": [
{
"keyName": "IDX_user_preference_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_user_preference_deleted_at\" ON \"user_preference\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_user_preference_user_id_key_unique",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_user_preference_user_id_key_unique\" ON \"user_preference\" (user_id, key) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_user_preference_user_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_user_preference_user_id\" ON \"user_preference\" (user_id) WHERE deleted_at IS NULL"
},
{
"keyName": "user_preference_pkey",
"columnNames": [
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {},
"nativeEnums": {}
},
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"entity": {
"name": "entity",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"user_id": {
"name": "user_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"is_system_default": {
"name": "is_system_default",
"type": "boolean",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "false",
"mappedType": "boolean"
},
"configuration": {
"name": "configuration",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "json"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "view_configuration",
"schema": "public",
"indexes": [
{
"keyName": "IDX_view_configuration_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_view_configuration_deleted_at\" ON \"view_configuration\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_view_configuration_entity_user_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_view_configuration_entity_user_id\" ON \"view_configuration\" (entity, user_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_view_configuration_entity_is_system_default",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_view_configuration_entity_is_system_default\" ON \"view_configuration\" (entity, is_system_default) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_view_configuration_user_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_view_configuration_user_id\" ON \"view_configuration\" (user_id) WHERE deleted_at IS NULL"
},
{
"keyName": "view_configuration_pkey",
"columnNames": [
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {},
"nativeEnums": {}
}
],
"nativeEnums": {}
}
@@ -0,0 +1,19 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20250717162007 extends Migration {
override async up(): Promise<void> {
this.addSql(`alter table if exists "user_preference" drop constraint if exists "user_preference_user_id_key_unique";`);
this.addSql(`create table if not exists "user_preference" ("id" text not null, "user_id" text not null, "key" text not null, "value" jsonb not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "user_preference_pkey" primary key ("id"));`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_user_preference_deleted_at" ON "user_preference" (deleted_at) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_user_preference_user_id_key_unique" ON "user_preference" (user_id, key) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_user_preference_user_id" ON "user_preference" (user_id) WHERE deleted_at IS NULL;`);
this.addSql(`create table if not exists "view_configuration" ("id" text not null, "entity" text not null, "name" text null, "user_id" text null, "is_system_default" boolean not null default false, "configuration" jsonb not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "view_configuration_pkey" primary key ("id"));`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_view_configuration_deleted_at" ON "view_configuration" (deleted_at) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_view_configuration_entity_user_id" ON "view_configuration" (entity, user_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_view_configuration_entity_is_system_default" ON "view_configuration" (entity, is_system_default) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_view_configuration_user_id" ON "view_configuration" (user_id) WHERE deleted_at IS NULL;`);
}
}
@@ -0,0 +1,2 @@
export { ViewConfiguration } from "./view-configuration"
export { UserPreference } from "./user-preference"
@@ -0,0 +1,18 @@
import { model } from "@medusajs/framework/utils"
export const UserPreference = model
.define("user_preference", {
id: model.id({ prefix: "usrpref" }).primaryKey(),
user_id: model.text(),
key: model.text().searchable(),
value: model.json(),
})
.indexes([
{
on: ["user_id", "key"],
unique: true,
},
{
on: ["user_id"],
},
])
@@ -0,0 +1,22 @@
import { model } from "@medusajs/framework/utils"
export const ViewConfiguration = model
.define("view_configuration", {
id: model.id({ prefix: "vconf" }).primaryKey(),
entity: model.text().searchable(),
name: model.text().searchable().nullable(),
user_id: model.text().nullable(),
is_system_default: model.boolean().default(false),
configuration: model.json(),
})
.indexes([
{
on: ["entity", "user_id"],
},
{
on: ["entity", "is_system_default"],
},
{
on: ["user_id"],
},
])
@@ -0,0 +1 @@
export { default as SettingsModuleService } from "./settings-module-service"
@@ -0,0 +1,365 @@
import {
Context,
DAL,
InferEntityType,
InternalModuleDeclaration,
ModulesSdkTypes,
SettingsTypes,
} from "@medusajs/framework/types"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
MedusaError,
MedusaService,
} from "@medusajs/framework/utils"
import { ViewConfiguration, UserPreference } from "@/models"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
viewConfigurationService: ModulesSdkTypes.IMedusaInternalService<any>
userPreferenceService: ModulesSdkTypes.IMedusaInternalService<any>
}
export default class SettingsModuleService
extends MedusaService<{
ViewConfiguration: { dto: SettingsTypes.ViewConfigurationDTO }
UserPreference: { dto: SettingsTypes.UserPreferenceDTO }
}>({ ViewConfiguration, UserPreference })
implements SettingsTypes.ISettingsModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly viewConfigurationService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof ViewConfiguration>
>
protected readonly userPreferenceService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof UserPreference>
>
constructor(
{
baseRepository,
viewConfigurationService,
userPreferenceService,
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
) {
super(...arguments)
this.baseRepository_ = baseRepository
this.viewConfigurationService_ = viewConfigurationService
this.userPreferenceService_ = userPreferenceService
}
@InjectTransactionManager()
// @ts-expect-error
async createViewConfigurations(
data:
| SettingsTypes.CreateViewConfigurationDTO
| SettingsTypes.CreateViewConfigurationDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<
SettingsTypes.ViewConfigurationDTO | SettingsTypes.ViewConfigurationDTO[]
> {
// Convert to array for validation only
const isArrayInput = Array.isArray(data)
const dataArray = isArrayInput ? data : [data]
// Validate system defaults
for (const config of dataArray) {
if (config.is_system_default && config.user_id) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"System default view configurations cannot have a user_id"
)
}
if (config.is_system_default) {
// Check if a system default already exists for this entity
const existingDefault = await this.viewConfigurationService_.list(
{
entity: config.entity,
is_system_default: true,
},
{ select: ["id"] },
sharedContext
)
if (existingDefault.length > 0) {
throw new MedusaError(
MedusaError.Types.DUPLICATE_ERROR,
`A system default view configuration already exists for entity: ${config.entity}`
)
}
}
}
const result = await super.createViewConfigurations(
dataArray,
sharedContext
)
return isArrayInput ? result : result[0]
}
@InjectTransactionManager()
// @ts-expect-error
async updateViewConfigurations(
idOrSelector: string | SettingsTypes.FilterableViewConfigurationProps,
data: SettingsTypes.UpdateViewConfigurationDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<
SettingsTypes.ViewConfigurationDTO | SettingsTypes.ViewConfigurationDTO[]
> {
let selector: SettingsTypes.FilterableViewConfigurationProps = {}
if (typeof idOrSelector === "string") {
selector = { id: idOrSelector }
} else {
selector = idOrSelector
}
// Special handling for configuration updates to ensure replacement instead of merge
if (data.configuration) {
// First, get the entities to update
const entities = await this.viewConfigurationService_.list(
selector,
{},
sharedContext
)
if (entities.length === 0) {
return typeof idOrSelector === "string" ? [] : []
}
// Use upsertWithReplace to update the configuration field without merging
const updateDataArray = entities.map((entity) => ({
id: entity.id,
...data,
configuration: {
visible_columns: data.configuration?.visible_columns ?? [],
column_order: data.configuration?.column_order ?? [],
column_widths:
data.configuration?.column_widths !== undefined
? data.configuration.column_widths
: {},
filters:
data.configuration?.filters !== undefined
? data.configuration.filters
: {},
sorting:
data.configuration?.sorting !== undefined
? data.configuration.sorting
: null,
search:
data.configuration?.search !== undefined
? data.configuration.search
: "",
},
}))
// Use upsertWithReplace which uses nativeUpdateMany internally and doesn't merge JSON fields
const { entities: updatedEntities } =
await this.viewConfigurationService_.upsertWithReplace(
updateDataArray,
{ relations: [] },
sharedContext
)
const serialized = await this.baseRepository_.serialize<
SettingsTypes.ViewConfigurationDTO[]
>(updatedEntities, { populate: true })
return typeof idOrSelector === "string" ? serialized[0] : serialized
}
// For non-configuration updates, use the standard update method
const updated = await this.viewConfigurationService_.update(
{ selector, data },
sharedContext
)
const serialized = await this.baseRepository_.serialize<
SettingsTypes.ViewConfigurationDTO[]
>(updated, { populate: true })
return typeof idOrSelector === "string" ? serialized[0] : serialized
}
@InjectManager()
async getUserPreference(
userId: string,
key: string,
@MedusaContext() sharedContext: Context = {}
): Promise<SettingsTypes.UserPreferenceDTO | null> {
const prefs = await this.userPreferenceService_.list(
{ user_id: userId, key },
{},
sharedContext
)
if (prefs.length === 0) {
return null
}
return await this.baseRepository_.serialize<SettingsTypes.UserPreferenceDTO>(
prefs[0],
{ populate: true }
)
}
@InjectTransactionManager()
async setUserPreference(
userId: string,
key: string,
value: any,
@MedusaContext() sharedContext: Context = {}
): Promise<SettingsTypes.UserPreferenceDTO> {
const existing = await this.userPreferenceService_.list(
{ user_id: userId, key },
{ select: ["id"] },
sharedContext
)
let result: InferEntityType<typeof UserPreference>
if (existing.length > 0) {
const updated = await this.userPreferenceService_.update(
[{ id: existing[0].id, value }],
sharedContext
)
result = updated[0]
} else {
const created = await this.userPreferenceService_.create(
{ user_id: userId, key, value },
sharedContext
)
result = created[0]
}
return await this.baseRepository_.serialize<SettingsTypes.UserPreferenceDTO>(
result,
{ populate: true }
)
}
@InjectManager()
async getActiveViewConfiguration(
entity: string,
userId: string,
@MedusaContext() sharedContext: Context = {}
): Promise<SettingsTypes.ViewConfigurationDTO | null> {
// Check if user has an active view preference
const activeViewPref = await this.getUserPreference(
userId,
`active_view.${entity}`,
sharedContext
)
// Check if we have a preference with a view configuration ID (not explicitly null)
if (
activeViewPref &&
activeViewPref.value?.viewConfigurationId &&
activeViewPref.value.viewConfigurationId !== null
) {
try {
return await this.retrieveViewConfiguration(
activeViewPref.value.viewConfigurationId,
{},
sharedContext
)
} catch (error) {
// View configuration might have been deleted
}
}
// If we have an explicit null preference, or no preference, or a deleted view
// We should check for defaults in this order:
// Check if user has any personal views (only if no explicit null preference)
if (!activeViewPref || activeViewPref.value?.viewConfigurationId !== null) {
const [personalView] = await this.listViewConfigurations(
{ entity, user_id: userId },
{ take: 1, order: { created_at: "ASC" } },
sharedContext
)
if (personalView) {
return personalView
}
}
// Fall back to system default
const systemDefaults = await this.listViewConfigurations(
{ entity, is_system_default: true },
{},
sharedContext
)
return systemDefaults.length > 0 ? systemDefaults[0] : null
}
@InjectTransactionManager()
async setActiveViewConfiguration(
entity: string,
userId: string,
viewConfigurationId: string,
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
// Verify the view configuration exists and user has access
const viewConfig = await this.retrieveViewConfiguration(
viewConfigurationId,
{},
sharedContext
)
if (viewConfig.entity !== entity) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`View configuration ${viewConfigurationId} is not for entity ${entity}`
)
}
if (viewConfig.user_id && viewConfig.user_id !== userId) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
`User ${userId} does not have access to view configuration ${viewConfigurationId}`
)
}
await this.setUserPreference(
userId,
`active_view.${entity}`,
{ viewConfigurationId },
sharedContext
)
}
@InjectManager()
async getSystemDefaultViewConfiguration(
entity: string,
@MedusaContext() sharedContext: Context = {}
): Promise<SettingsTypes.ViewConfigurationDTO | null> {
const systemDefaults = await this.listViewConfigurations(
{ entity, is_system_default: true },
{},
sharedContext
)
return systemDefaults.length > 0 ? systemDefaults[0] : null
}
@InjectTransactionManager()
async clearActiveViewConfiguration(
entity: string,
userId: string,
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
// Instead of deleting, set the preference to null
// This ensures we're using the same transaction pattern as setActiveViewConfiguration
await this.setUserPreference(
userId,
`active_view.${entity}`,
{ viewConfigurationId: null },
sharedContext
)
}
}
@@ -0,0 +1,5 @@
import { Logger } from "@medusajs/framework/types"
export type InitializeModuleInjectableDependencies = {
logger?: Logger
}