mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Added mapping from member subscribed to newsletters on edit/create
refs https://github.com/TryGhost/Team/issues/1545 - When editing or creating a member with the subscribed property, it is mapped to the corresponding newletters value - Defaults to all active newsletters with visibility = members and subscribe_on_signup = true - Tests in https://github.com/TryGhost/Ghost/pull/14583
This commit is contained in:
parent
9fcf65f3f7
commit
a82d0f499d
1 changed files with 30 additions and 23 deletions
|
@ -202,15 +202,9 @@ module.exports = class MemberRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe member to default newsletters
|
// Subscribe member to default newsletters
|
||||||
if (!memberData.newsletters && this._labsService.isSet('multipleNewsletters')) {
|
if (memberData.subscribed !== false && !memberData.newsletters) {
|
||||||
const browseOptions = _.pick(options, 'transacting');
|
const browseOptions = _.pick(options, 'transacting');
|
||||||
|
memberData.newsletters = await this.getSubscribeOnSignupNewsletters(browseOptions);
|
||||||
// By default subscribe to all active auto opt-in newsletters with members visibility
|
|
||||||
//TODO: Will mostly need to be updated later for paid-only newsletters
|
|
||||||
browseOptions.filter = 'status:active+subscribe_on_signup:true+visibility:members';
|
|
||||||
const newsletters = await this._newslettersService.browse(browseOptions);
|
|
||||||
|
|
||||||
memberData.newsletters = newsletters || [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const member = await this._Member.add({
|
const member = await this._Member.add({
|
||||||
|
@ -273,11 +267,26 @@ module.exports = class MemberRepository {
|
||||||
return member;
|
return member;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getSubscribeOnSignupNewsletters(browseOptions) {
|
||||||
|
// By default subscribe to all active auto opt-in newsletters with members visibility
|
||||||
|
//TODO: Will mostly need to be updated later for paid-only newsletters
|
||||||
|
browseOptions.filter = 'status:active+subscribe_on_signup:true+visibility:members';
|
||||||
|
const newsletters = await this._newslettersService.browse(browseOptions);
|
||||||
|
return newsletters || [];
|
||||||
|
}
|
||||||
|
|
||||||
async update(data, options) {
|
async update(data, options) {
|
||||||
const sharedOptions = {
|
const sharedOptions = {
|
||||||
transacting: options.transacting
|
transacting: options.transacting
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const initialMember = await this._Member.findOne({
|
||||||
|
id: options.id
|
||||||
|
}, {...sharedOptions, withRelated: ['products', 'newsletters']});
|
||||||
|
|
||||||
|
const existingProducts = initialMember.related('products').models;
|
||||||
|
const existingNewsletters = initialMember.related('newsletters').models;
|
||||||
|
|
||||||
const memberData = _.pick(data, [
|
const memberData = _.pick(data, [
|
||||||
'email',
|
'email',
|
||||||
'name',
|
'name',
|
||||||
|
@ -294,12 +303,6 @@ module.exports = class MemberRepository {
|
||||||
let productsToAdd = [];
|
let productsToAdd = [];
|
||||||
let productsToRemove = [];
|
let productsToRemove = [];
|
||||||
if (this._stripeAPIService.configured && data.products) {
|
if (this._stripeAPIService.configured && data.products) {
|
||||||
const member = await this._Member.findOne({
|
|
||||||
id: options.id
|
|
||||||
}, sharedOptions);
|
|
||||||
|
|
||||||
const existingProducts = await member.related('products').fetch(sharedOptions);
|
|
||||||
|
|
||||||
const existingProductIds = existingProducts.map(product => product.id);
|
const existingProductIds = existingProducts.map(product => product.id);
|
||||||
const incomingProductIds = data.products.map(product => product.id);
|
const incomingProductIds = data.products.map(product => product.id);
|
||||||
|
|
||||||
|
@ -312,7 +315,7 @@ module.exports = class MemberRepository {
|
||||||
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 initialMember.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'));
|
||||||
});
|
});
|
||||||
|
@ -342,18 +345,22 @@ module.exports = class MemberRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This maps the old susbcribed property to the new newsletters field
|
||||||
|
if (!memberData.newsletters) {
|
||||||
|
if (memberData.subscribed === false) {
|
||||||
|
memberData.newsletters = [];
|
||||||
|
} else if (memberData.subscribed === true && !existingNewsletters.find((n) => n.status === 'active')) {
|
||||||
|
const browseOptions = _.pick(options, 'transacting');
|
||||||
|
memberData.newsletters = await this.getSubscribeOnSignupNewsletters(browseOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Keep track of the newsletters that were added and removed of a member so we can generate the corresponding events
|
// Keep track of the newsletters that were added and removed of a member so we can generate the corresponding events
|
||||||
let newslettersToAdd = [];
|
let newslettersToAdd = [];
|
||||||
let newslettersToRemove = [];
|
let newslettersToRemove = [];
|
||||||
if (data.newsletters) {
|
if (memberData.newsletters) {
|
||||||
const member = await this._Member.findOne({
|
|
||||||
id: options.id
|
|
||||||
}, sharedOptions);
|
|
||||||
|
|
||||||
const existingNewsletters = await member.related('newsletters').fetch(sharedOptions);
|
|
||||||
|
|
||||||
const existingNewsletterIds = existingNewsletters.map(newsletter => newsletter.id);
|
const existingNewsletterIds = existingNewsletters.map(newsletter => newsletter.id);
|
||||||
const incomingNewsletterIds = data.newsletters.map(newsletter => newsletter.id);
|
const incomingNewsletterIds = memberData.newsletters.map(newsletter => newsletter.id);
|
||||||
|
|
||||||
newslettersToAdd = _.differenceWith(incomingNewsletterIds, existingNewsletterIds);
|
newslettersToAdd = _.differenceWith(incomingNewsletterIds, existingNewsletterIds);
|
||||||
newslettersToRemove = _.differenceWith(existingNewsletterIds, incomingNewsletterIds);
|
newslettersToRemove = _.differenceWith(existingNewsletterIds, incomingNewsletterIds);
|
||||||
|
|
Loading…
Add table
Reference in a new issue