From b1461d699fe04fa9a5f82c7904a8eb1ed83d6332 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Fri, 6 Oct 2023 20:53:23 +0100 Subject: [PATCH] Doc: Handle archived prices --- packages/lib/utils/joplinCloud.ts | 38 +++++++++++++++++++------------ packages/server/stripeConfig.json | 19 +++++++++++++++- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/packages/lib/utils/joplinCloud.ts b/packages/lib/utils/joplinCloud.ts index 581fa1066..796b698d6 100644 --- a/packages/lib/utils/joplinCloud.ts +++ b/packages/lib/utils/joplinCloud.ts @@ -65,6 +65,7 @@ export interface StripePublicConfigPrice { export interface StripePublicConfig { publishableKey: string; prices: StripePublicConfigPrice[]; + archivedPrices: StripePublicConfigPrice[]; webhookBaseUrl: string; } @@ -86,26 +87,33 @@ export function loadStripeConfig(env: string, filePath: string): StripePublicCon const config: StripePublicConfig = JSON.parse(fs.readFileSync(filePath, 'utf8'))[env]; if (!config) throw new Error(`Invalid env: ${env}`); - config.prices = config.prices.map(p => { + const decoratePrices = (p: StripePublicConfigPrice) => { return { ...p, formattedAmount: formatPrice(p.amount, p.currency), formattedMonthlyAmount: p.period === PricePeriod.Monthly ? formatPrice(p.amount, p.currency) : formatPrice(Number(p.amount) / 12, p.currency), }; - }); + }; + + config.prices = config.prices.map(decoratePrices); + config.archivedPrices = config.archivedPrices.map(decoratePrices); return config; } -export function findPrice(prices: StripePublicConfigPrice[], query: FindPriceQuery): StripePublicConfigPrice { +export function findPrice(config: StripePublicConfig, query: FindPriceQuery): StripePublicConfigPrice { let output: StripePublicConfigPrice = null; - if (query.accountType && query.period) { - output = prices.filter(p => p.accountType === query.accountType).find(p => p.period === query.period); - } else if (query.priceId) { - output = prices.find(p => p.id === query.priceId); - } else { - throw new Error(`Invalid query: ${JSON.stringify(query)}`); + for (const prices of [config.prices, config.archivedPrices]) { + if (query.accountType && query.period) { + output = prices.filter(p => p.accountType === query.accountType).find(p => p.period === query.period); + } else if (query.priceId) { + output = prices.find(p => p.id === query.priceId); + } else { + throw new Error(`Invalid query: ${JSON.stringify(query)}`); + } + + if (output) break; } if (!output) throw new Error(`Not found: ${JSON.stringify(query)}`); @@ -332,11 +340,11 @@ export function getPlans(stripeConfig: StripePublicConfig): Record