-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): ✨ Add routes for subscribing webhook
- Loading branch information
1 parent
5a80774
commit 68ae69f
Showing
21 changed files
with
470 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
apps/builder/components/dashboard/FolderContent/TypebotButtonOverlay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { withSentry } from '@sentry/nextjs' | ||
import prisma from 'libs/prisma' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { authenticateUser } from 'services/api/utils' | ||
import { methodNotAllowed } from 'utils' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.method === 'GET') { | ||
const user = await authenticateUser(req) | ||
if (!user) return res.status(401).json({ message: 'Not authenticated' }) | ||
const typebots = await prisma.typebot.findMany({ | ||
where: { ownerId: user.id }, | ||
select: { name: true, publishedTypebotId: true, id: true }, | ||
}) | ||
return res.send({ typebots }) | ||
} | ||
return methodNotAllowed(res) | ||
} | ||
|
||
export default withSentry(handler) |
57 changes: 57 additions & 0 deletions
57
...viewer/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/subscribeWebhook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { withSentry } from '@sentry/nextjs' | ||
import { Prisma } from 'db' | ||
import prisma from 'libs/prisma' | ||
import { IntegrationStepType, Typebot } from 'models' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { authenticateUser } from 'services/api/utils' | ||
import { methodNotAllowed } from 'utils' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.method === 'PATCH') { | ||
const user = await authenticateUser(req) | ||
if (!user) return res.status(401).json({ message: 'Not authenticated' }) | ||
const body = req.body as Record<string, string> | ||
if (!('url' in body)) | ||
return res.status(403).send({ message: 'url is missing in body' }) | ||
const { url } = body | ||
const typebotId = req.query.typebotId.toString() | ||
const stepId = req.query.stepId.toString() | ||
const typebot = (await prisma.typebot.findUnique({ | ||
where: { id_ownerId: { id: typebotId, ownerId: user.id } }, | ||
})) as Typebot | undefined | ||
if (!typebot) return res.status(400).send({ message: 'Typebot not found' }) | ||
try { | ||
const updatedTypebot = addUrlToWebhookStep(url, typebot, stepId) | ||
await prisma.typebot.update({ | ||
where: { id_ownerId: { id: typebotId, ownerId: user.id } }, | ||
data: { blocks: updatedTypebot.blocks as Prisma.JsonArray }, | ||
}) | ||
return res.send({ message: 'success' }) | ||
} catch (err) { | ||
return res | ||
.status(400) | ||
.send({ message: "stepId doesn't point to a Webhook step" }) | ||
} | ||
} | ||
return methodNotAllowed(res) | ||
} | ||
|
||
const addUrlToWebhookStep = ( | ||
url: string, | ||
typebot: Typebot, | ||
stepId: string | ||
): Typebot => ({ | ||
...typebot, | ||
blocks: typebot.blocks.map((b) => ({ | ||
...b, | ||
steps: b.steps.map((s) => { | ||
if (s.id === stepId) { | ||
if (s.type !== IntegrationStepType.WEBHOOK) throw new Error() | ||
return { ...s, webhook: { ...s.webhook, url } } | ||
} | ||
return s | ||
}), | ||
})), | ||
}) | ||
|
||
export default withSentry(handler) |
55 changes: 55 additions & 0 deletions
55
...ewer/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/unsubscribeWebhook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { withSentry } from '@sentry/nextjs' | ||
import { Prisma } from 'db' | ||
import prisma from 'libs/prisma' | ||
import { IntegrationStepType, Typebot } from 'models' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { authenticateUser } from 'services/api/utils' | ||
import { omit } from 'services/utils' | ||
import { methodNotAllowed } from 'utils' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.method === 'DELETE') { | ||
const user = await authenticateUser(req) | ||
if (!user) return res.status(401).json({ message: 'Not authenticated' }) | ||
const typebotId = req.query.typebotId.toString() | ||
const stepId = req.query.stepId.toString() | ||
const typebot = (await prisma.typebot.findUnique({ | ||
where: { id_ownerId: { id: typebotId, ownerId: user.id } }, | ||
})) as Typebot | undefined | ||
if (!typebot) return res.status(400).send({ message: 'Typebot not found' }) | ||
try { | ||
const updatedTypebot = removeUrlFromWebhookStep(typebot, stepId) | ||
await prisma.typebot.update({ | ||
where: { id_ownerId: { id: typebotId, ownerId: user.id } }, | ||
data: { | ||
blocks: updatedTypebot.blocks as Prisma.JsonArray, | ||
}, | ||
}) | ||
return res.send({ message: 'success' }) | ||
} catch (err) { | ||
return res | ||
.status(400) | ||
.send({ message: "stepId doesn't point to a Webhook step" }) | ||
} | ||
} | ||
return methodNotAllowed(res) | ||
} | ||
|
||
const removeUrlFromWebhookStep = ( | ||
typebot: Typebot, | ||
stepId: string | ||
): Typebot => ({ | ||
...typebot, | ||
blocks: typebot.blocks.map((b) => ({ | ||
...b, | ||
steps: b.steps.map((s) => { | ||
if (s.id === stepId) { | ||
if (s.type !== IntegrationStepType.WEBHOOK) throw new Error() | ||
return { ...s, webhook: omit(s.webhook, 'url') } | ||
} | ||
return s | ||
}), | ||
})), | ||
}) | ||
|
||
export default withSentry(handler) |
37 changes: 37 additions & 0 deletions
37
apps/viewer/pages/api/typebots/[typebotId]/webhookSteps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { withSentry } from '@sentry/nextjs' | ||
import prisma from 'libs/prisma' | ||
import { Block, IntegrationStepType } from 'models' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { authenticateUser } from 'services/api/utils' | ||
import { methodNotAllowed } from 'utils' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.method === 'GET') { | ||
const user = await authenticateUser(req) | ||
if (!user) return res.status(401).json({ message: 'Not authenticated' }) | ||
const typebotId = req.query.typebotId.toString() | ||
const typebot = await prisma.typebot.findUnique({ | ||
where: { id_ownerId: { id: typebotId, ownerId: user.id } }, | ||
select: { blocks: true }, | ||
}) | ||
const emptyWebhookSteps = (typebot?.blocks as Block[]).reduce< | ||
{ blockId: string; stepId: string; name: string }[] | ||
>((emptyWebhookSteps, block) => { | ||
const steps = block.steps.filter( | ||
(step) => step.type === IntegrationStepType.WEBHOOK && !step.webhook.url | ||
) | ||
return [ | ||
...emptyWebhookSteps, | ||
...steps.map((s) => ({ | ||
blockId: s.blockId, | ||
stepId: s.id, | ||
name: `${block.title} > ${s.id}`, | ||
})), | ||
] | ||
}, []) | ||
return res.send({ steps: emptyWebhookSteps }) | ||
} | ||
return methodNotAllowed(res) | ||
} | ||
|
||
export default withSentry(handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { withSentry } from '@sentry/nextjs' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { authenticateUser } from 'services/api/utils' | ||
import { isNotDefined, methodNotAllowed } from 'utils' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.method === 'GET') { | ||
const user = await authenticateUser(req) | ||
if (isNotDefined(user)) | ||
return res.status(404).send({ message: 'User not found' }) | ||
return res.send({ id: user.id }) | ||
} | ||
return methodNotAllowed(res) | ||
} | ||
|
||
export default withSentry(handler) |
Oops, something went wrong.
68ae69f
There was a problem hiding this comment.
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
demo.wemakebots.xyz
chat.hayuri.id
bot.pinpointinteractive.com
viewer.typebot.io
criar.somaperuzzo.com
bot.adventureconsulting.hu
viewer-v2-typebot-io.vercel.app
typebot-viewer.vercel.app
bot.theiofundation.org
viewer-v2-git-main-typebot-io.vercel.app
68ae69f
There was a problem hiding this comment.
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
app.typebot.io
builder-v2-typebot-io.vercel.app