0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

Updated guard for site data in Portal

closes https://github.com/TryGhost/Team/issues/2276

Portal had died with an unintelligible error about portal plans/includes being undefined when there was missing site data in some extreme edge cases. This change catches any errors in site transformation and logs it to console instead of crashing portal unexpectedly
This commit is contained in:
Rishabh 2023-01-13 21:01:58 +05:30
parent c4391567f3
commit fea75365c7

View file

@ -248,68 +248,73 @@ export function hasCommentsEnabled({site}) {
}
export function transformApiSiteData({site}) {
if (!site) {
return null;
}
try {
if (!site) {
return null;
}
if (site.tiers) {
site.products = site.tiers;
}
if (site.tiers) {
site.products = site.tiers;
}
site.products = site.products?.map((product) => {
return {
...product,
monthlyPrice: product.monthly_price,
yearlyPrice: product.yearly_price
};
});
site.products = site.products?.map((product) => {
return {
...product,
monthlyPrice: product.monthly_price,
yearlyPrice: product.yearly_price
};
});
site.is_stripe_configured = !!site.paid_members_enabled;
site.members_signup_access = 'all';
site.is_stripe_configured = !!site.paid_members_enabled;
site.members_signup_access = 'all';
if (!site.members_enabled) {
site.members_signup_access = 'none';
}
if (!site.members_enabled) {
site.members_signup_access = 'none';
}
if (site.members_invite_only) {
site.members_signup_access = 'invite';
}
if (site.members_invite_only) {
site.members_signup_access = 'invite';
}
site.allow_self_signup = false;
if (site.members_signup_access !== 'all') {
site.allow_self_signup = false;
}
// if stripe is not connected then selected plans mean nothing.
// disabling signup would be done by switching to "invite only" mode
if (site.paid_members_enabled) {
site.allow_self_signup = true;
}
if (site.members_signup_access !== 'all') {
site.allow_self_signup = false;
}
// self signup must be available for free plan signup to work
if (site.portal_plans?.includes('free')) {
site.allow_self_signup = true;
}
// if stripe is not connected then selected plans mean nothing.
// disabling signup would be done by switching to "invite only" mode
if (site.paid_members_enabled) {
site.allow_self_signup = true;
}
// Map tier visibility to old settings
if (site.products?.[0]?.visibility) {
// self signup must be available for free plan signup to work
if (site.portal_plans?.includes('free')) {
site.allow_self_signup = true;
}
// Map tier visibility to old settings
if (site.products?.[0]?.visibility) {
// Map paid tier visibility to portal products
site.portal_products = site.products.filter((p) => {
return p.visibility !== 'none' && p.type === 'paid';
}).map(p => p.id);
site.portal_products = site.products.filter((p) => {
return p.visibility !== 'none' && p.type === 'paid';
}).map(p => p.id);
// Map free tier visibility to portal plans
const freeProduct = site.products.find(p => p.type === 'free');
if (freeProduct) {
site.portal_plans = site.portal_plans?.filter(d => d !== 'free');
if (freeProduct?.visibility === 'public') {
site.portal_plans?.push('free');
// Map free tier visibility to portal plans
const freeProduct = site.products.find(p => p.type === 'free');
if (freeProduct) {
site.portal_plans = site.portal_plans?.filter(d => d !== 'free');
if (freeProduct?.visibility === 'public') {
site.portal_plans?.push('free');
}
}
}
}
return site;
return site;
} catch (error) {
/* eslint-disable no-console */
console.warn(`[Portal] Failed to read site data:`, error);
}
}
export function getAvailableProducts({site}) {