From 64a4fa9c7b45a1c260d13da5d4c8d6d4506b1532 Mon Sep 17 00:00:00 2001
From: Marcos Spessatto Defendi <marcos.defendi@rocket.chat>
Date: Fri, 31 Jan 2025 11:52:12 -0300
Subject: [PATCH] refactor: remove send confirmation email method calls
 (server) (#35014)

---
 apps/meteor/app/api/server/v1/users.ts        |  3 +-
 .../app/lib/server/functions/setEmail.ts      |  3 +-
 .../server/methods/sendConfirmationEmail.ts   | 40 ++++++++++---------
 3 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/apps/meteor/app/api/server/v1/users.ts b/apps/meteor/app/api/server/v1/users.ts
index b5bd18ae980..020269e6ef7 100644
--- a/apps/meteor/app/api/server/v1/users.ts
+++ b/apps/meteor/app/api/server/v1/users.ts
@@ -29,6 +29,7 @@ import { i18n } from '../../../../server/lib/i18n';
 import { resetUserE2EEncriptionKey } from '../../../../server/lib/resetUserE2EKey';
 import { sendWelcomeEmail } from '../../../../server/lib/sendWelcomeEmail';
 import { saveUserPreferences } from '../../../../server/methods/saveUserPreferences';
+import { sendConfirmationEmail } from '../../../../server/methods/sendConfirmationEmail';
 import { getUserForCheck, emailCheck } from '../../../2fa/server/code';
 import { resetTOTP } from '../../../2fa/server/functions/resetTOTP';
 import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
@@ -953,7 +954,7 @@ API.v1.addRoute(
 		async post() {
 			const { email } = this.bodyParams;
 
-			if (await Meteor.callAsync('sendConfirmationEmail', email)) {
+			if (await sendConfirmationEmail(email)) {
 				return API.v1.success();
 			}
 			return API.v1.failure();
diff --git a/apps/meteor/app/lib/server/functions/setEmail.ts b/apps/meteor/app/lib/server/functions/setEmail.ts
index a2387efd1e6..6acac66cd3f 100644
--- a/apps/meteor/app/lib/server/functions/setEmail.ts
+++ b/apps/meteor/app/lib/server/functions/setEmail.ts
@@ -9,6 +9,7 @@ import * as Mailer from '../../../mailer/server/api';
 import { settings } from '../../../settings/server';
 import { RateLimiter, validateEmailDomain } from '../lib';
 import { checkEmailAvailability } from './checkEmailAvailability';
+import { sendConfirmationEmail } from '../../../../server/methods/sendConfirmationEmail';
 
 let html = '';
 Meteor.startup(() => {
@@ -93,7 +94,7 @@ const _setEmail = async function (
 		email,
 	};
 	if (shouldSendVerificationEmail === true) {
-		await Meteor.callAsync('sendConfirmationEmail', result.email);
+		await sendConfirmationEmail(result.email);
 	}
 	return result;
 };
diff --git a/apps/meteor/server/methods/sendConfirmationEmail.ts b/apps/meteor/server/methods/sendConfirmationEmail.ts
index c7b3098b169..f89d1332b0f 100644
--- a/apps/meteor/server/methods/sendConfirmationEmail.ts
+++ b/apps/meteor/server/methods/sendConfirmationEmail.ts
@@ -7,29 +7,33 @@ import { Meteor } from 'meteor/meteor';
 
 import { methodDeprecationLogger } from '../../app/lib/server/lib/deprecationWarningLogger';
 
-Meteor.methods<ServerMethods>({
-	async sendConfirmationEmail(to) {
-		check(to, String);
+export const sendConfirmationEmail = async (to: string): Promise<boolean> => {
+	check(to, String);
 
-		methodDeprecationLogger.method('sendConfirmationEmail', '7.0.0');
+	const email = to.trim();
 
-		const email = to.trim();
+	const user = await Users.findOneByEmailAddress(email, { projection: { _id: 1 } });
 
-		const user = await Users.findOneByEmailAddress(email, { projection: { _id: 1 } });
+	if (!user) {
+		return false;
+	}
 
-		if (!user) {
-			return false;
-		}
+	try {
+		Accounts.sendVerificationEmail(user._id, email);
+		return true;
+	} catch (error: any) {
+		throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${error.message}`, {
+			method: 'registerUser',
+			message: error.message,
+		});
+	}
+};
 
-		try {
-			Accounts.sendVerificationEmail(user._id, email);
-			return true;
-		} catch (error: any) {
-			throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${error.message}`, {
-				method: 'registerUser',
-				message: error.message,
-			});
-		}
+Meteor.methods<ServerMethods>({
+	async sendConfirmationEmail(to) {
+		methodDeprecationLogger.method('sendConfirmationEmail', '7.0.0');
+
+		return sendConfirmationEmail(to);
 	},
 });
 
-- 
GitLab