Requests = new Mongo.Collection('requests');

Meteor.methods({
  addRequest: function(requestAttributes) {
    check(Meteor.userId(), String);
    check(requestAttributes, {
        user: Object,
        project: Object
    });

    // TODO add more checks

    var request = _.extend(requestAttributes, {
      createdAt: new Date(),
      status: 'pending'
    });

    Requests.insert(request);
    createRequestNotification(request);
  }
});

hasRequestPending = function(userId, projectId) {
  return Requests.find({
    'user.userId': userId,
    'project.projectId': projectId, status: 'pending' }).count() > 0;
};

Requests = new Mongo.Collection('requests');

Meteor.methods({
  addRequest: function(requestAttributes) {
    check(Meteor.userId(), String);
    check(requestAttributes, {
        user: Object,
        project: Object
    });

    if (hasRequestPending(requestAttributes.user.userId, requestAttributes.project.projectId)) {
      throw new Meteor.Error('User has already requested to join');
    }

    // TODO add more checks

    var request = _.extend(requestAttributes, {
      createdAt: new Date(),
      status: 'pending'
    });

    Requests.insert(request);
    createRequestNotification(request);
  }
});

hasRequestPending = function(userId, projectId) {
  return Requests.find({
    'user.userId': userId,
    'project.projectId': projectId,
    status: 'pending' }).count() > 0;
};

Throw an error if a user tries to join a project twice
Throw an error if a user tries to join a project twice
