Browse Source

patch re-invit

guillaume 7 years ago
parent
commit
df54f15ecb
3 changed files with 27 additions and 8 deletions
  1. 3 0
      client/components/settings/settingBody.js
  2. 21 8
      models/settings.js
  3. 3 0
      models/users.js

+ 3 - 0
client/components/settings/settingBody.js

@@ -82,6 +82,7 @@ BlazeComponent.extendComponent({
   },
   },
 
 
   inviteThroughEmail() {
   inviteThroughEmail() {
+    /* eslint-disable no-console */
     const emails = $('#email-to-invite').val().trim().split('\n').join(',').split(',');
     const emails = $('#email-to-invite').val().trim().split('\n').join(',').split(',');
     const boardsToInvite = [];
     const boardsToInvite = [];
     $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function () {
     $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function () {
@@ -99,9 +100,11 @@ BlazeComponent.extendComponent({
         // if (!err) {
         // if (!err) {
         //   TODO - show more info to user
         //   TODO - show more info to user
         // }
         // }
+
         this.setLoading(false);
         this.setLoading(false);
       });
       });
     }
     }
+    /* eslint-enable no-console */
   },
   },
 
 
   saveMailServerInfo() {
   saveMailServerInfo() {

+ 21 - 8
models/settings.js

@@ -124,20 +124,33 @@ if (Meteor.isServer) {
     sendInvitation(emails, boards) {
     sendInvitation(emails, boards) {
       check(emails, [String]);
       check(emails, [String]);
       check(boards, [String]);
       check(boards, [String]);
+
       const user = Users.findOne(Meteor.userId());
       const user = Users.findOne(Meteor.userId());
       if(!user.isAdmin){
       if(!user.isAdmin){
         throw new Meteor.Error('not-allowed');
         throw new Meteor.Error('not-allowed');
       }
       }
       emails.forEach((email) => {
       emails.forEach((email) => {
         if (email && SimpleSchema.RegEx.Email.test(email)) {
         if (email && SimpleSchema.RegEx.Email.test(email)) {
-          const code = getRandomNum(100000, 999999);
-          InvitationCodes.insert({code, email, boardsToBeInvited: boards, createdAt: new Date(), authorId: Meteor.userId()}, function(err, _id){
-            if (!err && _id) {
-              sendInvitationEmail(_id);
-            } else {
-              throw new Meteor.Error('invitation-generated-fail', err.message);
-            }
-          });
+          // Checks if the email is already link to an account.
+          const userExist = Users.findOne({email});
+          if (userExist){
+            throw new Meteor.Error('user-exist', `The user with the email ${email} has already an account.`);
+          }
+          // Checks if the email is already link to an invitation.
+          const invitation = InvitationCodes.findOne({email});
+          if (invitation){
+            InvitationCodes.update(invitation, {$set : {boardsToBeInvited: boards}});
+            sendInvitationEmail(invitation._id);
+          }else {
+            const code = getRandomNum(100000, 999999);
+            InvitationCodes.insert({code, email, boardsToBeInvited: boards, createdAt: new Date(), authorId: Meteor.userId()}, function(err, _id){
+              if (!err && _id) {
+                sendInvitationEmail(_id);
+              } else {
+                throw new Meteor.Error('invitation-generated-fail', err.message);
+              }
+            });
+          }
         }
         }
       });
       });
     },
     },

+ 3 - 0
models/users.js

@@ -503,6 +503,9 @@ if (Meteor.isServer) {
       user.profile.boardView = 'board-view-lists';
       user.profile.boardView = 'board-view-lists';
     }
     }
 
 
+    // Deletes the invitation.
+    InvitationCodes.remove(invitationCode._id);
+
     return user;
     return user;
   });
   });
 }
 }