Browse Source

refactor register

Yuki Takei 6 years ago
parent
commit
5f002a0500
2 changed files with 39 additions and 44 deletions
  1. 28 27
      src/server/routes/login.js
  2. 11 17
      src/server/util/mailer.js

+ 28 - 27
src/server/routes/login.js

@@ -132,33 +132,8 @@ module.exports = function(crowi, app) {
           }
 
           if (configManager.getConfig('crowi', 'security:registrationMode') !== aclService.labels.SECURITY_REGISTRATION_MODE_RESTRICTED) {
-            // send mails to all admin users (derived from crowi) -- 2020.06.18 Yuki Takei
-            const admins = await User.findAdmins();
-
-            const appTitle = appService.getAppTitle();
-            async.each(
-              admins,
-              (adminUser, next) => {
-                mailer.send({
-                  to: adminUser.email,
-                  subject: `[${appTitle}:admin] A New User Created and Waiting for Activation`,
-                  template: path.join(crowi.localeDir, 'en-US/admin/userWaitingActivation.txt'),
-                  vars: {
-                    createdUser: userData,
-                    adminUser,
-                    url: appService.getSiteUrl(),
-                    appTitle,
-                  },
-                },
-                (err, s) => {
-                  logger.info('completed to send email: ', err, s);
-                  next();
-                });
-              },
-              (err) => {
-                logger.info('Sending invitation email completed.', err);
-              },
-            );
+            // send mail asynchronous
+            sendEmailToAllAdmins(userData);
           }
 
           // add a flash message to inform the user that processing was successful -- 2017.09.23 Yuki Takei
@@ -177,6 +152,32 @@ module.exports = function(crowi, app) {
     }
   };
 
+  async function sendEmailToAllAdmins(userData) {
+    // send mails to all admin users (derived from crowi) -- 2020.06.18 Yuki Takei
+    const admins = await User.findAdmins();
+
+    const appTitle = appService.getAppTitle();
+
+    const promises = admins.map((admin) => {
+      return mailer.send({
+        to: admin.email,
+        subject: `[${appTitle}:admin] A New User Created and Waiting for Activation`,
+        template: path.join(crowi.localeDir, 'en-US/admin/userWaitingActivation.txt'),
+        vars: {
+          createdUser: userData,
+          admin,
+          url: appService.getSiteUrl(),
+          appTitle,
+        },
+      });
+    })
+
+    const results = await Promise.allSettled(promises);
+    results
+      .filter(result => result.status === 'rejected')
+      .forEach(result => logger.error(result.reason));
+  }
+
   actions.invited = async function(req, res) {
     if (!req.user) {
       return res.redirect('/login');

+ 11 - 17
src/server/util/mailer.js

@@ -93,25 +93,19 @@ module.exports = function(crowi) {
     return mc;
   }
 
-  function send(config, callback = () => {}) {
-    if (mailer) {
-      const templateVars = config.vars || {};
-      return swig.renderFile(
-        config.template,
-        templateVars,
-        (err, output) => {
-          if (err) {
-            throw err;
-          }
-
-          config.text = output;
-          return mailer.sendMail(setupMailConfig(config), callback);
-        },
-      );
+  async function send(config) {
+    if (!mailer) {
+      throw new Error('Mailer is not completed to set up. Please set up SMTP or AWS setting.');
     }
 
-    logger.debug('Mailer is not completed to set up. Please set up SMTP or AWS setting.');
-    return callback(new Error('Mailer is not completed to set up. Please set up SMTP or AWS setting.'), null);
+    const templateVars = config.vars || {};
+    const output = await swig.renderFile(
+      config.template,
+      templateVars,
+    );
+
+    config.text = output;
+    return mailer.sendMail(setupMailConfig(config));
   }