Restricted Members to only one Product

refs https://github.com/TryGhost/Team/issues/748

This ensures that you cannot add more than one product to a Member.

However it does allow a Member which already exists with more than one
Product to continue using the API. This is to account for edgecases such
as a Member going through the Stripe flow twice and ending up with
multiple subscriptions for multiple products
This commit is contained in:
Fabien O'Carroll 2021-06-16 11:33:21 +01:00 committed by Fabien 'egg' O'Carroll
parent 54c3340503
commit 83d25f71f4

View File

@ -80,6 +80,10 @@ module.exports = class MemberRepository {
const memberData = _.pick(data, ['email', 'name', 'note', 'subscribed', 'geolocation', 'created_at', 'products']);
if (memberData.products && memberData.products.length > 1) {
throw new errors.BadRequestError(tpl(messages.moreThanOneProduct));
}
const member = await this._Member.add({
...memberData,
labels
@ -128,6 +132,10 @@ module.exports = class MemberRepository {
const existingProductIds = existingProducts.map(product => product.id);
const incomingProductIds = data.products.map(product => product.id);
if (incomingProductIds.length > 1 && incomingProductIds.length > existingProductIds.length) {
throw new errors.BadRequestError(tpl(messages.moreThanOneProduct));
}
const productsToAdd = _.differenceWith(incomingProductIds, existingProductIds);
const productsToRemove = _.differenceWith(existingProductIds, incomingProductIds);
const productsToModify = productsToAdd.concat(productsToRemove);