Added newsletters to Portal site data

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

- allows Portal access to multiple newsletters on a site
- allows Portal to update member's newsletter subscription preference
This commit is contained in:
Rishabh 2022-04-04 18:15:40 +05:30 committed by Rishabh Garg
parent cc7c51e140
commit 16f35df448
2 changed files with 26 additions and 4 deletions

View File

@ -70,12 +70,12 @@ const getOfferData = async function (req, res) {
const updateMemberData = async function (req, res) {
try {
const data = _.pick(req.body, 'name', 'subscribed');
const data = _.pick(req.body, 'name', 'subscribed', 'newsletters');
const member = await membersService.ssr.getMemberDataFromSession(req, res);
if (member) {
const options = {
id: member.id,
withRelated: ['stripeSubscriptions', 'stripeSubscriptions.customer', 'stripeSubscriptions.stripePrice']
withRelated: ['stripeSubscriptions', 'stripeSubscriptions.customer', 'stripeSubscriptions.stripePrice', 'newsletters']
};
const updatedMember = await membersService.api.members.update(data, options);
@ -133,6 +133,11 @@ const getPortalProductPrices = async function () {
};
};
const getSiteNewsletters = async function () {
const newsletters = await models.Newsletter.findAll();
return newsletters.toJSON();
};
const getMemberSiteData = async function (req, res) {
const isStripeConfigured = membersService.config.isStripeConnected();
const domain = urlUtils.urlFor('home', true).match(new RegExp('^https?://([^/:?#]+)(?:[/:?#]|$)', 'i'));
@ -144,7 +149,7 @@ const getMemberSiteData = async function (req, res) {
}
const {products = [], prices = []} = await getPortalProductPrices() || {};
const portalVersion = config.get('portal:version');
const newsletters = await getSiteNewsletters();
const response = {
title: settingsCache.get('title'),
description: settingsCache.get('description'),
@ -170,6 +175,11 @@ const getMemberSiteData = async function (req, res) {
prices,
products
};
if (labsService.isSet('multipleNewsletters')) {
response.newsletters = newsletters;
}
if (labsService.isSet('multipleProducts')) {
response.portal_products = settingsCache.get('portal_products');
}

View File

@ -1,8 +1,16 @@
const labsService = require('../../../shared/labs');
function formatNewsletterResponse(newsletters) {
return newsletters.map(({id, name, description, sort_order: sortOrder}) => {
return {id, name, description, sort_order: sortOrder};
});
}
module.exports.formattedMemberResponse = function formattedMemberResponse(member) {
if (!member) {
return null;
}
return {
const data = {
uuid: member.uuid,
email: member.email,
name: member.name,
@ -12,4 +20,8 @@ module.exports.formattedMemberResponse = function formattedMemberResponse(member
subscriptions: member.subscriptions || [],
paid: member.status !== 'free'
};
if (member.newsletters && labsService.isSet('multipleNewsletters')) {
data.newsletters = formatNewsletterResponse(member.newsletters);
}
return data;
};