🛂 Check if isQuarantined can be toggled on sub update
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
|||||||
import { chatPriceIds, storagePriceIds } from './getSubscription'
|
import { chatPriceIds, storagePriceIds } from './getSubscription'
|
||||||
import { createCheckoutSessionUrl } from './createCheckoutSession'
|
import { createCheckoutSessionUrl } from './createCheckoutSession'
|
||||||
import { isAdminWriteWorkspaceForbidden } from '@/features/workspace/helpers/isAdminWriteWorkspaceForbidden'
|
import { isAdminWriteWorkspaceForbidden } from '@/features/workspace/helpers/isAdminWriteWorkspaceForbidden'
|
||||||
|
import { getUsage } from '@typebot.io/lib/api/getUsage'
|
||||||
|
|
||||||
export const updateSubscription = authenticatedProcedure
|
export const updateSubscription = authenticatedProcedure
|
||||||
.meta({
|
.meta({
|
||||||
@@ -66,6 +67,7 @@ export const updateSubscription = authenticatedProcedure
|
|||||||
id: workspaceId,
|
id: workspaceId,
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
|
isQuarantined: true,
|
||||||
stripeId: true,
|
stripeId: true,
|
||||||
members: {
|
members: {
|
||||||
select: {
|
select: {
|
||||||
@@ -159,13 +161,25 @@ export const updateSubscription = authenticatedProcedure
|
|||||||
return { checkoutUrl }
|
return { checkoutUrl }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let isQuarantined = workspace.isQuarantined
|
||||||
|
|
||||||
|
if (isQuarantined) {
|
||||||
|
const newChatsLimit = getChatsLimit({
|
||||||
|
plan,
|
||||||
|
additionalChatsIndex: additionalChats,
|
||||||
|
customChatsLimit: null,
|
||||||
|
})
|
||||||
|
const { totalChatsUsed } = await getUsage(prisma)(workspaceId)
|
||||||
|
if (totalChatsUsed < newChatsLimit) isQuarantined = false
|
||||||
|
}
|
||||||
|
|
||||||
const updatedWorkspace = await prisma.workspace.update({
|
const updatedWorkspace = await prisma.workspace.update({
|
||||||
where: { id: workspaceId },
|
where: { id: workspaceId },
|
||||||
data: {
|
data: {
|
||||||
plan,
|
plan,
|
||||||
additionalChatsIndex: additionalChats,
|
additionalChatsIndex: additionalChats,
|
||||||
additionalStorageIndex: additionalStorage,
|
additionalStorageIndex: additionalStorage,
|
||||||
isQuarantined: false,
|
isQuarantined,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
52
packages/lib/api/getUsage.ts
Normal file
52
packages/lib/api/getUsage.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import { PrismaClient } from '@typebot.io/prisma'
|
||||||
|
|
||||||
|
export const getUsage =
|
||||||
|
(prisma: PrismaClient) => async (workspaceId: string) => {
|
||||||
|
const now = new Date()
|
||||||
|
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
|
||||||
|
const firstDayOfNextMonth = new Date(
|
||||||
|
now.getFullYear(),
|
||||||
|
now.getMonth() + 1,
|
||||||
|
1
|
||||||
|
)
|
||||||
|
const typebots = await prisma.typebot.findMany({
|
||||||
|
where: {
|
||||||
|
workspace: {
|
||||||
|
id: workspaceId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
select: { id: true },
|
||||||
|
})
|
||||||
|
|
||||||
|
const [
|
||||||
|
totalChatsUsed,
|
||||||
|
{
|
||||||
|
_sum: { storageUsed: totalStorageUsed },
|
||||||
|
},
|
||||||
|
] = await Promise.all([
|
||||||
|
prisma.result.count({
|
||||||
|
where: {
|
||||||
|
typebotId: { in: typebots.map((typebot) => typebot.id) },
|
||||||
|
hasStarted: true,
|
||||||
|
createdAt: {
|
||||||
|
gte: firstDayOfMonth,
|
||||||
|
lt: firstDayOfNextMonth,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
prisma.answer.aggregate({
|
||||||
|
where: {
|
||||||
|
storageUsed: { gt: 0 },
|
||||||
|
result: {
|
||||||
|
typebotId: { in: typebots.map((typebot) => typebot.id) },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
_sum: { storageUsed: true },
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
|
||||||
|
return {
|
||||||
|
totalChatsUsed,
|
||||||
|
totalStorageUsed: totalStorageUsed ?? 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
} from '@typebot.io/prisma'
|
} from '@typebot.io/prisma'
|
||||||
import { isDefined } from '@typebot.io/lib'
|
import { isDefined } from '@typebot.io/lib'
|
||||||
import { getChatsLimit } from '@typebot.io/lib/pricing'
|
import { getChatsLimit } from '@typebot.io/lib/pricing'
|
||||||
|
import { getUsage } from '@typebot.io/lib/api/getUsage'
|
||||||
import { promptAndSetEnvironment } from './utils'
|
import { promptAndSetEnvironment } from './utils'
|
||||||
import { Workspace } from '@typebot.io/schemas'
|
import { Workspace } from '@typebot.io/schemas'
|
||||||
import { sendAlmostReachedChatsLimitEmail } from '@typebot.io/emails/src/emails/AlmostReachedChatsLimitEmail'
|
import { sendAlmostReachedChatsLimitEmail } from '@typebot.io/emails/src/emails/AlmostReachedChatsLimitEmail'
|
||||||
@@ -123,7 +124,7 @@ const sendAlertIfLimitReached = async (
|
|||||||
if (taggedWorkspaces.includes(workspace.id) || workspace.isQuarantined)
|
if (taggedWorkspaces.includes(workspace.id) || workspace.isQuarantined)
|
||||||
continue
|
continue
|
||||||
taggedWorkspaces.push(workspace.id)
|
taggedWorkspaces.push(workspace.id)
|
||||||
const { totalChatsUsed } = await getUsage(workspace.id)
|
const { totalChatsUsed } = await getUsage(prisma)(workspace.id)
|
||||||
const chatsLimit = getChatsLimit(workspace)
|
const chatsLimit = getChatsLimit(workspace)
|
||||||
if (
|
if (
|
||||||
chatsLimit > 0 &&
|
chatsLimit > 0 &&
|
||||||
@@ -204,50 +205,4 @@ const sendAlertIfLimitReached = async (
|
|||||||
return events
|
return events
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUsage = async (workspaceId: string) => {
|
|
||||||
const now = new Date()
|
|
||||||
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
|
|
||||||
const firstDayOfNextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1)
|
|
||||||
const typebots = await prisma.typebot.findMany({
|
|
||||||
where: {
|
|
||||||
workspace: {
|
|
||||||
id: workspaceId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
select: { id: true },
|
|
||||||
})
|
|
||||||
|
|
||||||
const [
|
|
||||||
totalChatsUsed,
|
|
||||||
{
|
|
||||||
_sum: { storageUsed: totalStorageUsed },
|
|
||||||
},
|
|
||||||
] = await Promise.all([
|
|
||||||
prisma.result.count({
|
|
||||||
where: {
|
|
||||||
typebotId: { in: typebots.map((typebot) => typebot.id) },
|
|
||||||
hasStarted: true,
|
|
||||||
createdAt: {
|
|
||||||
gte: firstDayOfMonth,
|
|
||||||
lt: firstDayOfNextMonth,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
prisma.answer.aggregate({
|
|
||||||
where: {
|
|
||||||
storageUsed: { gt: 0 },
|
|
||||||
result: {
|
|
||||||
typebotId: { in: typebots.map((typebot) => typebot.id) },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
_sum: { storageUsed: true },
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
|
|
||||||
return {
|
|
||||||
totalChatsUsed,
|
|
||||||
totalStorageUsed: totalStorageUsed ?? 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sendTotalResultsDigest().then()
|
sendTotalResultsDigest().then()
|
||||||
|
|||||||
Reference in New Issue
Block a user