Skip to content

Commit

Permalink
Step 15.10: Add a reply method
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha authored and DAB0mB committed Dec 14, 2016
1 parent c3d46e3 commit db5b4d2
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions both/methods/parties.methods.ts
Expand Up @@ -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.