bde148f286
* wip * some extra fixes * stuff kinda works? * need to figure out mocks * need to figure out mocks * fix db listener * gqlgen fix * minor gqlgen watch adjustment * lint fixes * delete old codegen file * converting migrations to ESM * getModuleDIrectory * vitest sort of works * added back ts-vitest * resolve gql double load * fixing test timeout configs * TSC lint fix * fix automate tests * moar debugging * debugging * more debugging * codegen update * server works * yargs migrated * chore(server): getting rid of global mocks for Server ESM (#5046) * got rid of email mock * got rid of comment mocks * got rid of multi region mocks * got rid of stripe mock * admin override mock updated * removed final mock * fixing import.meta.resolve calls * another import.meta.resolve fix * added requested test * nyc ESM fix * removed unneeded deps + linting * yarn lock forgot to commit * tryna fix flakyness * email capture util fix * sendEmail fix * fix TSX check * sender transporter fix + CR comments * merge main fix * test fixx * circleci fix * gqlgen bigint fix * error formatter fix * more error formatting improvements * esmloader added to Dockerfile * more dockerfile fixes * bg jobs fix
72 lines
2.3 KiB
TypeScript
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-es'
|
|
|
|
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()
|
|
})
|