Exemplo n.º 1
0
Voted.prototype.deleteAllUserVotesForActivity = function(userId, activityId)
{
    var _this = this;
    if(!db.activity.exists(activityId))
    {
        throw new error.NotFoundError('Activity not found')
    }

    db.suggested.outEdges(activityId).forEach(function(edge) {
        _this.deleteUserVote(userId, edge._to);
    });
};
Exemplo n.º 2
0
Voted.prototype.saveUserVote = function(userId, suggestionId)
{
    if(!db.user.exists(userId))
    {
        throw new error.NotFoundError('User')
    }

    if(!db.suggestion.exists(suggestionId))
    {
        throw new error.NotFoundError('Suggestion');
    }

    // Requires user to be joined before voting
    var suggestedExample = {};
    suggestedExample._to = suggestionId;
    var suggested = db.suggested.firstExample(suggestedExample);
    if(suggested)
    {
        var joinedExample = {};
        joinedExample._from = userId;
        joinedExample._to = suggested._from;
        var joined = db.joined.firstExample(joinedExample);
        if(!joined)
        {
            throw new error.UnauthorizedError('Voting for event user not joined to');
        }
    }
    else
    {
        throw new error.GenericError('Suggestion id does not have corresponding suggested edge');
    }


    var example = {};
    example[this.FROM_FIELD] = userId;
    example[this.TO_FIELD] = suggestionId;
    var result = db.voted.firstExample(example);
    if(result == null)
    {
        result = db.voted.save(userId, suggestionId, {});
        if(result.error == true)
        {
            throw new error.GenericError('Saving user vote for suggestion failed.');
        }
    }
    return result;
};