Files
speckle-server/packages/server/modules/gatekeeper/services/prices.ts
T
Gergő Jedlicska 61ca128ce2 gergo/multiCurrency (#4379)
* feat(gatekeeper): support multiple currencies

* feat(helm): add new currency based prices to helm chart

* chore(env): add example currency based pricing values

* fix(ci): update price ids to the proper values

* feat(helm): rename price ids to fit multi currency

* feat(gatekeeper): currency input for checkout session

* Updated prices in the FE

* chore(gatekeeper): remove old checkout session flow

* Updated prices in the FE

* Fix FE

* Fix pipeline

---------

Co-authored-by: Mike Tasset <mike.tasset@gmail.com>
2025-04-11 17:37:47 +02:00

72 lines
2.3 KiB
TypeScript

import {
GetRecurringPrices,
GetWorkspacePlanProductPrices,
GetWorkspacePlanProductAndPriceIds
} from '@/modules/gatekeeper/domain/billing'
import {
Currency,
WorkspacePlanProductPrices
} from '@/modules/gatekeeperCore/domain/billing'
import { MisconfiguredEnvironmentError } from '@/modules/shared/errors'
import {
redisCacheProviderFactory,
wrapFactoryWithCache
} from '@/modules/shared/utils/caching'
import {
PaidWorkspacePlansNew,
TIME,
WorkspacePlanBillingIntervals
} from '@speckle/shared'
import { set } from 'lodash'
export const getFreshWorkspacePlanProductPricesFactory =
(deps: {
getRecurringPrices: GetRecurringPrices
getWorkspacePlanProductAndPriceIds: GetWorkspacePlanProductAndPriceIds
}): GetWorkspacePlanProductPrices =>
async () => {
const prices = await deps.getRecurringPrices()
const productAndPriceIds = deps.getWorkspacePlanProductAndPriceIds()
const productPrices = Object.values(Currency).reduce((acc, currency) => {
const currencyPrices = Object.values(PaidWorkspacePlansNew).reduce(
(acc, paidPlan) => {
const intervalPrices = Object.values(WorkspacePlanBillingIntervals).reduce(
(acc, interval) => {
const product = productAndPriceIds[paidPlan]
const priceId = product[interval][currency]
const price = prices.find(
(p) =>
p.productId === product.productId &&
p.id === priceId &&
p.currency === currency
)
if (!price)
throw new MisconfiguredEnvironmentError(
`${interval} price not found for ${paidPlan} plan and ${currency} currency`
)
set(acc, interval, { amount: price.unitAmount / 100, currency })
return acc
},
{}
)
set(acc, paidPlan, intervalPrices)
return acc
},
{}
)
set(acc, currency, currencyPrices)
return acc
}, {}) as WorkspacePlanProductPrices
return productPrices
}
export const getWorkspacePlanProductPricesFactory = wrapFactoryWithCache({
factory: getFreshWorkspacePlanProductPricesFactory,
name: `modules/gatekeeper/services/prices:getWorkspacePlanPricesFactory`,
ttlMs: 1000 * TIME.day, // 1 day
cacheProvider: redisCacheProviderFactory()
})