From d7905a3dd553efb083644c08a24f042c27c4348e Mon Sep 17 00:00:00 2001
From: Kevin Aleman <kaleman960@gmail.com>
Date: Thu, 7 Dec 2023 09:26:47 -0600
Subject: [PATCH] refactor: Remove `livechat.js` file (#31179)

---
 apps/meteor/app/livechat/client/{ui.js => ui.ts}       |  0
 apps/meteor/app/livechat/imports/server/rest/sms.js    |  6 ++++--
 apps/meteor/app/livechat/server/api/v1/webhooks.ts     | 10 ++++++----
 apps/meteor/app/livechat/server/index.ts               |  2 --
 apps/meteor/app/livechat/server/lib/Helper.ts          |  3 +--
 apps/meteor/app/livechat/server/lib/Livechat.js        |  9 ---------
 .../livechat/server/methods/getAgentOverviewData.ts    |  2 --
 .../livechat/server/methods/getAnalyticsChartData.ts   |  3 ---
 .../server/methods/getAnalyticsOverviewData.ts         |  2 --
 .../livechat/server/methods/removeAllClosedRooms.ts    |  6 ++++--
 apps/meteor/app/livechat/server/startup.ts             |  6 ++++--
 11 files changed, 19 insertions(+), 30 deletions(-)
 rename apps/meteor/app/livechat/client/{ui.js => ui.ts} (100%)
 delete mode 100644 apps/meteor/app/livechat/server/lib/Livechat.js

diff --git a/apps/meteor/app/livechat/client/ui.js b/apps/meteor/app/livechat/client/ui.ts
similarity index 100%
rename from apps/meteor/app/livechat/client/ui.js
rename to apps/meteor/app/livechat/client/ui.ts
diff --git a/apps/meteor/app/livechat/imports/server/rest/sms.js b/apps/meteor/app/livechat/imports/server/rest/sms.js
index 9d2bee13378..117407807da 100644
--- a/apps/meteor/app/livechat/imports/server/rest/sms.js
+++ b/apps/meteor/app/livechat/imports/server/rest/sms.js
@@ -1,5 +1,6 @@
 import { OmnichannelIntegration } from '@rocket.chat/core-services';
 import { OmnichannelSourceType } from '@rocket.chat/core-typings';
+import { Logger } from '@rocket.chat/logger';
 import { LivechatVisitors, LivechatRooms, LivechatDepartment } from '@rocket.chat/models';
 import { Random } from '@rocket.chat/random';
 import { serverFetch as fetch } from '@rocket.chat/server-fetch';
@@ -8,9 +9,10 @@ import { Meteor } from 'meteor/meteor';
 import { API } from '../../../../api/server';
 import { FileUpload } from '../../../../file-upload/server';
 import { settings } from '../../../../settings/server';
-import { Livechat } from '../../../server/lib/Livechat';
 import { Livechat as LivechatTyped } from '../../../server/lib/LivechatTyped';
 
+const logger = new Logger('SMS');
+
 const getUploadFile = async (details, fileUrl) => {
 	const response = await fetch(fileUrl);
 
@@ -156,7 +158,7 @@ API.v1.addRoute('livechat/sms-incoming/:service', {
 					attachment.title_link_download = true;
 				}
 			} catch (err) {
-				Livechat.logger.error({ msg: 'Attachment upload failed', err });
+				logger.error({ msg: 'Attachment upload failed', err });
 				attachment = {
 					fields: [
 						{
diff --git a/apps/meteor/app/livechat/server/api/v1/webhooks.ts b/apps/meteor/app/livechat/server/api/v1/webhooks.ts
index e282e2bd548..af7c97d5145 100644
--- a/apps/meteor/app/livechat/server/api/v1/webhooks.ts
+++ b/apps/meteor/app/livechat/server/api/v1/webhooks.ts
@@ -1,8 +1,10 @@
+import { Logger } from '@rocket.chat/logger';
 import { serverFetch as fetch } from '@rocket.chat/server-fetch';
 
 import { API } from '../../../../api/server';
 import { settings } from '../../../../settings/server';
-import { Livechat } from '../../lib/Livechat';
+
+const logger = new Logger('WebhookTest');
 
 API.v1.addRoute(
 	'livechat/webhook.test',
@@ -70,18 +72,18 @@ API.v1.addRoute(
 			}
 
 			try {
-				Livechat.logger.debug(`Testing webhook ${webhookUrl}`);
+				logger.debug(`Testing webhook ${webhookUrl}`);
 				const request = await fetch(webhookUrl, options);
 				const response = await request.text();
 
-				Livechat.logger.debug({ response });
+				logger.debug({ response });
 				if (request.status === 200) {
 					return API.v1.success();
 				}
 
 				throw new Error('Invalid status code');
 			} catch (error) {
-				Livechat.logger.error(`Error testing webhook: ${error}`);
+				logger.error(`Error testing webhook: ${error}`);
 				throw new Error('error-invalid-webhook-response');
 			}
 		},
diff --git a/apps/meteor/app/livechat/server/index.ts b/apps/meteor/app/livechat/server/index.ts
index 06ad84ec49a..7b650f5b296 100644
--- a/apps/meteor/app/livechat/server/index.ts
+++ b/apps/meteor/app/livechat/server/index.ts
@@ -76,5 +76,3 @@ import './api';
 import './api/rest';
 import './externalFrame';
 import './methods/saveBusinessHour';
-
-export { Livechat } from './lib/Livechat';
diff --git a/apps/meteor/app/livechat/server/lib/Helper.ts b/apps/meteor/app/livechat/server/lib/Helper.ts
index 78351cdc668..f360004fc77 100644
--- a/apps/meteor/app/livechat/server/lib/Helper.ts
+++ b/apps/meteor/app/livechat/server/lib/Helper.ts
@@ -38,7 +38,6 @@ import { hasRoleAsync } from '../../../authorization/server/functions/hasRole';
 import { sendNotification } from '../../../lib/server';
 import { sendMessage } from '../../../lib/server/functions/sendMessage';
 import { settings } from '../../../settings/server';
-import { Livechat } from './Livechat';
 import { Livechat as LivechatTyped } from './LivechatTyped';
 import { queueInquiry, saveQueueInquiry } from './QueueManager';
 import { RoutingManager } from './RoutingManager';
@@ -296,7 +295,7 @@ export const parseAgentCustomFields = (customFields?: Record<string, any>) => {
 			const parseCustomFields = JSON.parse(accountCustomFields);
 			return Object.keys(parseCustomFields).filter((customFieldKey) => parseCustomFields[customFieldKey].sendToIntegrations === true);
 		} catch (error) {
-			Livechat.logger.error(error);
+			logger.error(error);
 			return [];
 		}
 	};
diff --git a/apps/meteor/app/livechat/server/lib/Livechat.js b/apps/meteor/app/livechat/server/lib/Livechat.js
deleted file mode 100644
index a47d472f289..00000000000
--- a/apps/meteor/app/livechat/server/lib/Livechat.js
+++ /dev/null
@@ -1,9 +0,0 @@
-// Note: Please don't add any new methods to this file, since its still in js and we are migrating to ts
-// Please add new methods to LivechatTyped.ts
-import { Logger } from '@rocket.chat/logger';
-
-const logger = new Logger('Livechat');
-
-export const Livechat = {
-	logger,
-};
diff --git a/apps/meteor/app/livechat/server/methods/getAgentOverviewData.ts b/apps/meteor/app/livechat/server/methods/getAgentOverviewData.ts
index 182cd238ec9..b5ea8dafe92 100644
--- a/apps/meteor/app/livechat/server/methods/getAgentOverviewData.ts
+++ b/apps/meteor/app/livechat/server/methods/getAgentOverviewData.ts
@@ -6,7 +6,6 @@ import { Meteor } from 'meteor/meteor';
 
 import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
 import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
-import { Livechat } from '../lib/Livechat';
 
 declare module '@rocket.chat/ui-contexts' {
 	// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -27,7 +26,6 @@ Meteor.methods<ServerMethods>({
 		}
 
 		if (!options.chartOptions?.name) {
-			Livechat.logger.warn('Incorrect analytics options');
 			return;
 		}
 
diff --git a/apps/meteor/app/livechat/server/methods/getAnalyticsChartData.ts b/apps/meteor/app/livechat/server/methods/getAnalyticsChartData.ts
index b5b1003969f..85c8e033b8f 100644
--- a/apps/meteor/app/livechat/server/methods/getAnalyticsChartData.ts
+++ b/apps/meteor/app/livechat/server/methods/getAnalyticsChartData.ts
@@ -5,7 +5,6 @@ import type { ServerMethods } from '@rocket.chat/ui-contexts';
 import { Meteor } from 'meteor/meteor';
 
 import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
-import { Livechat } from '../lib/Livechat';
 
 declare module '@rocket.chat/ui-contexts' {
 	// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -24,14 +23,12 @@ Meteor.methods<ServerMethods>({
 		}
 
 		if (!options.chartOptions?.name) {
-			Livechat.logger.error('Incorrect chart options');
 			return;
 		}
 
 		const user = await Users.findOneById(userId, { projection: { _id: 1, utcOffset: 1 } });
 
 		if (!user) {
-			Livechat.logger.error('User not found');
 			return;
 		}
 
diff --git a/apps/meteor/app/livechat/server/methods/getAnalyticsOverviewData.ts b/apps/meteor/app/livechat/server/methods/getAnalyticsOverviewData.ts
index 81fe2658301..c9bb3d16345 100644
--- a/apps/meteor/app/livechat/server/methods/getAnalyticsOverviewData.ts
+++ b/apps/meteor/app/livechat/server/methods/getAnalyticsOverviewData.ts
@@ -7,7 +7,6 @@ import { Meteor } from 'meteor/meteor';
 import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
 import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
 import { settings } from '../../../settings/server';
-import { Livechat } from '../lib/Livechat';
 
 declare module '@rocket.chat/ui-contexts' {
 	// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -27,7 +26,6 @@ Meteor.methods<ServerMethods>({
 		}
 
 		if (!options.analyticsOptions?.name) {
-			Livechat.logger.error('Incorrect analytics options');
 			return;
 		}
 
diff --git a/apps/meteor/app/livechat/server/methods/removeAllClosedRooms.ts b/apps/meteor/app/livechat/server/methods/removeAllClosedRooms.ts
index 149cd0a5c5c..9be699ae05e 100644
--- a/apps/meteor/app/livechat/server/methods/removeAllClosedRooms.ts
+++ b/apps/meteor/app/livechat/server/methods/removeAllClosedRooms.ts
@@ -1,4 +1,5 @@
 import type { IOmnichannelRoom } from '@rocket.chat/core-typings';
+import { Logger } from '@rocket.chat/logger';
 import { LivechatRooms } from '@rocket.chat/models';
 import type { ServerMethods } from '@rocket.chat/ui-contexts';
 import { Meteor } from 'meteor/meteor';
@@ -16,6 +17,7 @@ declare module '@rocket.chat/ui-contexts' {
 
 Meteor.methods<ServerMethods>({
 	async 'livechat:removeAllClosedRooms'(departmentIds) {
+		const logger = new Logger('livechat:removeAllClosedRooms');
 		const user = Meteor.userId();
 
 		if (!user || !(await hasPermissionAsync(user, 'remove-closed-livechat-rooms'))) {
@@ -25,7 +27,7 @@ Meteor.methods<ServerMethods>({
 		}
 
 		// These are not debug logs since we want to know when the action is performed
-		Livechat.logger.info(`User ${Meteor.userId()} is removing all closed rooms`);
+		logger.info(`User ${Meteor.userId()} is removing all closed rooms`);
 
 		const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
 		const promises: Promise<void>[] = [];
@@ -34,7 +36,7 @@ Meteor.methods<ServerMethods>({
 		});
 		await Promise.all(promises);
 
-		Livechat.logger.info(`User ${Meteor.userId()} removed ${promises.length} closed rooms`);
+		logger.info(`User ${Meteor.userId()} removed ${promises.length} closed rooms`);
 		return promises.length;
 	},
 });
diff --git a/apps/meteor/app/livechat/server/startup.ts b/apps/meteor/app/livechat/server/startup.ts
index c8487f742b3..3ea87f3d568 100644
--- a/apps/meteor/app/livechat/server/startup.ts
+++ b/apps/meteor/app/livechat/server/startup.ts
@@ -1,5 +1,6 @@
 import type { IUser } from '@rocket.chat/core-typings';
 import { ILivechatAgentStatus, isOmnichannelRoom } from '@rocket.chat/core-typings';
+import { Logger } from '@rocket.chat/logger';
 import { LivechatRooms } from '@rocket.chat/models';
 import { Accounts } from 'meteor/accounts-base';
 import { Meteor } from 'meteor/meteor';
@@ -12,12 +13,13 @@ import { hasPermissionAsync } from '../../authorization/server/functions/hasPerm
 import { settings } from '../../settings/server';
 import { businessHourManager } from './business-hour';
 import { createDefaultBusinessHourIfNotExists } from './business-hour/Helper';
-import { Livechat } from './lib/Livechat';
 import { Livechat as LivechatTyped } from './lib/LivechatTyped';
 import { RoutingManager } from './lib/RoutingManager';
 import { LivechatAgentActivityMonitor } from './statistics/LivechatAgentActivityMonitor';
 import './roomAccessValidator.internalService';
 
+const logger = new Logger('LivechatStartup');
+
 Meteor.startup(async () => {
 	roomCoordinator.setRoomFind('l', (_id) => LivechatRooms.findOneById(_id));
 
@@ -63,7 +65,7 @@ Meteor.startup(async () => {
 	await createDefaultBusinessHourIfNotExists();
 
 	settings.watch<boolean>('Livechat_enable_business_hours', async (value) => {
-		Livechat.logger.info(`Changing business hour type to ${value}`);
+		logger.info(`Changing business hour type to ${value}`);
 		if (value) {
 			await businessHourManager.startManager();
 			return;
-- 
GitLab