Added email invite limit

This commit is contained in:
TheHollidayInn
2017-04-11 16:36:45 -06:00
committed by Sabe Jones
parent ad0a51167d
commit 8a2e6a98c2
4 changed files with 37 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import { v4 as generateUUID } from 'uuid';
const INVITES_LIMIT = 100;
const PARTY_LIMIT_MEMBERS = 30;
const MAX_EMAIL_INVITES_BY_USER = 200;
describe('Post /groups/:groupId/invite', () => {
let inviter;
@@ -205,13 +206,37 @@ describe('Post /groups/:groupId/invite', () => {
});
});
it('returns an error when a user has sent the max number of email invites', async () => {
let inviterWithMax = await generateUser({
invitesSent: MAX_EMAIL_INVITES_BY_USER,
balance: 4,
});
let tmpGroup = await inviterWithMax.post('/groups', {
name: groupName,
type: 'guild',
});
await expect(inviterWithMax.post(`/groups/${tmpGroup._id}/invite`, {
emails: [testInvite],
inviter: 'inviter name',
}))
.to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('inviteLimitReached'),
});
});
it('invites a user to a group by email', async () => {
let res = await inviter.post(`/groups/${group._id}/invite`, {
emails: [testInvite],
inviter: 'inviter name',
});
let updatedUser = await inviter.sync();
expect(res).to.exist;
expect(updatedUser.invitesSent).to.eql(1);
});
it('invites multiple users to a group by email', async () => {
@@ -219,7 +244,10 @@ describe('Post /groups/:groupId/invite', () => {
emails: [testInvite, {name: 'test2', email: 'test2@habitica.com'}],
});
let updatedUser = await inviter.sync();
expect(res).to.exist;
expect(updatedUser.invitesSent).to.eql(2);
});
});

View File

@@ -270,5 +270,6 @@
"confirmCancelGroupPlan": "Are you sure you want to cancel the group plan and remove its benefits from all members, including their free subscriptions?",
"canceledGroupPlan": "Canceled Group Plan",
"groupPlanCanceled": "Group Plan will become inactive on",
"purchasedGroupPlanPlanExtraMonths": "You have <%= months %> months of extra group plan credit."
"purchasedGroupPlanPlanExtraMonths": "You have <%= months %> months of extra group plan credit.",
"inviteLimitReached": "You have already sent the max number of email invites."
}

View File

@@ -27,6 +27,8 @@ import amzLib from '../../libs/amazonPayments';
import shared from '../../../common';
import apiMessages from '../../libs/apiMessages';
const MAX_EMAIL_INVITES_BY_USER = 200;
/**
* @apiDefine GroupBodyInvalid
* @apiError (400) {BadRequest} GroupBodyInvalid A parameter in the group body was invalid.
@@ -1056,6 +1058,8 @@ api.inviteToGroup = {
req.checkParams('groupId', res.t('groupIdRequired')).notEmpty();
if (user.invitesSent >= MAX_EMAIL_INVITES_BY_USER) throw new NotAuthorized(res.t('inviteLimitReached'));
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
@@ -1079,6 +1083,8 @@ api.inviteToGroup = {
if (emails) {
let emailInvites = emails.map((invite) => _inviteByEmail(invite, group, user, req, res));
user.invitesSent += emails.length;
await user.save();
let emailResults = await Bluebird.all(emailInvites);
results.push(...emailResults);
}

View File

@@ -550,6 +550,7 @@ let schema = new Schema({
}},
webhooks: [WebhookSchema],
loginIncentives: {type: Number, default: 0},
invitesSent: {type: Number, default: 0},
}, {
strict: true,
minimize: false, // So empty objects are returned