Fix bug where mutliple user ids could not be saved

This commit is contained in:
Blade Barringer
2015-08-21 08:19:39 -05:00
parent 7fcfe6734b
commit 3153fbac3c
4 changed files with 169 additions and 28 deletions

View File

@@ -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.');
});
});
});
});

View File

@@ -4,38 +4,48 @@ habitrpg.controller('InviteToGroupCtrl', ['$scope', 'User', 'Groups', 'injectedG
$scope.group = injectedGroup; $scope.group = injectedGroup;
$scope.inviter = User.user.profile.name; $scope.inviter = User.user.profile.name;
$scope.emails = [{name:"",email:""},{name:"",email:""}]; _resetInvitees();
$scope.invitees = {uuid:""};
$scope.addUuid = function() {
$scope.invitees.push({uuid: ''});
};
$scope.addEmail = function() {
$scope.emails.push({name: '', email: ''});
};
$scope.inviteNewUsers = function(inviteMethod) { $scope.inviteNewUsers = function(inviteMethod) {
if (!$scope.group._id) {
group.create($scope.newGroup, function() {
_inviteByMethod(inviteMethod); _inviteByMethod(inviteMethod);
});
} else {
_inviteByMethod(inviteMethod);
}
}; };
function _inviteByMethod(inviteMethod) { function _inviteByMethod(inviteMethod) {
if (inviteMethod === 'email') { if (inviteMethod === 'email') {
Groups.Group.invite({gid: $scope.group._id}, {inviter: $scope.inviter, emails: $scope.emails}, function(){ Groups.Group.invite({gid: $scope.group._id}, {inviter: $scope.inviter, emails: $scope.emails}, function(){
Notification.text(window.env.t('invitationsSent')); Notification.text(window.env.t('invitationsSent'));
$scope.emails = [{name:'',email:''},{name:'',email:''}]; _resetInvitees();
}, function(){ }, function(){
$scope.emails = [{name:'',email:''},{name:'',email:''}]; _resetInvitees();
}); });
} }
else if (inviteMethod === 'uuid') { 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')); Notification.text(window.env.t('invitationsSent'));
$scope.invitees = {uuid:""}; _resetInvitees();
}, function(){ }, function(){
$scope.invitees = {uuid:""}; _resetInvitees();
}); });
} }
else { else {
return console.log('Invalid invite method.') return console.log('Invalid invite method.')
} }
} }
function _resetInvitees() {
var emptyEmails = [{name:"",email:""},{name:"",email:""}];
var emptyInvitees = [{uuid: ''}];
$scope.emails = emptyEmails;
$scope.invitees = emptyInvitees;
}
}]); }]);

View File

@@ -600,9 +600,6 @@ var inviteByUUIDs = function(uuids, group, req, res, next){
async.series([ async.series([
function(cb){ function(cb){
invite.save(cb); invite.save(cb);
},
function(cb){
group.save(cb);
} }
], function(err, results){ ], function(err, results){
if (err) return cb(err); if (err) return cb(err);
@@ -636,14 +633,20 @@ var inviteByUUIDs = function(uuids, group, req, res, next){
}, function(err){ }, function(err){
if(err) return err.code ? res.json(err.code, {err: err.err}) : next(err); if(err) return err.code ? res.json(err.code, {err: err.err}) : 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? // 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){ populateQuery(group.type, Group.findById(group._id)).exec(function(err, populatedGroup){
if(err) return next(err); if(err) return next(err);
res.json(populatedGroup); res.json(populatedGroup);
}); });
}
]);
}); });
}; };
var inviteByEmails = function(invites, group, req, res, next){ var inviteByEmails = function(invites, group, req, res, next){

View File

@@ -58,7 +58,7 @@ script(type='text/ng-template', id='modals/invite-party.html')
input.form-control(type='email', ng-model='email.email') input.form-control(type='email', ng-model='email.email')
tr tr
td(colspan=2) 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 i.glyphicon.glyphicon-plus
tr tr
td.form-group(colspan=2) td.form-group(colspan=2)
@@ -75,14 +75,16 @@ script(type='text/ng-template', id='modals/invite-party.html')
tr tr
th=env.t('userId') th=env.t('userId')
tbody tbody
tr(ng-repeat='user in invitees track by $index')
td
input.form-control(type='text', ng-model='user.uuid')
tr tr
td td
input.form-control(type='text', ng-model='invitees.uuid') a.btn.btn-xs.pull-right(ng-click='addUuid()')
i.glyphicon.glyphicon-plus
tr tr
td td
.col-sm-1 .col-sm-4.col-sm-offset-8
.col-sm-7
.col-sm-4
button.btn.btn-primary(type='submit')=env.t('sendInvitations') button.btn.btn-primary(type='submit')=env.t('sendInvitations')
.row .row
.quest_basilist.pull-left .quest_basilist.pull-left