Skip to content

Commit

Permalink
🐛 Remove publicId and customDomain duplication on imported bots
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Aug 17, 2023
1 parent 906845b commit 304dfe2
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 69 deletions.
63 changes: 29 additions & 34 deletions apps/builder/src/features/billing/api/getUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import prisma from '@/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWorkspaceFobidden'

export const getUsage = authenticatedProcedure
.meta({
Expand All @@ -25,51 +26,45 @@ export const getUsage = authenticatedProcedure
const workspace = await prisma.workspace.findFirst({
where: {
id: workspaceId,
members: { some: { userId: user.id } },
},
select: {
members: {
select: {
userId: true,
},
},
typebots: {
select: { id: true, results: { select: { id: true } } },
},
},
})
if (!workspace)
if (!workspace || isReadWorkspaceFobidden(workspace, user))
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Workspace not found',
})

const now = new Date()
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
const [
totalChatsUsed,
{
_sum: { storageUsed: totalStorageUsed },
const totalChatsUsed = await prisma.result.count({
where: {
typebotId: { in: workspace.typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
},
},
] = await prisma.$transaction(async (tx) => {
const typebots = await tx.typebot.findMany({
where: {
workspace: {
id: workspaceId,
},
})
const {
_sum: { storageUsed: totalStorageUsed },
} = await prisma.answer.aggregate({
where: {
storageUsed: { gt: 0 },
result: {
typebotId: { in: workspace.typebots.map((typebot) => typebot.id) },
},
})

return Promise.all([
prisma.result.count({
where: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
},
},
}),
prisma.answer.aggregate({
where: {
storageUsed: { gt: 0 },
result: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
},
},
_sum: { storageUsed: true },
}),
])
},
_sum: { storageUsed: true },
})

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const createCredentials = authenticatedProcedure
},
select: { id: true, members: true },
})
if (!workspace || (await isWriteWorkspaceForbidden(workspace, user)))
if (!workspace || isWriteWorkspaceForbidden(workspace, user))
throw new TRPCError({ code: 'NOT_FOUND', message: 'Workspace not found' })

const { encryptedData, iv } = await encrypt(credentials.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const deleteCredentials = authenticatedProcedure
},
select: { id: true, members: true },
})
if (!workspace || (await isWriteWorkspaceForbidden(workspace, user)))
if (!workspace || isWriteWorkspaceForbidden(workspace, user))
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Workspace not found',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const listCredentials = authenticatedProcedure
},
},
})
if (!workspace || (await isReadWorkspaceFobidden(workspace, user)))
if (!workspace || isReadWorkspaceFobidden(workspace, user))
throw new TRPCError({ code: 'NOT_FOUND', message: 'Workspace not found' })

return { credentials: workspace.credentials }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useScopedI18n } from '@/locales'
import { TypebotInDashboard } from '@/features/dashboard/types'
import { isMobile } from '@/helpers/isMobile'
import { trpc, trpcVanilla } from '@/lib/trpc'
import { duplicateName } from '@/features/typebot/helpers/duplicateName'

