chore(workspaces): fix build
This commit is contained in:
@@ -294,120 +294,6 @@ export const addWorkspaceSubscriptionSeatIfNeededFactoryOld =
|
||||
})
|
||||
}
|
||||
|
||||
type DownscaleWorkspaceSubscription = (args: {
|
||||
workspaceSubscription: WorkspaceSubscription
|
||||
}) => Promise<boolean>
|
||||
|
||||
export const downscaleWorkspaceSubscriptionFactory =
|
||||
({
|
||||
getWorkspacePlan,
|
||||
countWorkspaceRole,
|
||||
getWorkspacePlanProductId,
|
||||
reconcileSubscriptionData
|
||||
}: {
|
||||
getWorkspacePlan: GetWorkspacePlan
|
||||
countWorkspaceRole: CountWorkspaceRoleWithOptionalProjectRole
|
||||
getWorkspacePlanProductId: GetWorkspacePlanProductId
|
||||
reconcileSubscriptionData: ReconcileSubscriptionData
|
||||
}): DownscaleWorkspaceSubscription =>
|
||||
async ({ workspaceSubscription }) => {
|
||||
const workspaceId = workspaceSubscription.workspaceId
|
||||
|
||||
const workspacePlan = await getWorkspacePlan({ workspaceId })
|
||||
if (!workspacePlan) throw new WorkspacePlanNotFoundError()
|
||||
|
||||
switch (workspacePlan.name) {
|
||||
case 'team':
|
||||
case 'pro':
|
||||
// Cause seat types matter, a future issue
|
||||
throw new NotImplementedError()
|
||||
case 'starter':
|
||||
case 'plus':
|
||||
case 'business':
|
||||
break
|
||||
case 'unlimited':
|
||||
case 'academia':
|
||||
case 'starterInvoiced':
|
||||
case 'plusInvoiced':
|
||||
case 'businessInvoiced':
|
||||
case 'free':
|
||||
throw new WorkspacePlanMismatchError()
|
||||
default:
|
||||
throwUncoveredError(workspacePlan)
|
||||
}
|
||||
|
||||
if (workspacePlan.status === 'canceled') return false
|
||||
|
||||
// TODO: Guests will be able to have a paid seat
|
||||
const [guestCount, memberCount, adminCount] = await Promise.all([
|
||||
countWorkspaceRole({ workspaceId, workspaceRole: 'workspace:guest' }),
|
||||
countWorkspaceRole({ workspaceId, workspaceRole: 'workspace:member' }),
|
||||
countWorkspaceRole({ workspaceId, workspaceRole: 'workspace:admin' })
|
||||
])
|
||||
|
||||
const subscriptionData = cloneDeep(workspaceSubscription.subscriptionData)
|
||||
|
||||
mutateSubscriptionDataWithNewValidSeatNumbers({
|
||||
seatCount: guestCount,
|
||||
workspacePlan: 'guest',
|
||||
getWorkspacePlanProductId,
|
||||
subscriptionData
|
||||
})
|
||||
mutateSubscriptionDataWithNewValidSeatNumbers({
|
||||
seatCount: memberCount + adminCount,
|
||||
workspacePlan: workspacePlan.name,
|
||||
getWorkspacePlanProductId,
|
||||
subscriptionData
|
||||
})
|
||||
|
||||
if (!isEqual(subscriptionData, workspaceSubscription.subscriptionData)) {
|
||||
await reconcileSubscriptionData({ subscriptionData, prorationBehavior: 'none' })
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export const manageSubscriptionDownscaleFactory =
|
||||
({
|
||||
getWorkspaceSubscriptions,
|
||||
downscaleWorkspaceSubscription,
|
||||
updateWorkspaceSubscription
|
||||
}: {
|
||||
getWorkspaceSubscriptions: GetWorkspaceSubscriptions
|
||||
downscaleWorkspaceSubscription: DownscaleWorkspaceSubscription
|
||||
updateWorkspaceSubscription: UpsertWorkspaceSubscription
|
||||
}) =>
|
||||
async (context: { logger: Logger }) => {
|
||||
const { logger } = context
|
||||
const subscriptions = await getWorkspaceSubscriptions()
|
||||
for (const workspaceSubscription of subscriptions) {
|
||||
const log = logger.child({ workspaceId: workspaceSubscription.workspaceId })
|
||||
try {
|
||||
const subDownscaled = await downscaleWorkspaceSubscription({
|
||||
workspaceSubscription
|
||||
})
|
||||
if (subDownscaled) {
|
||||
log.info(
|
||||
'Downscaled workspace subscription to match the current workspace team'
|
||||
)
|
||||
} else {
|
||||
log.info('Did not need to downscale the workspace subscription')
|
||||
}
|
||||
} catch (err) {
|
||||
log.error({ err }, 'Failed to downscale workspace subscription')
|
||||
}
|
||||
const newBillingCycleEnd = calculateNewBillingCycleEnd({ workspaceSubscription })
|
||||
const updatedWorkspaceSubscription = {
|
||||
...workspaceSubscription,
|
||||
currentBillingCycleEnd: newBillingCycleEnd
|
||||
}
|
||||
await updateWorkspaceSubscription({
|
||||
workspaceSubscription: updatedWorkspaceSubscription
|
||||
})
|
||||
log.info({ updatedWorkspaceSubscription }, 'Updated workspace billing cycle end')
|
||||
}
|
||||
}
|
||||
|
||||
export const getTotalSeatsCountByPlanFactory =
|
||||
({
|
||||
getWorkspacePlanProductId
|
||||
|
||||
@@ -147,6 +147,7 @@ describe('Workspaces Billing', () => {
|
||||
customerId: cryptoRandomString({ length: 10 }),
|
||||
cancelAt: null,
|
||||
status: 'active',
|
||||
currentPeriodEnd: new Date(),
|
||||
products: [
|
||||
{
|
||||
priceId: createRandomString(),
|
||||
|
||||
Reference in New Issue
Block a user