Skip to content

Commit

Permalink
🗃️ Write faster prisma queries
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Dec 8, 2022
1 parent fe8a531 commit 7eac2c7
Show file tree
Hide file tree
Showing 23 changed files with 235 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getLinkedTypebots } from '@/features/blocks/logic/typebotLink/api'
import prisma from '@/lib/prisma'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { Typebot, Webhook } from 'models'
Expand Down Expand Up @@ -40,7 +40,7 @@ export const getResultExampleProcedure = authenticatedProcedure
)
.query(async ({ input: { typebotId, blockId }, ctx: { user } }) => {
const typebot = (await prisma.typebot.findFirst({
where: canReadTypebot(typebotId, user),
where: canReadTypebots(typebotId, user),
select: {
groups: true,
edges: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { Group, Typebot, Webhook, WebhookBlock } from 'models'
Expand Down Expand Up @@ -36,7 +36,7 @@ export const listWebhookBlocksProcedure = authenticatedProcedure
)
.query(async ({ input: { typebotId }, ctx: { user } }) => {
const typebot = (await prisma.typebot.findFirst({
where: canReadTypebot(typebotId, user),
where: canReadTypebots(typebotId, user),
select: {
groups: true,
webhooks: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canWriteTypebot } from '@/utils/api/dbRules'
import { canWriteTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { Typebot, Webhook, WebhookBlock } from 'models'
Expand Down Expand Up @@ -31,7 +31,7 @@ export const subscribeWebhookProcedure = authenticatedProcedure
)
.query(async ({ input: { typebotId, blockId, url }, ctx: { user } }) => {
const typebot = (await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
select: {
groups: true,
webhooks: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canWriteTypebot } from '@/utils/api/dbRules'
import { canWriteTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { Typebot, Webhook, WebhookBlock } from 'models'
Expand Down Expand Up @@ -30,7 +30,7 @@ export const unsubscribeWebhookProcedure = authenticatedProcedure
)
.query(async ({ input: { typebotId, blockId }, ctx: { user } }) => {
const typebot = (await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
select: {
groups: true,
webhooks: true,
Expand Down
4 changes: 2 additions & 2 deletions apps/builder/src/features/results/api/archiveResults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canWriteTypebot } from '@/utils/api/dbRules'
import { canWriteTypebots } from '@/utils/api/dbRules'
import { deleteFiles } from '@/utils/api/storage'
import { User, Prisma } from 'db'
import { InputBlockType, Typebot } from 'models'
Expand All @@ -14,7 +14,7 @@ export const archiveResults = async ({
resultsFilter?: Prisma.ResultWhereInput
}) => {
const typebot = await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
select: { groups: true },
})
if (!typebot) return { success: false }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { canWriteTypebot } from '@/utils/api/dbRules'
import { canWriteTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
Expand Down Expand Up @@ -34,7 +34,7 @@ export const deleteResultsProcedure = authenticatedProcedure
user,
resultsFilter: {
id: (idsArray?.length ?? 0) > 0 ? { in: idsArray } : undefined,
typebot: canWriteTypebot(typebotId, user),
typebot: canWriteTypebots(typebotId, user),
},
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { logSchema } from 'models'
import { z } from 'zod'
Expand All @@ -22,9 +22,14 @@ export const getResultLogsProcedure = authenticatedProcedure
)
.output(z.object({ logs: z.array(logSchema) }))
.query(async ({ input: { typebotId, resultId }, ctx: { user } }) => {
const typebot = await prisma.typebot.findFirst({
where: canReadTypebots(typebotId, user),
select: { id: true },
})
if (!typebot) throw new Error('Typebot not found')
const logs = await prisma.log.findMany({
where: {
result: { id: resultId, typebot: canReadTypebot(typebotId, user) },
result: { id: resultId, typebotId: typebot.id },
},
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { ResultWithAnswers, resultWithAnswersSchema } from 'models'
Expand Down Expand Up @@ -38,11 +38,17 @@ export const getResultsProcedure = authenticatedProcedure
message: 'limit must be between 1 and 200',
})
const { cursor } = input
const typebot = await prisma.typebot.findFirst({
where: canReadTypebots(input.typebotId, user),
select: { id: true },
})
if (!typebot)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
const results = (await prisma.result.findMany({
take: limit + 1,
cursor: cursor ? { id: cursor } : undefined,
where: {
typebot: canReadTypebot(input.typebotId, user),
typebotId: typebot.id,
answers: { some: {} },
},
orderBy: {
Expand Down
4 changes: 2 additions & 2 deletions apps/builder/src/pages/api/publicTypebots/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { InputBlockType, PublicTypebot } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { canPublishFileInput, canWriteTypebot } from '@/utils/api/dbRules'
import { canPublishFileInput, canWriteTypebots } from '@/utils/api/dbRules'
import { getAuthenticatedUser } from '@/features/auth/api'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils/api'

Expand Down Expand Up @@ -39,7 +39,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
await prisma.publicTypebot.deleteMany({
where: {
id: publishedTypebotId,
typebot: canWriteTypebot(typebotId, user),
typebot: canWriteTypebots(typebotId, user),
},
})
return res.send({ success: true })
Expand Down
15 changes: 8 additions & 7 deletions apps/builder/src/pages/api/typebots/[typebotId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { withSentry } from '@sentry/nextjs'
import { CollaborationType } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canReadTypebot, canWriteTypebot } from '@/utils/api/dbRules'
import { canReadTypebots, canWriteTypebots } from '@/utils/api/dbRules'
import { methodNotAllowed, notAuthenticated } from 'utils/api'
import { getAuthenticatedUser } from '@/features/auth/api'
import { archiveResults } from '@/features/results/api'
Expand All @@ -15,7 +15,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
const typebot = await prisma.typebot.findFirst({
where: {
...canReadTypebot(typebotId, user),
...canReadTypebots(typebotId, user),
isArchived: { not: true },
},
include: {
Expand Down Expand Up @@ -46,26 +46,27 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
})
if (!success) return res.status(500).send({ success: false })
await prisma.publicTypebot.deleteMany({
where: { typebot: canWriteTypebot(typebotId, user) },
where: { typebot: canWriteTypebots(typebotId, user) },
})
const typebots = await prisma.typebot.updateMany({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
data: { isArchived: true },
})
return res.send({ typebots })
}
if (req.method === 'PUT') {
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const existingTypebot = await prisma.typebot.findFirst({
where: canReadTypebot(typebotId, user),
where: canReadTypebots(typebotId, user),
select: { updatedAt: true },
})
if (
existingTypebot &&
existingTypebot?.updatedAt > new Date(data.updatedAt)
)
return res.send({ message: 'Found newer version of typebot in database' })
const typebots = await prisma.typebot.updateMany({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
data: {
...data,
theme: data.theme ?? undefined,
Expand All @@ -78,7 +79,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'PATCH') {
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const typebots = await prisma.typebot.updateMany({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
data,
})
return res.send({ typebots })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed, notAuthenticated } from 'utils/api'
import { withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from '@/features/auth/api'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'GET') {
const typebotId = req.query.typebotId as string
const typebot = await prisma.typebot.findFirst({
where: canReadTypebot(typebotId, user),
include: { publishedTypebot: true },
where: canReadTypebots(typebotId, user),
select: { publishedTypebot: true },
})
const publishedTypebot =
typebot?.publishedTypebot as unknown as PublicTypebot
Expand Down
18 changes: 11 additions & 7 deletions apps/builder/src/pages/api/typebots/[typebotId]/analytics/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { Stats } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { getAuthenticatedUser } from '@/features/auth/api'
import { methodNotAllowed, notAuthenticated } from 'utils/api'

Expand All @@ -12,23 +12,27 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
const typebotId = req.query.typebotId as string

const typebot = await prisma.typebot.findFirst({
where: canReadTypebots(typebotId, user),
select: { id: true },
})

if (!typebot) return res.status(404).send({ message: 'Typebot not found' })

const totalViews = await prisma.result.count({
where: {
typebotId,
typebot: canReadTypebot(typebotId, user),
typebotId: typebot.id,
},
})
const totalStarts = await prisma.result.count({
where: {
typebotId,
typebot: canReadTypebot(typebotId, user),
typebotId: typebot.id,
answers: { some: {} },
},
})
const totalCompleted = await prisma.result.count({
where: {
typebotId,
typebot: canReadTypebot(typebotId, user),
typebotId: typebot.id,
isCompleted: true,
},
})
Expand Down
5 changes: 3 additions & 2 deletions apps/builder/src/pages/api/typebots/[typebotId]/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { getAuthenticatedUser } from '@/features/auth/api'
import { methodNotAllowed, notAuthenticated, notFound } from 'utils/api'

Expand All @@ -11,7 +11,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
const typebotId = req.query.typebotId as string
const typebot = await prisma.typebot.findFirst({
where: canReadTypebot(typebotId, user),
where: canReadTypebots(typebotId, user),
select: { groups: true },
})
if (!typebot) return notFound(res)
return res.send({ groups: typebot.groups })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { getAuthenticatedUser } from '@/features/auth/api'
import { methodNotAllowed, notAuthenticated } from 'utils/api'

Expand All @@ -11,7 +11,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const typebotId = req.query.typebotId as string
if (req.method === 'GET') {
const collaborators = await prisma.collaboratorsOnTypebots.findMany({
where: { typebot: canReadTypebot(typebotId, user) },
where: { typebot: canReadTypebots(typebotId, user) },
include: { user: { select: { name: true, image: true, email: true } } },
})
return res.send({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { withSentry } from '@sentry/nextjs'
import { CollaborationType, WorkspaceRole } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canReadTypebot, canWriteTypebot } from '@/utils/api/dbRules'
import { canReadTypebots, canWriteTypebots } from '@/utils/api/dbRules'
import {
badRequest,
forbidden,
Expand All @@ -19,15 +19,15 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const typebotId = req.query.typebotId as string
if (req.method === 'GET') {
const invitations = await prisma.invitation.findMany({
where: { typebotId, typebot: canReadTypebot(typebotId, user) },
where: { typebotId, typebot: canReadTypebots(typebotId, user) },
})
return res.send({
invitations,
})
}
if (req.method === 'POST') {
const typebot = await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
include: { workspace: { select: { name: true } } },
})
if (!typebot || !typebot.workspaceId) return forbidden(res)
Expand Down
5 changes: 3 additions & 2 deletions apps/builder/src/pages/api/typebots/[typebotId]/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { defaultWebhookAttributes } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { canWriteTypebot } from '@/utils/api/dbRules'
import { canWriteTypebots } from '@/utils/api/dbRules'
import { getAuthenticatedUser } from '@/features/auth/api'
import { forbidden, methodNotAllowed, notAuthenticated } from 'utils/api'

Expand All @@ -12,7 +12,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
const typebotId = req.query.typebotId as string
const typebot = await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
select: { id: true },
})
if (!typebot) return forbidden(res)
const webhook = await prisma.webhook.create({
Expand Down
Loading

5 comments on commit 7eac2c7

@vercel
Copy link

@vercel vercel bot commented on 7eac2c7 Dec 8, 2022

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-alpha – ./apps/viewer

ns8.vn
yobot.me
247987.com
8jours.top
bot.aws.bj
bot.bbc.bj
finplex.be
sat.cr8.ai
bot.aipr.kr
minipost.uk
bt.id8rs.com
bot.krdfy.com
goldorayo.com
signup.cr8.ai
vhpage.cr8.ai
am.nigerias.io
an.nigerias.io
ar.nigerias.io
bot.enreso.org
bot.lalmon.com
ticketfute.com
apo.nigerias.io
apr.nigerias.io
aso.nigerias.io
bot.ageenda.com
bot.artiweb.app
bot.devitus.com
bot.tc-mail.com
chat.sureb4.com
eventhub.com.au
games.klujo.com
sakuranembro.it
typebot.aloe.do
bot.piccinato.co
bot.sv-energy.it
botc.ceox.com.br
clo.closeer.work
faqs.nigerias.io
feedback.ofx.one
form.syncwin.com
kw.wpwakanda.com
myrentalhost.com
stan.vselise.com
start.taxtree.io
typebot.aloe.bot
voicehelp.cr8.ai
app.chatforms.net
bot.agfunnel.tech
bot.hostnation.de
bot.maitempah.com
bot.phuonghub.com
bot.reviewzer.com
cares.urlabout.me
fmm.wpwakanda.com
gentleman-shop.fr
k1.kandabrand.com
lb.ticketfute.com
ov1.wpwakanda.com
ov2.wpwakanda.com
ov3.wpwakanda.com
1988.bouclidom.com
andreimayer.com.br
bot.megafox.com.br
bot.neferlopez.com
bots.robomotion.io
cadu.uninta.edu.br
dicanatural.online
goalsettingbot.com
positivobra.com.br
survey.digienge.io
this-is-a-test.com
zap.techadviser.in
bot.digitalbled.com
bot.eventhub.com.au
bot.jepierre.com.br
bot.winglabs.com.br
carsalesenquiry.com
demo.botscientis.us
forms.webisharp.com
kbsub.wpwakanda.com
live.botscientis.us
mentoria.omelhor.vc
nutrisamirbayde.com
order.maitempah.com
quest.wpwakanda.com
survey1.digienge.io
test.botscientis.us
typebot.stillio.com
wordsandimagery.com
bium.gratirabbit.com
bot.ansuraniphone.my
bot.cotemeuplano.com
chat.hayurihijab.com
chatbee.agfunnel.com
click.sevenoways.com
connect.growthguy.in
link.cascadigital.com.br
reward.onlinebotdemo.xyz
type.opaulovieira.com.br
aibot.angrybranding.co.uk

@vercel
Copy link

@vercel vercel bot commented on 7eac2c7 Dec 8, 2022

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 7eac2c7 Dec 8, 2022

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

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

@vercel
Copy link

@vercel vercel bot commented on 7eac2c7 Dec 8, 2022

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 7eac2c7 Dec 8, 2022

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.