From 1592ae004269f6fb59b5febd14906ece6d2a5a6f Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 27 Dec 2022 13:11:00 -0600 Subject: [PATCH] Move to core instead of livechat --- .../livechat/server/settingsRegenerator.ts | 28 ------------- apps/meteor/app/livechat/server/startup.js | 3 -- apps/meteor/server/lib/settingsRegenerator.ts | 40 +++++++++++++++++++ apps/meteor/server/startup/index.ts | 1 + 4 files changed, 41 insertions(+), 31 deletions(-) delete mode 100644 apps/meteor/app/livechat/server/settingsRegenerator.ts create mode 100644 apps/meteor/server/lib/settingsRegenerator.ts diff --git a/apps/meteor/app/livechat/server/settingsRegenerator.ts b/apps/meteor/app/livechat/server/settingsRegenerator.ts deleted file mode 100644 index 95f238ecaef8..000000000000 --- a/apps/meteor/app/livechat/server/settingsRegenerator.ts +++ /dev/null @@ -1,28 +0,0 @@ -// Validates settings on DB are correct on structure -// And deletes invalid ones -import { Settings } from '@rocket.chat/models'; - -import { Logger } from '../../../server/lib/logger/Logger'; - -// Validates settings on DB are correct on structure by matching the ones missing all the required fields -const logger = new Logger('SettingsRegenerator'); -export async function settingsRegenerator() { - const invalidSettings = await Settings.find({ - // Putting the $and explicit to ensure it's "intentional" - $and: [ - { value: { $exists: false } }, - { type: { $exists: false } }, - { public: { $exists: false } }, - { packageValue: { $exists: false } }, - { blocked: { $exists: false } }, - { sorter: { $exists: false } }, - { i18nLabel: { $exists: false } }, - ], - }).toArray(); - - if (invalidSettings.length > 0) { - logger.warn(`Found ${invalidSettings.length} invalid settings on DB. Deleting them...`); - logger.warn(invalidSettings.map(({ _id }) => _id)); - await Settings.deleteMany({ _id: { $in: invalidSettings.map(({ _id }) => _id) } }); - } -} diff --git a/apps/meteor/app/livechat/server/startup.js b/apps/meteor/app/livechat/server/startup.js index 97a6f913aec3..f9dcf3201f81 100644 --- a/apps/meteor/app/livechat/server/startup.js +++ b/apps/meteor/app/livechat/server/startup.js @@ -12,7 +12,6 @@ import { createDefaultBusinessHourIfNotExists } from './business-hour/Helper'; import { hasPermission } from '../../authorization/server'; import { Livechat } from './lib/Livechat'; import { RoutingManager } from './lib/RoutingManager'; -import { settingsRegenerator } from './settingsRegenerator'; import './roomAccessValidator.internalService'; @@ -84,6 +83,4 @@ Meteor.startup(async () => { !user?.roles?.includes('bot') && Livechat.setUserStatusLivechatIf(user._id, 'not-available', {}, { livechatStatusSystemModified: true }), ); - - await settingsRegenerator(); }); diff --git a/apps/meteor/server/lib/settingsRegenerator.ts b/apps/meteor/server/lib/settingsRegenerator.ts new file mode 100644 index 000000000000..954a896343cd --- /dev/null +++ b/apps/meteor/server/lib/settingsRegenerator.ts @@ -0,0 +1,40 @@ +// Validates settings on DB are correct on structure +// And deletes invalid ones +import { Meteor } from 'meteor/meteor'; +import { Settings } from '@rocket.chat/models'; + +import { Logger } from './logger/Logger'; + +// Validates settings on DB are correct on structure by matching the ones missing all the required fields +const logger = new Logger('SettingsRegenerator'); +export async function settingsRegenerator() { + const invalidSettings = await Settings.find( + { + // Putting the $and explicit to ensure it's "intentional" + $and: [ + { value: { $exists: false } }, + { type: { $exists: false } }, + { public: { $exists: false } }, + { packageValue: { $exists: false } }, + { blocked: { $exists: false } }, + { sorter: { $exists: false } }, + { i18nLabel: { $exists: false } }, + ], + }, + { projection: { _id: 1 } }, + ).toArray(); + + if (invalidSettings.length > 0) { + logger.warn({ + msg: 'Invalid settings found on DB. Deleting them.', + settings: invalidSettings.map(({ _id }) => _id), + }); + await Settings.deleteMany({ _id: { $in: invalidSettings.map(({ _id }) => _id) } }); + } else { + logger.info('No invalid settings found on DB.'); + } +} + +Meteor.startup(async () => { + await settingsRegenerator(); +}); diff --git a/apps/meteor/server/startup/index.ts b/apps/meteor/server/startup/index.ts index cb2ef39b7a9e..86385bd15623 100644 --- a/apps/meteor/server/startup/index.ts +++ b/apps/meteor/server/startup/index.ts @@ -9,6 +9,7 @@ import './coreApps'; import './presenceTroubleshoot'; import '../hooks'; import '../lib/rooms/roomTypes'; +import '../lib/settingsRegenerator'; import { isRunningMs } from '../lib/isRunningMs'; // only starts network broker if running in micro services mode