2
0

♻️ Remove storage limit related code

This commit is contained in:
Baptiste Arnaud
2023-09-20 16:06:53 +02:00
parent 7d57e8dd06
commit d7dc5fb5fb
25 changed files with 44 additions and 493 deletions

View File

@@ -226,7 +226,6 @@ const saveAnswer =
groupId: block.groupId,
content: reply,
variableId: block.options.variableId,
storageUsed: 0,
},
reply,
state,

View File

@@ -18,12 +18,7 @@ export const getUsage =
select: { id: true },
})
const [
totalChatsUsed,
{
_sum: { storageUsed: totalStorageUsed },
},
] = await Promise.all([
const [totalChatsUsed] = await Promise.all([
prisma.result.count({
where: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
@@ -34,19 +29,9 @@ export const getUsage =
},
},
}),
prisma.answer.aggregate({
where: {
storageUsed: { gt: 0 },
result: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
},
},
_sum: { storageUsed: true },
}),
])
return {
totalChatsUsed,
totalStorageUsed: totalStorageUsed ?? 0,
}
}

View File

@@ -22,7 +22,6 @@ type CreateFakeResultsProps = {
count: number
customResultIdPrefix?: string
isChronological?: boolean
fakeStorage?: number
}
export const injectFakeResults = async ({
@@ -30,7 +29,6 @@ export const injectFakeResults = async ({
customResultIdPrefix,
typebotId,
isChronological,
fakeStorage,
}: CreateFakeResultsProps) => {
const resultIdPrefix = customResultIdPrefix ?? createId()
await prisma.result.createMany({
@@ -53,17 +51,13 @@ export const injectFakeResults = async ({
}),
],
})
return createAnswers({ fakeStorage, resultIdPrefix, count })
return createAnswers({ resultIdPrefix, count })
}
const createAnswers = ({
count,
resultIdPrefix,
fakeStorage,
}: { resultIdPrefix: string } & Pick<
CreateFakeResultsProps,
'fakeStorage' | 'count'
>) => {
}: { resultIdPrefix: string } & Pick<CreateFakeResultsProps, 'count'>) => {
return prisma.answer.createMany({
data: [
...Array.from(Array(count)).map((_, idx) => ({
@@ -71,7 +65,6 @@ const createAnswers = ({
content: `content${idx}`,
blockId: 'block1',
groupId: 'group1',
storageUsed: fakeStorage ? Math.round(fakeStorage / count) : null,
})),
],
})

View File

@@ -47,54 +47,6 @@ export const chatsLimit = {
[Plan.UNLIMITED]: { totalIncluded: infinity },
} as const
export const storageLimit = {
[Plan.FREE]: { totalIncluded: 0 },
[Plan.STARTER]: {
graduatedPrice: [
{ totalIncluded: 2, price: 0 },
{
totalIncluded: 3,
price: 2,
},
{
totalIncluded: 4,
price: 4,
},
{
totalIncluded: 5,
price: 6,
},
],
},
[Plan.PRO]: {
graduatedPrice: [
{ totalIncluded: 10, price: 0 },
{
totalIncluded: 15,
price: 8,
},
{
totalIncluded: 25,
price: 24,
},
{
totalIncluded: 40,
price: 49,
},
],
},
[Plan.CUSTOM]: {
totalIncluded: 2,
increaseStep: {
amount: 1,
price: 2,
},
},
[Plan.OFFERED]: { totalIncluded: 2 },
[Plan.LIFETIME]: { totalIncluded: 10 },
[Plan.UNLIMITED]: { totalIncluded: infinity },
} as const
export const seatsLimit = {
[Plan.FREE]: { totalIncluded: 1 },
[Plan.STARTER]: {
@@ -124,22 +76,6 @@ export const getChatsLimit = ({
return totalIncluded
}
export const getStorageLimit = ({
plan,
additionalStorageIndex,
customStorageLimit,
}: Pick<
Workspace,
'additionalStorageIndex' | 'plan' | 'customStorageLimit'
>) => {
if (customStorageLimit) return customStorageLimit
const totalIncluded =
plan === Plan.STARTER || plan === Plan.PRO
? storageLimit[plan].graduatedPrice[additionalStorageIndex].totalIncluded
: storageLimit[plan].totalIncluded
return totalIncluded
}
export const getSeatsLimit = ({
plan,
customSeatsLimit,
@@ -164,14 +100,12 @@ export const isSeatsLimitReached = ({
export const computePrice = (
plan: Plan,
selectedTotalChatsIndex: number,
selectedTotalStorageIndex: number,
frequency: 'monthly' | 'yearly'
) => {
if (plan !== Plan.STARTER && plan !== Plan.PRO) return
const price =
prices[plan] +
chatsLimit[plan].graduatedPrice[selectedTotalChatsIndex].price +
storageLimit[plan].graduatedPrice[selectedTotalStorageIndex].price
chatsLimit[plan].graduatedPrice[selectedTotalChatsIndex].price
return frequency === 'monthly' ? price : price - price * 0.16
}

View File

@@ -63,7 +63,6 @@ const subscriptionUpdatedEventSchema = workspaceEvent.merge(
data: z.object({
plan: z.nativeEnum(Plan),
additionalChatsIndex: z.number(),
additionalStorageIndex: z.number(),
}),
})
)
@@ -83,9 +82,7 @@ const workspaceLimitReachedEventSchema = workspaceEvent.merge(
name: z.literal('Workspace limit reached'),
data: z.object({
chatsLimit: z.number(),
storageLimit: z.number(),
totalChatsUsed: z.number(),
totalStorageUsed: z.number(),
}),
})
)

View File

@@ -4,7 +4,7 @@ import {
WorkspaceRole,
} from '@typebot.io/prisma'
import { isDefined } from '@typebot.io/lib'
import { getChatsLimit, getStorageLimit } from '@typebot.io/lib/pricing'
import { getChatsLimit } from '@typebot.io/lib/pricing'
import { promptAndSetEnvironment } from './utils'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
@@ -141,14 +141,9 @@ const sendAlertIfLimitReached = async (
if (taggedWorkspaces.includes(workspace.id) || workspace.isQuarantined)
continue
taggedWorkspaces.push(workspace.id)
const { totalChatsUsed, totalStorageUsed } = await getUsage(workspace.id)
const totalStorageUsedInGb = totalStorageUsed / 1024 / 1024 / 1024
const { totalChatsUsed } = await getUsage(workspace.id)
const chatsLimit = getChatsLimit(workspace)
const storageLimit = getStorageLimit(workspace)
if (
(chatsLimit > 0 && totalChatsUsed >= chatsLimit) ||
(storageLimit > 0 && totalStorageUsedInGb >= storageLimit)
) {
if (chatsLimit > 0 && totalChatsUsed >= chatsLimit) {
events.push(
...workspace.members
.filter((member) => member.role === WorkspaceRole.ADMIN)
@@ -160,9 +155,7 @@ const sendAlertIfLimitReached = async (
workspaceId: workspace.id,
data: {
totalChatsUsed,
totalStorageUsed: totalStorageUsedInGb,
chatsLimit,
storageLimit,
},
} satisfies TelemetryEvent)
)
@@ -186,12 +179,7 @@ const getUsage = async (workspaceId: string) => {
select: { id: true },
})
const [
totalChatsUsed,
{
_sum: { storageUsed: totalStorageUsed },
},
] = await Promise.all([
const [totalChatsUsed] = await Promise.all([
prisma.result.count({
where: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
@@ -202,20 +190,10 @@ const getUsage = async (workspaceId: string) => {
},
},
}),
prisma.answer.aggregate({
where: {
storageUsed: { gt: 0 },
result: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
},
},
_sum: { storageUsed: true },
}),
])
return {
totalChatsUsed,
totalStorageUsed: totalStorageUsed ?? 0,
}
}

View File

@@ -8,6 +8,7 @@
"sourceMap": true,
"outDir": "dist",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
"allowSyntheticDefaultImports": true,
"jsx": "preserve"
}
}