Skip to content

Commit

Permalink
feat: add endpoint to send welcome email
Browse files Browse the repository at this point in the history
  • Loading branch information
tapiarafael committed Nov 3, 2023
1 parent c6b4b54 commit 2cb1c1d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
20 changes: 20 additions & 0 deletions apps/meteor/app/api/server/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,26 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
'users.sendWelcomeEmail',
{
authRequired: true,
},
{
async post() {
const { email } = this.bodyParams;
if (!email) {
return API.v1.failure("The 'email' param is required");
}

if (await Meteor.callAsync('sendWelcomeEmail', email)) {
return API.v1.success();
}
return API.v1.failure();
},
},
);

API.v1.addRoute(
'users.register',
{
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import './methods/saveUserPreferences';
import './methods/saveUserProfile';
import './methods/sendConfirmationEmail';
import './methods/sendForgotPasswordEmail';
import './methods/sendWelcomeEmail';
import './methods/setAvatarFromService';
import './methods/setUserActiveStatus';
import './methods/setUserPassword';
Expand Down
49 changes: 49 additions & 0 deletions apps/meteor/server/methods/sendWelcomeEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Users } from '@rocket.chat/models';
import type { ServerMethods } from '@rocket.chat/ui-contexts';
import { check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

import * as Mailer from '../../app/mailer/server/api';
import { settings } from '../../app/settings/server';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
sendWelcomeEmail(to: string): boolean | undefined;
}
}

Meteor.methods<ServerMethods>({
async sendWelcomeEmail(to) {
check(to, String);

const email = to.trim();

const user = await Users.findOneByEmailAddress(email, { projection: { _id: 1 } });

if (!user) {
return false;
}

try {
let html = '';
Mailer.getTemplate('Accounts_UserAddedEmail_Email', (template) => {
html = template;
});

await Mailer.send({
to: email,
from: settings.get('From_Email'),
subject: settings.get('Accounts_UserAddedEmail_Subject'),
html,
});

return true;
} catch (error: any) {
throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${error.message}`, {
method: 'sendWelcomeEmail',
message: error.message,
});
}
},
});
4 changes: 4 additions & 0 deletions packages/rest-typings/src/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ export type UsersEndpoints = {
}>;
};

'/v1/users.sendWelcomeEmail': {
POST: (params: { email: string }) => void;
};

'/v1/users.setAvatar': {
POST: (params: UsersSetAvatar) => void;
};
Expand Down

0 comments on commit 2cb1c1d

Please sign in to comment.