Permalink
Browse files

Step 15.10: Add a reply method

  • Loading branch information...
1 parent c3d46e3 commit db5b4d282d26d87c6d9acf0735d088f9ede14254 @dotansimha dotansimha committed with DAB0mB Sep 27, 2016
Showing with 45 additions and 0 deletions.
  1. +45 −0 both/methods/parties.methods.ts
@@ -43,5 +43,50 @@ Meteor.methods({
});
}
}
+ },
+ reply: function(partyId: string, rsvp: string) {
+ check(partyId, String);
+ check(rsvp, String);
+
+ if (!this.userId)
+ throw new Meteor.Error('403', 'You must be logged-in to reply');
+
+ if (['yes', 'no', 'maybe'].indexOf(rsvp) === -1)
+ throw new Meteor.Error('400', 'Invalid RSVP');
+
+ let party = Parties.findOne({ _id: partyId });
+
+ if (!party)
+ throw new Meteor.Error('404', 'No such party');
+
+ if (party.owner === this.userId)
+ throw new Meteor.Error('500', 'You are the owner!');
+
+ if (!party.public && (!party.invited || party.invited.indexOf(this.userId) == -1))
+ throw new Meteor.Error('403', 'No such party'); // its private, but let's not tell this to the user
+
+ let rsvpIndex = party.rsvps ? party.rsvps.findIndex((rsvp) => rsvp.userId === this.userId) : -1;
+
+ if (rsvpIndex !== -1) {
+ // update existing rsvp entry
+ if (Meteor.isServer) {
+ // update the appropriate rsvp entry with $
+ Parties.update(
+ { _id: partyId, 'rsvps.userId': this.userId },
+ { $set: { 'rsvps.$.response': rsvp } });
+ } else {
+ // minimongo doesn't yet support $ in modifier. as a temporary
+ // workaround, make a modifier that uses an index. this is
+ // safe on the client since there's only one thread.
+ let modifier = { $set: {} };
+ modifier.$set['rsvps.' + rsvpIndex + '.response'] = rsvp;
+
+ Parties.update(partyId, modifier);
+ }
+ } else {
+ // add new rsvp entry
+ Parties.update(partyId,
+ { $push: { rsvps: { userId: this.userId, response: rsvp } } });
+ }
}
});

0 comments on commit db5b4d2

Please sign in to comment.