Vulcan/server/invites.js

39 lines
1.3 KiB
JavaScript
Raw Normal View History

2013-10-23 19:43:42 +09:00
Meteor.methods({
inviteUser: function (userId) {
2013-12-01 11:25:17 +09:00
var currentUser = Meteor.user(),
invitedUser = Meteor.users.findOne(userId),
invite = {
invitedId: userId,
invitedName: getDisplayName(invitedUser),
time: new Date()
},
currentUserCanInvite = (currentUser.inviteCount > 0 && canInvite(currentUser)),
currentUserIsAdmin = isAdmin(currentUser);
2013-10-23 19:43:42 +09:00
// if the current user is logged in, still has available invites and is himself invited (or an admin), and the target user is not invited
2013-12-01 11:25:17 +09:00
if(currentUser && !isInvited(invitedUser) && (currentUserIsAdmin || currentUserCanInvite)){
2013-10-23 19:43:42 +09:00
// update invinting user
Meteor.users.update(Meteor.userId(), {$inc:{inviteCount: -1}, $inc:{invitedCount: 1}, $push:{invites: invite}});
2013-10-23 19:43:42 +09:00
// update invited user
2013-12-01 11:25:17 +09:00
var a=Meteor.users.update(userId, {$set: {
2013-10-23 19:43:42 +09:00
isInvited: true,
invitedBy: currentUser._id,
invitedByName: getDisplayName(currentUser)
}});
console.log(a);
2013-10-23 19:43:42 +09:00
createNotification({
2013-10-23 20:20:10 +09:00
event: 'accountApproved',
properties: {},
2013-10-23 20:27:19 +09:00
userToNotify: invitedUser,
userDoingAction: currentUser,
2013-10-23 20:20:10 +09:00
sendEmail: true
});
2013-10-23 19:43:42 +09:00
}else{
throw new Meteor.Error(701, "You can't invite this user, sorry.");
}
}
2013-12-01 11:25:17 +09:00
});