Skip to content

Commit

Permalink
🛂 Reset isQuarantined when upgrading workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Apr 23, 2023
1 parent 69e1c4f commit c6983c9
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 278 deletions.
5 changes: 1 addition & 4 deletions apps/builder/src/features/billing/api/updateSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ export const updateSubscription = authenticatedProcedure
plan,
additionalChatsIndex: additionalChats,
additionalStorageIndex: additionalStorage,
chatsLimitFirstEmailSentAt: null,
chatsLimitSecondEmailSentAt: null,
storageLimitFirstEmailSentAt: null,
storageLimitSecondEmailSentAt: null,
isQuarantined: false,
},
})

Expand Down
9 changes: 1 addition & 8 deletions apps/builder/src/pages/api/stripe/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
stripeId: session.customer as string,
additionalChatsIndex: parseInt(additionalChats),
additionalStorageIndex: parseInt(additionalStorage),
chatsLimitFirstEmailSentAt: null,
chatsLimitSecondEmailSentAt: null,
storageLimitFirstEmailSentAt: null,
storageLimitSecondEmailSentAt: null,
isQuarantined: false,
},
})

Expand Down Expand Up @@ -137,10 +134,6 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
plan: Plan.FREE,
additionalChatsIndex: 0,
additionalStorageIndex: 0,
chatsLimitFirstEmailSentAt: null,
chatsLimitSecondEmailSentAt: null,
storageLimitFirstEmailSentAt: null,
storageLimitSecondEmailSentAt: null,
customChatsLimit: null,
customStorageLimit: null,
customSeatsLimit: null,
Expand Down
119 changes: 1 addition & 118 deletions apps/viewer/src/features/blocks/inputs/fileUpload/api/getUploadUrl.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { publicProcedure } from '@/helpers/server/trpc'
import prisma from '@/lib/prisma'
import { TRPCError } from '@trpc/server'
import { getStorageLimit } from '@typebot.io/lib/pricing'
import {
FileInputBlock,
InputBlockType,
LogicBlockType,
PublicTypebot,
TypebotLinkBlock,
} from '@typebot.io/schemas'
import { byId, env, isDefined } from '@typebot.io/lib'
import { byId, isDefined } from '@typebot.io/lib'
import { z } from 'zod'
import { generatePresignedUrl } from '@typebot.io/lib/api/storage'
import {
sendAlmostReachedStorageLimitEmail,
sendReachedStorageLimitEmail,
} from '@typebot.io/emails'
import { WorkspaceRole } from '@typebot.io/prisma'

const LIMIT_EMAIL_TRIGGER_PERCENT = 0.8

export const getUploadUrl = publicProcedure
.meta({
Expand Down Expand Up @@ -58,7 +50,6 @@ export const getUploadUrl = publicProcedure
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY',
})

