diff --git a/src/program-settings/program-settings.service.ts b/src/program-settings/program-settings.service.ts index 1d1de75..a3439dc 100644 --- a/src/program-settings/program-settings.service.ts +++ b/src/program-settings/program-settings.service.ts @@ -605,14 +605,48 @@ export class ProgramSettingsService implements OnModuleInit { } private async listTenantRows(tenantId: string): Promise { - const rows = await this.prisma.setting.findMany({ - where: { tenantId, deletedAt: null }, - orderBy: { key: 'asc' }, - }); - return rows - .filter((row: Setting) => !isRuntimeProgramSettingKey(row.key)) - .filter((row: Setting) => this.isSystemProgramSettingKey(row.key)) - .map((row: Setting) => this.toApiRow(row)); + await this.ensureTemplatesSeeded(); + const catalogKeys = [...DEPLOY_SETTING_CATALOG_KEYS].sort(); + + const [tenantRows, templates] = await Promise.all([ + this.prisma.setting.findMany({ + where: { tenantId, deletedAt: null, key: { in: catalogKeys } }, + }), + this.prisma.settingTemplate.findMany({ + where: { deletedAt: null, key: { in: catalogKeys } }, + }), + ]); + + const tenantByKey = new Map(tenantRows.map((row: Setting) => [row.key, row])); + const templateByKey = new Map(templates.map((row: SettingTemplate) => [row.key, row])); + const seedByKey = new Map( + this.readSeedRows() + .filter((row) => row.key && DEPLOY_SETTING_CATALOG_KEYS.has(String(row.key))) + .map((row) => [String(row.key), row]), + ); + + return catalogKeys + .filter((key) => !isRuntimeProgramSettingKey(key)) + .map((key) => { + const tenant = tenantByKey.get(key); + if (tenant) return this.toApiRow(tenant); + + const tpl = templateByKey.get(key); + if (tpl) return this.toApiRow(tpl); + + const seed = seedByKey.get(key); + return { + key, + value: seed?.value == null ? '' : String(seed.value), + title_fa: seed?.title_fa || key, + description: seed?.description || '', + type: seed?.type || '', + type_data: '', + is_active: seed?.is_active ?? 1, + can_edit: 0, + is_exist: 0, + }; + }); } private toApiRow(row: Setting | SettingTemplate): ProgramSettingRow {