Files
speckle-server/packages/server/modules/gatekeeper/services/prices.ts
T
Kristaps Fabians Geikins 211922b6a6 chore: get rid of all old workspace plan code (#4624)
* first batch of changes

* tests fix

* FE fixed

* renaming constants

* test fixes

* moar test fixes

* another test fix

* reenable app rover check

---------

Co-authored-by: Gergő Jedlicska <gergo@jedlicska.com>
2025-04-30 19:18:32 +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 {
PaidWorkspacePlans,
TIME_MS,
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(PaidWorkspacePlans).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: TIME_MS.day,
cacheProvider: redisCacheProviderFactory()
})