type Props = {
typebot: TypebotInDashboard
Expand Down Expand Up @@ -221,10 +222,3 @@ export const TypebotButton = ({
</Button>
)
}

const duplicateName = (name: string | `${string} (${number})`) => {
const match = name.match(/^(.*) \((\d+)\)$/)
if (!match) return `${name} (1)`
const [, nameWithoutNumber, number] = match
return `${nameWithoutNumber} (${Number(number) + 1})`
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ export const CreateNewTypebotButtons = () => {
mutate({
workspaceId: workspace.id,
typebot: {
...(typebot ? { ...typebot } : {}),
...(typebot
? {
...typebot,
publicId: undefined,
customDomain: undefined,
}
: {}),
folderId,
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ export const ImportTypebotFromFileButton = ({
const file = e.target.files[0]
const fileContent = await readFile(file)
try {
const typebot = parseInvalidTypebot(JSON.parse(fileContent))
typebotSchema
const typebot = typebotSchema
.omit({
createdAt: true,
updatedAt: true,
})
.parse(typebot)
.parse(parseInvalidTypebot(JSON.parse(fileContent)))
onNewTypebot(typebot as Typebot)
} catch (err) {
console.error(err)
Expand Down
6 changes: 6 additions & 0 deletions apps/builder/src/features/typebot/helpers/duplicateName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const duplicateName = (name: string | `${string} (${number})`) => {
const match = name.match(/^(.*) \((\d+)\)$/)
if (!match) return `${name} (1)`
const [, nameWithoutNumber, number] = match
return `${nameWithoutNumber} (${Number(number) + 1})`
}
2 changes: 1 addition & 1 deletion apps/builder/src/features/workspace/api/deleteWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const deleteWorkspace = authenticatedProcedure
include: { members: true },
})

if (!workspace || (await isAdminWriteWorkspaceForbidden(workspace, user)))
if (!workspace || isAdminWriteWorkspaceForbidden(workspace, user))
throw new TRPCError({ code: 'NOT_FOUND', message: 'No workspaces found' })

await prisma.workspace.deleteMany({
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/src/features/workspace/api/getWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const getWorkspace = authenticatedProcedure
include: { members: true },
})

if (!workspace || (await isReadWorkspaceFobidden(workspace, user)))
if (!workspace || isReadWorkspaceFobidden(workspace, user))
throw new TRPCError({ code: 'NOT_FOUND', message: 'No workspaces found' })

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const listInvitationsInWorkspace = authenticatedProcedure
include: { members: true, invitations: true },
})

if (!workspace || (await isReadWorkspaceFobidden(workspace, user)))
if (!workspace || isReadWorkspaceFobidden(workspace, user))
throw new TRPCError({ code: 'NOT_FOUND', message: 'No workspaces found' })

return { invitations: workspace.invitations }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const listMembersInWorkspace = authenticatedProcedure
},
})

if (!workspace || (await isReadWorkspaceFobidden(workspace, user)))
if (!workspace || isReadWorkspaceFobidden(workspace, user))
throw new TRPCError({ code: 'NOT_FOUND', message: 'No workspaces found' })

return {
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/src/features/workspace/api/updateWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const updateWorkspace = authenticatedProcedure
if (!workspace)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Workspace not found' })

if (await isAdminWriteWorkspaceForbidden(workspace, user))
if (isAdminWriteWorkspaceForbidden(workspace, user))
throw new TRPCError({
code: 'FORBIDDEN',
message: 'You are not allowed to update this workspace',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MemberInWorkspace, User } from '@typebot.io/prisma'

export const isAdminWriteWorkspaceForbidden = async (
export const isAdminWriteWorkspaceForbidden = (
workspace: {
members: MemberInWorkspace[]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MemberInWorkspace, User } from '@typebot.io/prisma'

export const isReadWorkspaceFobidden = async (
export const isReadWorkspaceFobidden = (
workspace: {
members: MemberInWorkspace[]
members: Pick<MemberInWorkspace, 'userId'>[]
},
user: Pick<User, 'email' | 'id'>
) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MemberInWorkspace, User } from '@typebot.io/prisma'

export const isWriteWorkspaceForbidden = async (
export const isWriteWorkspaceForbidden = (
workspace: {
members: MemberInWorkspace[]
},
Expand Down
15 changes: 3 additions & 12 deletions apps/docs/openapi/builder/_spec_.json
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
}
}
},
"/linkedTypebots": {
"/typebots/{typebotId}/linkedTypebots": {
"get": {
"operationId": "getLinkedTypebots",
"summary": "Get linked typebots",
Expand All @@ -440,21 +440,12 @@
],
"parameters": [
{
"name": "workspaceId",
"in": "query",
"name": "typebotId",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "typebotIds",
"in": "query",
"required": true,
"schema": {
"type": "string"
},
"description": "Comma separated list of typebot ids"
}
],
"responses": {
Expand Down

4 comments on commit 304dfe2

@vercel
Copy link

@vercel vercel bot commented on 304dfe2 Aug 17, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 304dfe2 Aug 17, 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

psmix.online
receita.info
rhino.cr8.ai
sheep.cr8.ai
snake.cr8.ai
svhm.mprs.in
tiger.cr8.ai
video.cr8.ai
webwhats.pro
yoda.riku.ai
zebra.cr8.ai
bemestar.club
bot.krdfy.com
ov1.wpwakanda.com
ov2.wpwakanda.com
ov3.wpwakanda.com
pcb.drapamela.com
redeemchatgpt.com
softwarelucra.com
support.triplo.ai
viewer.typebot.io
welcome.triplo.ai
www.thegymgame.it
zeropendencia.com
1988.bouclidom.com
a.onewebcenter.com
amancarseat.online
amostra-safe.click
andreimayer.com.br
bebesemcolicas.com
bot.innovacion.fun
bot.lucide.contact
bot.neferlopez.com
bot.photonative.de
bot.samplehunt.com
bot.sinalcerto.com
bot.wphelpchat.com
bots.robomotion.io
cadu.uninta.edu.br
chat.hand-made.one
chat.tuanpakya.com
chat.webisharp.com
chatbotforthat.com
descobrindotudo.me
dicanatural.online
digitalhelp.com.au
draraquelnutri.com
drcarlosyoshi.site
goalsettingbot.com
leads.gecoelho.com
noticiasnet.online
novoappespiao.site
omarcodosanjos.com
pant.maxbot.com.br
pantherview.cr8.ai
positivobra.com.br
rollingball.cr8.ai
speciallife.com.br
sub.yolozeeeer.com
survey.digienge.io
tribe.ezbooking.ai
w.onewebcenter.com
viewer-v2-typebot-io.vercel.app
mdb.assessoria.jbatista.progenbr.com
mdb.assessoria.mauricio.progenbr.com
mdb.evento.autocadastro.progenbr.com
form.shopmercedesbenzsouthorlando.com
mdb.evento.equipeinterna.progenbr.com
bot.studiotecnicoimmobiliaremerelli.it
mdb.assessoria.boaventura.progenbr.com
mdb.assessoria.jtrebesqui.progenbr.com
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
gabinete.baleia.formulario.progenbr.com
mdb.assessoria.carreirinha.progenbr.com
chrome-os-inquiry-system.itschromeos.com
mdb.assessoria.paulomarques.progenbr.com
viewer-v2-git-main-typebot-io.vercel.app
main-menu-for-itschromeos.itschromeos.com
mdb.assessoria.qrcode.ademir.progenbr.com
mdb.assessoria.qrcode.arthur.progenbr.com
mdb.assessoria.qrcode.danilo.progenbr.com
mdb.assessoria.qrcode.marcao.progenbr.com
mdb.assessoria.qrcode.marcio.progenbr.com
mdb.assessoria.qrcode.aloisio.progenbr.com
mdb.assessoria.qrcode.girotto.progenbr.com
mdb.assessoria.qrcode.marinho.progenbr.com
mdb.assessoria.qrcode.rodrigo.progenbr.com
mdb.assessoria.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.desideri.progenbr.com
mdb.assessoria.qrcode.fernanda.progenbr.com
mdb.assessoria.qrcode.jbatista.progenbr.com
mdb.assessoria.qrcode.mauricio.progenbr.com
mdb.assessoria.fernanda.regional.progenbr.com
mdb.assessoria.qrcode.boaventura.progenbr.com
mdb.assessoria.qrcode.jtrebesqui.progenbr.com
mdb.assessoria.qrcode.carreirinha.progenbr.com
mdb.assessoria.qrcode.paulomarques.progenbr.com
mdb.assessoria.qrcode.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.fernanda.regional.progenbr.com

@vercel
Copy link

@vercel vercel bot commented on 304dfe2 Aug 17, 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:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 304dfe2 Aug 17, 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:

docs – ./apps/docs

docs-git-main-typebot-io.vercel.app
docs-typebot-io.vercel.app
docs.typebot.io

Please sign in to comment.