Updated membercount checks (#10006)

* Updated membercount checks

* Added get member count method

* Updated tests to correctly add users
This commit is contained in:
Keith Holliday
2018-02-26 13:03:04 -07:00
committed by GitHub
parent 6118336a5d
commit 7dcd550209
5 changed files with 39 additions and 9 deletions

View File

@@ -216,6 +216,9 @@ describe('Amazon Payments - Subscribe', () => {
});
it('subscribes with amazon', async () => {
user.guilds.push(groupId);
await user.save();
await amzLib.subscribe({
billingAgreementId,
sub,
@@ -241,8 +244,13 @@ describe('Amazon Payments - Subscribe', () => {
user = new User();
user.guilds.push(groupId);
await user.save();
group.memberCount = 2;
await group.save();
// Add existing users
user = new User();
user.guilds.push(groupId);
await user.save();
// Set expected amount
sub.key = 'group_monthly';
sub.price = 9;
amount = 12;

View File

@@ -227,6 +227,11 @@ describe('checkout with subscription', () => {
sub = data.sub;
groupId = group._id;
email = 'test@test.com';
// Add user to group
user.guilds.push(groupId);
await user.save();
headers = {};
await stripePayments.checkout({
@@ -267,9 +272,15 @@ describe('checkout with subscription', () => {
groupId = group._id;
email = 'test@test.com';
headers = {};
// Add user to group
user.guilds.push(groupId);
await user.save();
user = new User();
user.guilds.push(groupId);
await user.save();
group.memberCount = 2;
await group.save();

View File

@@ -270,10 +270,10 @@ api.subscribe = async function subscribe (options) {
let priceOfSingleMember = 3;
if (groupId) {
let groupFields = basicGroupFields.concat(' purchased');
let group = await Group.getGroup({user, groupId, populateLeader: false, groupFields});
amount = sub.price + (group.memberCount - leaderCount) * priceOfSingleMember;
const groupFields = basicGroupFields.concat(' purchased');
const group = await Group.getGroup({user, groupId, populateLeader: false, groupFields});
const membersCount = await group.getMemberCount();
amount = sub.price + (membersCount - leaderCount) * priceOfSingleMember;
}
await this.setBillingAgreementDetails({

View File

@@ -97,9 +97,10 @@ api.checkout = async function checkout (options, stripeInc) {
if (groupId) {
customerObject.quantity = sub.quantity;
let groupFields = basicGroupFields.concat(' purchased');
let group = await Group.getGroup({user, groupId, populateLeader: false, groupFields});
customerObject.quantity = group.memberCount + sub.quantity - 1;
const groupFields = basicGroupFields.concat(' purchased');
const group = await Group.getGroup({user, groupId, populateLeader: false, groupFields});
const membersCount = await group.getMemberCount();
customerObject.quantity = membersCount + sub.quantity - 1;
}
response = await stripeApi.customers.create(customerObject);

View File

@@ -442,6 +442,16 @@ schema.methods.isMember = function isGroupMember (user) {
}
};
schema.methods.getMemberCount = async function getMemberCount () {
let query = { guilds: this._id };
if (this.type === 'party') {
query = { 'party._id': this._id };
}
return await User.count(query).exec();
};
export function chatDefaults (msg, user) {
let message = {
id: shared.uuid(),