diff --git a/core/server/services/members/middleware.js b/core/server/services/members/middleware.js index 1446d7e790..2aedceb128 100644 --- a/core/server/services/members/middleware.js +++ b/core/server/services/members/middleware.js @@ -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'); } diff --git a/core/server/services/members/utils.js b/core/server/services/members/utils.js index 3b4c057826..1fe63cfb56 100644 --- a/core/server/services/members/utils.js +++ b/core/server/services/members/utils.js @@ -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; };