mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Supported comped status for create/update methods
refs https://github.com/TryGhost/Team/issues/790 Both creating and updating members only ever need to explicitly set either the 'comped' or 'free' status as these methods do not deal with Stripe. When updating a member if the products are not changing, we do not attempt to change the status either.
This commit is contained in:
parent
7f71e28392
commit
8d8d886705
1 changed files with 46 additions and 10 deletions
|
@ -84,8 +84,17 @@ module.exports = class MemberRepository {
|
||||||
throw new errors.BadRequestError(tpl(messages.moreThanOneProduct));
|
throw new errors.BadRequestError(tpl(messages.moreThanOneProduct));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const memberStatusData = {
|
||||||
|
status: 'free'
|
||||||
|
};
|
||||||
|
|
||||||
|
if (memberData.products.length === 1) {
|
||||||
|
memberStatusData.status = 'comped';
|
||||||
|
}
|
||||||
|
|
||||||
const member = await this._Member.add({
|
const member = await this._Member.add({
|
||||||
...memberData,
|
...memberData,
|
||||||
|
...memberStatusData,
|
||||||
labels
|
labels
|
||||||
}, options);
|
}, options);
|
||||||
|
|
||||||
|
@ -122,6 +131,18 @@ module.exports = class MemberRepository {
|
||||||
transacting: options.transacting
|
transacting: options.transacting
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const memberData = _.pick(data, [
|
||||||
|
'email',
|
||||||
|
'name',
|
||||||
|
'note',
|
||||||
|
'subscribed',
|
||||||
|
'labels',
|
||||||
|
'geolocation',
|
||||||
|
'products'
|
||||||
|
]);
|
||||||
|
|
||||||
|
const memberStatusData = {};
|
||||||
|
|
||||||
if (this._stripeAPIService.configured && data.products) {
|
if (this._stripeAPIService.configured && data.products) {
|
||||||
const member = await this._Member.findOne({
|
const member = await this._Member.findOne({
|
||||||
id: options.id
|
id: options.id
|
||||||
|
@ -140,7 +161,7 @@ module.exports = class MemberRepository {
|
||||||
const productsToRemove = _.differenceWith(existingProductIds, incomingProductIds);
|
const productsToRemove = _.differenceWith(existingProductIds, incomingProductIds);
|
||||||
const productsToModify = productsToAdd.concat(productsToRemove);
|
const productsToModify = productsToAdd.concat(productsToRemove);
|
||||||
|
|
||||||
if (productsToModify.length > 0) {
|
if (productsToModify.length !== 0) {
|
||||||
const exisitingSubscriptions = await member.related('stripeSubscriptions').fetch(sharedOptions);
|
const exisitingSubscriptions = await member.related('stripeSubscriptions').fetch(sharedOptions);
|
||||||
const existingActiveSubscriptions = exisitingSubscriptions.filter((subscription) => {
|
const existingActiveSubscriptions = exisitingSubscriptions.filter((subscription) => {
|
||||||
return this.isActiveSubscriptionStatus(subscription.get('status'));
|
return this.isActiveSubscriptionStatus(subscription.get('status'));
|
||||||
|
@ -150,17 +171,24 @@ module.exports = class MemberRepository {
|
||||||
throw new errors.BadRequestError(tpl(messages.existingSubscriptions));
|
throw new errors.BadRequestError(tpl(messages.existingSubscriptions));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CASE: We are removing all products from a member & there were no active subscriptions - the member is "free"
|
||||||
|
if (incomingProductIds.length === 0) {
|
||||||
|
memberStatusData.status = 'free';
|
||||||
|
} else {
|
||||||
|
// CASE: We are changing products & there were not active stripe subscriptions - the member is "comped"
|
||||||
|
if (productsToModify.length !== 0) {
|
||||||
|
memberStatusData.status = 'comped';
|
||||||
|
} else {
|
||||||
|
// CASE: We are not changing any products - leave the status alone
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const member = await this._Member.edit(_.pick(data, [
|
const member = await this._Member.edit({
|
||||||
'email',
|
...memberData,
|
||||||
'name',
|
...memberStatusData
|
||||||
'note',
|
}, options);
|
||||||
'subscribed',
|
|
||||||
'labels',
|
|
||||||
'geolocation',
|
|
||||||
'products'
|
|
||||||
]), options);
|
|
||||||
|
|
||||||
// member._changed.subscribed has a value if the `subscribed` attribute is passed in the update call, regardless of the previous value
|
// member._changed.subscribed has a value if the `subscribed` attribute is passed in the update call, regardless of the previous value
|
||||||
if (member.attributes.subscribed !== member._previousAttributes.subscribed) {
|
if (member.attributes.subscribed !== member._previousAttributes.subscribed) {
|
||||||
|
@ -188,6 +216,14 @@ module.exports = class MemberRepository {
|
||||||
}, sharedOptions);
|
}, sharedOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (member.attributes.status !== member._previousAttributes.status) {
|
||||||
|
await this._MemberStatusEvent.add({
|
||||||
|
member_id: member.id,
|
||||||
|
from_status: member._previousAttributes.status,
|
||||||
|
to_status: member.get('status')
|
||||||
|
}, sharedOptions);
|
||||||
|
}
|
||||||
|
|
||||||
if (this._stripeAPIService.configured && member._changed.email) {
|
if (this._stripeAPIService.configured && member._changed.email) {
|
||||||
await member.related('stripeCustomers').fetch();
|
await member.related('stripeCustomers').fetch();
|
||||||
const customers = member.related('stripeCustomers');
|
const customers = member.related('stripeCustomers');
|
||||||
|
|
Loading…
Add table
Reference in a new issue