Skip to content

Commit

Permalink
fix(groups): roll back unneeded Stripe lib change, refactor migration
Browse files Browse the repository at this point in the history
  • Loading branch information
SabreCat committed Jun 15, 2018
1 parent d15eaee commit 63c08d7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
66 changes: 40 additions & 26 deletions migrations/groups/reconcile-group-plan-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const CONNECTION_STRING = nconf.get('MIGRATION_CONNECT_STRING');
let dbGroups = monk(CONNECTION_STRING).get('groups', { castIds: false });
let dbUsers = monk(CONNECTION_STRING).get('users', { castIds: false });

function fixGroupPlanMembers () {
console.info('Group ID,Customer ID,Plan ID,Quantity,Recorded Member Count,Actual Member Count');
async function fixGroupPlanMembers () {
console.info('Group ID, Customer ID, Plan ID, Quantity, Recorded Member Count, Actual Member Count');
let groupPlanCount = 0;
let fixedGroupCount = 0;

dbGroups.find(
{
$and:
Expand All @@ -43,6 +44,7 @@ function fixGroupPlanMembers () {
).each(async (group, {close, pause, resume}) => { // eslint-disable-line no-unused-vars
pause();
groupPlanCount++;

const canonicalMemberCount = await dbUsers.count(
{
$or:
Expand All @@ -58,35 +60,47 @@ function fixGroupPlanMembers () {
const quantityMismatch = group.purchased.plan.quantity !== group.memberCount + 2;
const incorrectQuantity = isMonthlyPlan && quantityMismatch;

if (incorrectMemberCount || incorrectQuantity) {
console.info(`${group._id},${group.purchased.plan.customerId},${group.purchased.plan.planId},${group.purchased.plan.quantity},${group.memberCount},${canonicalMemberCount}`);
return dbGroups.update(
{_id: group._id},
{$set: {memberCount: canonicalMemberCount}}
).then(async () => {
fixedGroupCount++;
if (group.purchased.plan.paymentMethod === 'Stripe') {
await stripePayments.chargeForAdditionalGroupMember(group);
} else if (incorrectQuantity) {
return dbGroups.update(
{_id: group._id},
{$set: {'purchased.plan.quantity': canonicalMemberCount + 2}}
).then(() => {
resume();
});
} else {
resume();
}
}).catch((err) => {
console.log(err);
return process.exit(1);
});
} else {
if (!incorrectMemberCount && !incorrectQuantity) {
resume();
return;
}

console.info(`${group._id}, ${group.purchased.plan.customerId}, ${group.purchased.plan.planId}, ${group.purchased.plan.quantity}, ${group.memberCount}, ${canonicalMemberCount}`);

const groupUpdate = await dbGroups.update(
{ _id: group._id },
{
$set: {
memberCount: canonicalMemberCount,
},
}
);

if (!groupUpdate) return;

fixedGroupCount++;
if (group.purchased.plan.paymentMethod === 'Stripe') {
await stripePayments.chargeForAdditionalGroupMember(group);
await dbGroups.update(
{_id: group._id},
{$set: {'purchased.plan.quantity': canonicalMemberCount + 2}}
);
}

if (incorrectQuantity) {
await dbGroups.update(
{_id: group._id},
{$set: {'purchased.plan.quantity': canonicalMemberCount + 2}}
);
}

resume();
}).then(() => {
console.info(`Fixed ${fixedGroupCount} out of ${groupPlanCount} active Group Plans`);
return process.exit(0);
}).catch((err) => {
console.log(err);
return process.exit(1);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ describe('Stripe - Upgrade Group Plan', () => {
await stripePayments.chargeForAdditionalGroupMember(updatedGroup);

expect(spy.calledOnce).to.be.true;
updatedGroup = await Group.findById(group._id).exec();
expect(updatedGroup.purchased.plan.quantity).to.eql(4);
});
});
2 changes: 1 addition & 1 deletion website/server/libs/payments/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ api.chargeForAdditionalGroupMember = async function chargeForAdditionalGroupMemb
}
);

await Group.update({_id: group._id}, {$set:{'purchased.plan.quantity': group.memberCount + plan.quantity - 1}}).exec();
group.purchased.plan.quantity = group.memberCount + plan.quantity - 1;
};

/**
Expand Down

0 comments on commit 63c08d7

Please sign in to comment.