From 3153fbac3cfbc0fc6fc3330b49fbeb6ad23fdefc Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Fri, 21 Aug 2015 08:19:39 -0500 Subject: [PATCH] Fix bug where mutliple user ids could not be saved --- .../spec/controllers/inviteToGroupCtrlSpec.js | 126 ++++++++++++++++++ .../js/controllers/inviteToGroupCtrl.js | 38 ++++-- website/src/controllers/groups.js | 21 +-- .../views/shared/modals/invite-friends.jade | 12 +- 4 files changed, 169 insertions(+), 28 deletions(-) create mode 100644 test/spec/controllers/inviteToGroupCtrlSpec.js diff --git a/test/spec/controllers/inviteToGroupCtrlSpec.js b/test/spec/controllers/inviteToGroupCtrlSpec.js new file mode 100644 index 0000000000..adff834143 --- /dev/null +++ b/test/spec/controllers/inviteToGroupCtrlSpec.js @@ -0,0 +1,126 @@ +'use strict'; + +describe('Invite to Group Controller', function() { + var scope, ctrl, groups, user, guild, $rootScope; + + beforeEach(function() { + user = specHelper.newUser({ + profile: { name: 'Mario' } + }); + + module(function($provide) { + $provide.value('User', {}); + $provide.value('injectedGroup', { user: user }); + }); + + inject(function($rootScope, $controller, Groups){ + scope = $rootScope.$new(); + + // Load RootCtrl to ensure shared behaviors are loaded + $controller('RootCtrl', {$scope: scope, User: {user: user}}); + + ctrl = $controller('InviteToGroupCtrl', {$scope: scope, User: {user: user}}); + + groups = Groups; + }); + }); + + describe('addEmail', function() { + it('adds blank email to email list', function() { + scope.emails = [{name: 'Mario', email: 'mario@mushroomkingdom.com'}]; + scope.addEmail(); + + expect(scope.emails).to.eql([{name: 'Mario', email: 'mario@mushroomkingdom.com'}, {name: '', email: ''}]); + }); + }); + + describe('addUuid', function() { + it('adds blank uuid to invitees list', function() { + scope.invitees = [{uuid: 'user1'}]; + scope.addUuid(); + + expect(scope.invitees).to.eql([{uuid: 'user1'}, {uuid: ''}]); + }); + }); + + describe('inviteNewUsers', function() { + beforeEach(function() { + scope.group = specHelper.newGroup(); + sandbox.stub(groups.Group, 'invite'); + }); + + context('email', function() { + it('invites user with emails', function() { + scope.emails[0].name = 'Luigi'; + scope.emails[0].email = 'mario_bro@themushroomkingdom.com'; + + scope.inviteNewUsers('email'); + expect(groups.Group.invite).to.be.calledOnce; + expect(groups.Group.invite).to.be.calledWith({ + gid: scope.group._id, + }, { + inviter: user.profile.name, + emails: [{name: 'Luigi', email: 'mario_bro@themushroomkingdom.com'},{name: '', email: ''}] + + }); + }); + + it('resets email list after sending', function() { + groups.Group.invite.yields(); + scope.emails[0].name = 'Luigi'; + scope.emails[0].email = 'mario_bro@themushroomkingdom.com'; + + scope.inviteNewUsers('email'); + + expect(scope.emails).to.eql([{name:'', email: ''},{name:'', email: ''}]); + }); + }); + + context('uuid', function() { + it('invites user with uuid', function() { + scope.invitees = [{uuid: '1234'}]; + + scope.inviteNewUsers('uuid'); + expect(groups.Group.invite).to.be.calledOnce; + expect(groups.Group.invite).to.be.calledWith({ + gid: scope.group._id, + }, { + uuids: ['1234'] + }); + }); + + it('invites users with uuids', function() { + scope.invitees = [{uuid: 'user1'}, {uuid: 'user2'}, {uuid: 'user3'}]; + + scope.inviteNewUsers('uuid'); + expect(groups.Group.invite).to.be.calledOnce; + expect(groups.Group.invite).to.be.calledWith({ + gid: scope.group._id, + }, { + uuids: ['user1', 'user2', 'user3'] + }); + }); + + it('resets invitee list after sending', function() { + groups.Group.invite.yields(); + scope.invitees = [{uuid: 'user1'}, {uuid: 'user2'}, {uuid: 'user3'}]; + + scope.inviteNewUsers('uuid'); + + expect(scope.invitees).to.eql([{uuid: ''}]); + }); + }); + + context('invalid invite method', function() { + it('logs error', function() { + sandbox.stub(console, 'log'); + + scope.inviteNewUsers(); + expect(groups.Group.invite).to.not.be.called; + expect(console.log).to.be.calledOnce; + expect(console.log).to.be.calledWith('Invalid invite method.'); + }); + }); + + }); +}); diff --git a/website/public/js/controllers/inviteToGroupCtrl.js b/website/public/js/controllers/inviteToGroupCtrl.js index 2dffe449fa..0a5b5bd662 100644 --- a/website/public/js/controllers/inviteToGroupCtrl.js +++ b/website/public/js/controllers/inviteToGroupCtrl.js @@ -4,38 +4,48 @@ habitrpg.controller('InviteToGroupCtrl', ['$scope', 'User', 'Groups', 'injectedG $scope.group = injectedGroup; $scope.inviter = User.user.profile.name; - $scope.emails = [{name:"",email:""},{name:"",email:""}]; - $scope.invitees = {uuid:""}; + _resetInvitees(); + + $scope.addUuid = function() { + $scope.invitees.push({uuid: ''}); + }; + + $scope.addEmail = function() { + $scope.emails.push({name: '', email: ''}); + }; $scope.inviteNewUsers = function(inviteMethod) { - if (!$scope.group._id) { - group.create($scope.newGroup, function() { - _inviteByMethod(inviteMethod); - }); - } else { - _inviteByMethod(inviteMethod); - } + _inviteByMethod(inviteMethod); }; function _inviteByMethod(inviteMethod) { if (inviteMethod === 'email') { Groups.Group.invite({gid: $scope.group._id}, {inviter: $scope.inviter, emails: $scope.emails}, function(){ Notification.text(window.env.t('invitationsSent')); - $scope.emails = [{name:'',email:''},{name:'',email:''}]; + _resetInvitees(); }, function(){ - $scope.emails = [{name:'',email:''},{name:'',email:''}]; + _resetInvitees(); }); } else if (inviteMethod === 'uuid') { - Groups.Group.invite({gid: $scope.group._id}, {uuids: [$scope.invitees.uuid]}, function(){ + var uuids = _.pluck($scope.invitees, 'uuid'); + Groups.Group.invite({gid: $scope.group._id}, {uuids: uuids}, function(){ Notification.text(window.env.t('invitationsSent')); - $scope.invitees = {uuid:""}; + _resetInvitees(); }, function(){ - $scope.invitees = {uuid:""}; + _resetInvitees(); }); } else { return console.log('Invalid invite method.') } } + + function _resetInvitees() { + var emptyEmails = [{name:"",email:""},{name:"",email:""}]; + var emptyInvitees = [{uuid: ''}]; + + $scope.emails = emptyEmails; + $scope.invitees = emptyInvitees; + } }]); diff --git a/website/src/controllers/groups.js b/website/src/controllers/groups.js index c525101eb8..19e0409ed1 100644 --- a/website/src/controllers/groups.js +++ b/website/src/controllers/groups.js @@ -600,9 +600,6 @@ var inviteByUUIDs = function(uuids, group, req, res, next){ async.series([ function(cb){ invite.save(cb); - }, - function(cb){ - group.save(cb); } ], function(err, results){ if (err) return cb(err); @@ -636,14 +633,20 @@ var inviteByUUIDs = function(uuids, group, req, res, next){ }, function(err){ if(err) return err.code ? res.json(err.code, {err: err.err}) : next(err); - // TODO pass group from save above don't find it again, or you have to find it again in order to run populate? - populateQuery(group.type, Group.findById(group._id)).exec(function(err, populatedGroup){ - if(err) return next(err); + async.series([ + function(cb) { + group.save(cb); + }, + function(cb) { + // TODO pass group from save above don't find it again, or you have to find it again in order to run populate? + populateQuery(group.type, Group.findById(group._id)).exec(function(err, populatedGroup){ + if(err) return next(err); - res.json(populatedGroup); - }); + res.json(populatedGroup); + }); + } + ]); }); - }; var inviteByEmails = function(invites, group, req, res, next){ diff --git a/website/views/shared/modals/invite-friends.jade b/website/views/shared/modals/invite-friends.jade index ed7cdc3237..b34a579c90 100644 --- a/website/views/shared/modals/invite-friends.jade +++ b/website/views/shared/modals/invite-friends.jade @@ -58,7 +58,7 @@ script(type='text/ng-template', id='modals/invite-party.html') input.form-control(type='email', ng-model='email.email') tr td(colspan=2) - a.btn.btn-xs.pull-right(ng-click='emails = emails.concat([{name:"",email:""}])') + a.btn.btn-xs.pull-right(ng-click='addEmail()') i.glyphicon.glyphicon-plus tr td.form-group(colspan=2) @@ -75,14 +75,16 @@ script(type='text/ng-template', id='modals/invite-party.html') tr th=env.t('userId') tbody + tr(ng-repeat='user in invitees track by $index') + td + input.form-control(type='text', ng-model='user.uuid') tr td - input.form-control(type='text', ng-model='invitees.uuid') + a.btn.btn-xs.pull-right(ng-click='addUuid()') + i.glyphicon.glyphicon-plus tr td - .col-sm-1 - .col-sm-7 - .col-sm-4 + .col-sm-4.col-sm-offset-8 button.btn.btn-primary(type='submit')=env.t('sendInvitations') .row .quest_basilist.pull-left