2
0

fix: 🚑️ Webhook and instant updates

This commit is contained in:
Baptiste Arnaud
2022-02-22 10:16:35 +01:00
parent 642a42779b
commit d49461cde6
36 changed files with 317 additions and 204 deletions

View File

@ -12,7 +12,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(401).json({ message: 'Not authenticated' })
const user = session.user as User
const { code } = JSON.parse(req.body)
const { code } =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const coupon = await prisma.coupon.findFirst({
where: { code, dateRedeemed: null },
})

View File

@ -26,7 +26,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.send({ folders })
}
if (req.method === 'POST') {
const data = JSON.parse(req.body) as Pick<DashboardFolder, 'parentFolderId'>
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Pick<DashboardFolder, 'parentFolderId'>
const folder = await prisma.dashboardFolder.create({
data: { ...data, ownerId: user.id, name: 'New folder' },
})

View File

@ -26,7 +26,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.send({ folders })
}
if (req.method === 'PATCH') {
const data = JSON.parse(req.body) as Partial<DashboardFolder>
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Partial<DashboardFolder>
const folders = await prisma.dashboardFolder.update({
where: { id_ownerId: { id, ownerId: user.id } },
data,

View File

@ -5,8 +5,9 @@ import { createTransport } from 'nodemailer'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
const { from, port, isTlsEnabled, username, password, host, to } =
JSON.parse(req.body) as SmtpCredentialsData & { to: string }
const { from, port, isTlsEnabled, username, password, host, to } = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as SmtpCredentialsData & { to: string }
const transporter = createTransport({
host,
port,

View File

@ -12,7 +12,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
if (req.method === 'POST') {
const data = JSON.parse(req.body)
const data =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const typebot = await prisma.publicTypebot.create({
data: { ...data },
})

View File

@ -12,7 +12,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const id = req.query.id.toString()
if (req.method === 'PUT') {
const data = JSON.parse(req.body)
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const typebots = await prisma.publicTypebot.update({
where: { id },
data,

View File

@ -10,7 +10,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2020-08-27',
})
const { email, currency } = JSON.parse(req.body)
const { email, currency } =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const session = await stripe.checkout.sessions.create({
success_url: `${req.headers.origin}/typebots?stripe=success`,
cancel_url: `${req.headers.origin}/typebots?stripe=cancel`,

View File

@ -26,7 +26,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.send({ typebots })
}
if (req.method === 'POST') {
const data = JSON.parse(req.body)
const data =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const typebot = await prisma.typebot.create({
data:
'blocks' in data

View File

@ -41,7 +41,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.send({ typebots })
}
if (req.method === 'PUT') {
const data = JSON.parse(req.body)
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const typebots = await prisma.typebot.update({
where: { id_ownerId: { id: typebotId, ownerId: user.id } },
data: {
@ -53,7 +53,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.send({ typebots })
}
if (req.method === 'PATCH') {
const data = JSON.parse(req.body)
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const typebots = await prisma.typebot.update({
where: { id_ownerId: { id: typebotId, ownerId: user.id } },
data,

View File

@ -12,7 +12,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const id = req.query.id.toString()
if (req.method === 'PUT') {
const data = JSON.parse(req.body)
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const typebots = await prisma.user.update({
where: { id },
data,

View File

@ -23,7 +23,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.send({ credentials })
}
if (req.method === 'POST') {
const data = JSON.parse(req.body) as Omit<Credentials, 'ownerId'>
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Omit<Credentials, 'ownerId'>
const { encryptedData, iv } = encrypt(data.data)
const credentials = await prisma.credentials.create({
data: {

View File

@ -22,7 +22,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.send({ customDomains })
}
if (req.method === 'POST') {
const data = JSON.parse(req.body) as Omit<CustomDomain, 'ownerId'>
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Omit<CustomDomain, 'ownerId'>
try {
await createDomainOnVercel(data.name)
} catch (err) {