await checkIfStorageLimitReached(typebotId)
const publicTypebot = (await prisma.publicTypebot.findFirst({
where: { typebotId },
select: {
Expand Down Expand Up @@ -118,111 +109,3 @@ const getFileUploadBlock = async (
return fileUploadBlockFromLinkedTypebots
return null
}

const checkIfStorageLimitReached = async (
typebotId: string
): Promise<boolean> => {
const typebot = await prisma.typebot.findUnique({
where: { id: typebotId },
select: {
workspace: {
select: {
id: true,
additionalStorageIndex: true,
plan: true,
storageLimitFirstEmailSentAt: true,
storageLimitSecondEmailSentAt: true,
customStorageLimit: true,
},
},
},
})
if (!typebot?.workspace) throw new Error('Workspace not found')
const { workspace } = typebot
const {
_sum: { storageUsed: totalStorageUsed },
} = await prisma.answer.aggregate({
where: {
storageUsed: { gt: 0 },
result: {
typebot: {
workspaceId: typebot.workspace.id,
},
},
},
_sum: { storageUsed: true },
})
if (!totalStorageUsed) return false
const hasSentFirstEmail = workspace.storageLimitFirstEmailSentAt !== null
const hasSentSecondEmail = workspace.storageLimitSecondEmailSentAt !== null
const storageLimit = getStorageLimit(typebot.workspace)
if (storageLimit === -1) return false
const storageLimitBytes = storageLimit * 1024 * 1024 * 1024
if (
totalStorageUsed >= storageLimitBytes * LIMIT_EMAIL_TRIGGER_PERCENT &&
!hasSentFirstEmail &&
env('E2E_TEST') !== 'true'
)
await sendAlmostReachStorageLimitNotification({
workspaceId: workspace.id,
storageLimit,
})
if (
totalStorageUsed >= storageLimitBytes &&
!hasSentSecondEmail &&
env('E2E_TEST') !== 'true'
)
await sendReachStorageLimitNotification({
workspaceId: workspace.id,
storageLimit,
})
return totalStorageUsed >= storageLimitBytes
}

const sendAlmostReachStorageLimitNotification = async ({
workspaceId,
storageLimit,
}: {
workspaceId: string
storageLimit: number
}) => {
const members = await prisma.memberInWorkspace.findMany({
where: { role: WorkspaceRole.ADMIN, workspaceId },
include: { user: { select: { email: true } } },
})

await sendAlmostReachedStorageLimitEmail({
to: members.map((member) => member.user.email).filter(isDefined),
storageLimit,
url: `${process.env.NEXTAUTH_URL}/typebots?workspaceId=${workspaceId}`,
})

await prisma.workspace.update({
where: { id: workspaceId },
data: { storageLimitFirstEmailSentAt: new Date() },
})
}

const sendReachStorageLimitNotification = async ({
workspaceId,
storageLimit,
}: {
workspaceId: string
storageLimit: number
}) => {
const members = await prisma.memberInWorkspace.findMany({
where: { role: WorkspaceRole.ADMIN, workspaceId },
include: { user: { select: { email: true } } },
})

await sendReachedStorageLimitEmail({
to: members.map((member) => member.user.email).filter(isDefined),
storageLimit,
url: `${process.env.NEXTAUTH_URL}/typebots?workspaceId=${workspaceId}`,
})

await prisma.workspace.update({
where: { id: workspaceId },
data: { storageLimitSecondEmailSentAt: new Date() },
})
}
145 changes: 0 additions & 145 deletions apps/viewer/src/features/usage/checkChatsUsage.ts

This file was deleted.

9 changes: 6 additions & 3 deletions apps/viewer/src/pages/api/typebots/[typebotId]/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import prisma from '@/lib/prisma'
import { ResultWithAnswers } from '@typebot.io/schemas'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from '@typebot.io/lib/api'
import { checkChatsUsage } from '@/features/usage/checkChatsUsage'

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
Expand All @@ -26,8 +25,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
if (req.method === 'POST') {
const typebotId = req.query.typebotId as string
const hasReachedLimit = await checkChatsUsage({ typebotId })
if (hasReachedLimit) return res.send({ result: null, hasReachedLimit })
const typebot = await prisma.typebot.findFirst({
where: { id: typebotId },
select: { workspace: { select: { isQuarantined: true } } },
})
if (typebot?.workspace.isQuarantined)
return res.send({ result: null, hasReachedLimit: true })
const result = await prisma.result.create({
data: {
typebotId,
Expand Down

1 comment on commit c6983c9

@vercel
Copy link

@vercel vercel bot commented on c6983c9 Apr 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

facelabko.com
filmylogy.com
goldorayo.com
rabbit.cr8.ai
signup.cr8.ai
start.taxt.co
turkey.cr8.ai
vhpage.cr8.ai
vitamyway.com
am.nigerias.io
an.nigerias.io
app.yvon.earth
ar.nigerias.io
bot.enreso.org
bot.rslabs.pro
bots.bridge.ai
chat.hayuri.id
chat.uprize.hu
chatgpt.lam.ee
chicken.cr8.ai
gollum.riku.ai
gsbulletin.com
journey.cr8.ai
panther.cr7.ai
panther.cr8.ai
pay.sifuim.com
penguin.cr8.ai
talk.gocare.io
test.bot.gives
ticketfute.com
unicorn.cr8.ai
apo.nigerias.io
apr.nigerias.io
aso.nigerias.io
blackcan.cr8.ai
bot.4display.nl
bot.ageenda.com
bot.artiweb.app
bot.devitus.com
bot.jesopizz.it
bot.reeplai.com
bot.scayver.com
bot.tc-mail.com
chat.lalmon.com
chat.sureb4.com
eventhub.com.au
fitness.riku.ai
games.klujo.com
cadu.uninta.edu.br
chat.tuanpakya.com
dicanatural.online
digitalhelp.com.au
goalsettingbot.com
pant.maxbot.com.br
pantherview.cr8.ai
positivobra.com.br
rollingball.cr8.ai
survey.digienge.io
this-is-a-test.com
zap.techadviser.in
ai.digitaldaftar.in
bot.boston-voip.com
bot.cabinpromos.com
bot.carnaval.studio
bot.digitalbled.com
bot.dsignagency.com
bot.eventhub.com.au
bot.jepierre.com.br
bot.leadgenpod.site
bot.ltmidias.com.br
bot.viralsangat.com
bot.winglabs.com.br
carsalesenquiry.com
chat.marius.digital
chatbot.matthesv.de
chatbot.repplai.com
demo.botscientis.us
demo.wemakebots.xyz
forms.webisharp.com
hrbot.robomotion.io
inearephones.cr8.ai
kbsub.wpwakanda.com
limitenahora.com.br
live.botscientis.us
mentoria.omelhor.vc
nutrisamirbayde.com
order.maitempah.com
profileadscloud.com
quest.wpwakanda.com
support.wawplus.com
survey1.digienge.io
surveys.essiell.com
test.botscientis.us
test.getreview.help
test.reventepro.com
typebot.stillio.com
wordsandimagery.com
88584434.therpm.club
92109660.therpm.club
assistent.m-vogel.de

Please sign in to comment.