Files
speckle-server/packages/server/modules/gatekeeper/services/prices.ts
T
Gergő Jedlicska a3644a50c5 gergo/web 2931 introduce missing workspace plans (#4323)
* fix(gatekeeper): missing priceId-s should stop the server from booting

* feat(shared): add all new workspace plans

* feat(billing): add new world plans

* feat(ci): use stripe sandbox id-s from test env vars

* chore(ci): remove defunct stripe context

* chore(server-env): fix server env example

* feat(gatekeeper): add missing plans to graphql schema

* test(gatekeeper): update plan upgrade tests to match implementation

* fix(ci): update ci .env source

* fix(ci): we do need some secrets from stripe

* fix(ci): run gql schema introspect with the test env

* fix(frontend): format new plan names

* chore(ci): temp disable gql schema checks

* feat(helm): add unlimited seat prices env vars

* feat(helm): add unlimited seat prices to values

* feat(gatekeeper): do not load priceId-s as a side effect, load with module init
2025-04-07 15:24:57 +02:00

74 lines
2.4 KiB
TypeScript

import {
GetRecurringPrices,
GetWorkspacePlanProductPrices,
GetWorkspacePlanProductAndPriceIds
} from '@/modules/gatekeeper/domain/billing'
import { WorkspacePlanProductPrices } from '@/modules/gatekeeperCore/domain/billing'
import { MisconfiguredEnvironmentError } from '@/modules/shared/errors'
import {
redisCacheProviderFactory,
wrapFactoryWithCache
} from '@/modules/shared/utils/caching'
import { Optional, TIME } from '@speckle/shared'
import { set } from 'lodash'
export const getFreshWorkspacePlanProductPricesFactory =
(deps: {
getRecurringPrices: GetRecurringPrices
getWorkspacePlanProductAndPriceIds: GetWorkspacePlanProductAndPriceIds
}): GetWorkspacePlanProductPrices =>
async () => {
const productPrices = await deps.getRecurringPrices()
const productAndPriceIds = deps.getWorkspacePlanProductAndPriceIds()
const ret = Object.entries(productAndPriceIds).reduce((acc, [plan, planIds]) => {
const { productId, monthly } = planIds
const monthlyPrice = productPrices.find(
(p) => p.id === monthly && p.productId === productId
)
if (!monthlyPrice)
throw new MisconfiguredEnvironmentError(
`Price ${monthly} not found for plan ${plan}`
)
const monthlyStruct = {
amount: monthlyPrice.unitAmount / 100,
currency: monthlyPrice.currency.toUpperCase()
}
let yearlyStruct: Optional<typeof monthlyStruct> = undefined
if ('yearly' in planIds) {
const { yearly } = planIds
const yearlyPrice = productPrices.find((p) => p.id === yearly)
if (!yearlyPrice) {
throw new MisconfiguredEnvironmentError(
`Price ${yearly} not found for plan ${plan}`
)
}
yearlyStruct = {
amount: yearlyPrice.unitAmount / 100,
currency: yearlyPrice.currency.toUpperCase()
}
}
const result = {
monthly: monthlyStruct,
productId,
...(yearlyStruct ? { yearly: yearlyStruct } : {})
}
// TS typing is tricky here cause of type differences between plans
set(acc, plan, result)
return acc
}, {} as WorkspacePlanProductPrices)
return ret
}
export const getWorkspacePlanProductPricesFactory = wrapFactoryWithCache({
factory: getFreshWorkspacePlanProductPricesFactory,
name: `modules/gatekeeper/services/prices:getWorkspacePlanPricesFactory`,
ttlMs: 1000 * TIME.day, // 1 day
cacheProvider: redisCacheProviderFactory()
})