2
0

feat: ️ Remove branding automatically

This commit is contained in:
Baptiste Arnaud
2022-06-20 11:28:18 +02:00
parent f676166a17
commit 3e7b34c721
2 changed files with 17 additions and 3 deletions

View File

@ -1,10 +1,10 @@
import { withSentry } from '@sentry/nextjs' import { withSentry } from '@sentry/nextjs'
import { Prisma, WorkspaceRole } from 'db' import { Plan, Prisma, WorkspaceRole } from 'db'
import prisma from 'libs/prisma' import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils' import { getAuthenticatedUser } from 'services/api/utils'
import { parseNewTypebot } from 'services/typebots/typebots' import { parseNewTypebot } from 'services/typebots/typebots'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils' import { badRequest, methodNotAllowed, notAuthenticated, notFound } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => { const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req) const user = await getAuthenticatedUser(req)
@ -74,6 +74,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.send({ typebots }) return res.send({ typebots })
} }
if (req.method === 'POST') { if (req.method === 'POST') {
const workspace = await prisma.workspace.findFirst({
where: { id: req.body.workspaceId },
select: { plan: true },
})
if (!workspace) return notFound(res, "Couldn't find workspace")
const data = const data =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const typebot = await prisma.typebot.create({ const typebot = await prisma.typebot.create({
@ -82,6 +87,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
? data ? data
: (parseNewTypebot({ : (parseNewTypebot({
ownerAvatarUrl: user.image, ownerAvatarUrl: user.image,
isBrandingEnabled: workspace.plan === Plan.FREE,
...data, ...data,
}) as Prisma.TypebotUncheckedCreateInput), }) as Prisma.TypebotUncheckedCreateInput),
}) })

View File

@ -400,11 +400,13 @@ export const parseNewTypebot = ({
name, name,
ownerAvatarUrl, ownerAvatarUrl,
workspaceId, workspaceId,
isBrandingEnabled = true,
}: { }: {
folderId: string | null folderId: string | null
workspaceId: string workspaceId: string
name: string name: string
ownerAvatarUrl?: string ownerAvatarUrl?: string
isBrandingEnabled?: boolean
}): Omit< }): Omit<
Typebot, Typebot,
| 'createdAt' | 'createdAt'
@ -443,7 +445,13 @@ export const parseNewTypebot = ({
hostAvatar: { isEnabled: true, url: ownerAvatarUrl }, hostAvatar: { isEnabled: true, url: ownerAvatarUrl },
}, },
}, },
settings: defaultSettings, settings: {
...defaultSettings,
general: {
...defaultSettings.general,
isBrandingEnabled,
},
},
} }
} }