Permalink
Browse files

Step 15.1: Add a party invitation method

  • Loading branch information...
1 parent c701352 commit 00735f1c8d6c784973db04c0a2ad5dcf655a4470 @dotansimha dotansimha committed with DAB0mB Sep 27, 2016
Showing with 47 additions and 0 deletions.
  1. +47 −0 both/methods/parties.methods.ts
@@ -0,0 +1,47 @@
+import {Parties} from '../collections/parties.collection';
+import {Email} from 'meteor/email';
+import {check} from 'meteor/check';
+import {Meteor} from 'meteor/meteor';
+
+function getContactEmail(user:Meteor.User):string {
+ if (user.emails && user.emails.length)
+ return user.emails[0].address;
+
+ return null;
+}
+
+Meteor.methods({
+ invite: function (partyId:string, userId:string) {
+ check(partyId, String);
+ check(userId, String);
+
+ let party = Parties.collection.findOne(partyId);
+
+ if (!party)
+ throw new Meteor.Error('404', 'No such party!');
+
+ if (party.public)
+ throw new Meteor.Error('400', 'That party is public. No need to invite people.');
+
+ if (party.owner !== this.userId)
+ throw new Meteor.Error('403', 'No permissions!');
+
+ if (userId !== party.owner && (party.invited || []).indexOf(userId) == -1) {
+ Parties.collection.update(partyId, {$addToSet: {invited: userId}});
+
+ let from = getContactEmail(Meteor.users.findOne(this.userId));
+ let to = getContactEmail(Meteor.users.findOne(userId));
+
+ if (Meteor.isServer && to) {
+ Email.send({
+ from: 'noreply@socially.com',
+ to: to,
+ replyTo: from || undefined,
+ subject: 'PARTY: ' + party.name,
+ text: `Hi, I just invited you to ${party.name} on Socially.
+ \n\nCome check it out: ${Meteor.absoluteUrl()}\n`
+ });
+ }
+ }
+ }
+});

0 comments on commit 00735f1

Please sign in to comment.