Files
speckle-server/packages/server/modules/gatekeeper/services/subscriptions/mutateSubscriptionDataWithNewValidSeatNumbers.ts
T
Daniel Gak Anagrov 3e7e11b8a1 feat(gatekeeper): add error log on seat mismatch (#5004)
* feat: added a specific errors on downscale issues
2025-07-07 12:28:59 +02:00

46 lines
1.3 KiB
TypeScript

import {
GetWorkspacePlanProductId,
SubscriptionDataInput
} from '@/modules/gatekeeper/domain/billing'
import { WorkspacePricingProducts } from '@/modules/gatekeeperCore/domain/billing'
import { LogicError } from '@/modules/shared/errors'
import { SubscriptionStateError } from '@/modules/gatekeeper/errors/billing'
export const mutateSubscriptionDataWithNewValidSeatNumbers = ({
seatCount,
workspacePlan,
getWorkspacePlanProductId,
subscriptionData
}: {
seatCount: number
workspacePlan: WorkspacePricingProducts
getWorkspacePlanProductId: GetWorkspacePlanProductId
subscriptionData: SubscriptionDataInput
}): void => {
const productId = getWorkspacePlanProductId({ workspacePlan })
const product = subscriptionData.products.find(
(product) => product.productId === productId
)
if (product === undefined && seatCount === 0) return
if (product === undefined) {
throw new LogicError('Product not found at mutation')
}
if (seatCount < 0) {
throw new LogicError('Invalid seat count, cannot be negative')
}
if (product.quantity < seatCount) {
throw new SubscriptionStateError('Subscription missing an upscale')
}
if (seatCount === 0) {
const prodIndex = subscriptionData.products.indexOf(product)
subscriptionData.products.splice(prodIndex, 1)
return
}
product.quantity = seatCount
}