2
0

feat(editor): Team workspaces

This commit is contained in:
Baptiste Arnaud
2022-05-13 15:22:44 -07:00
parent 6c2986590b
commit f0fdf08b00
132 changed files with 3354 additions and 1228 deletions

View File

@ -17,6 +17,8 @@ import { KBarProvider } from 'kbar'
import { actions } from 'libs/kbar'
import { enableMocks } from 'mocks'
import { SupportBubble } from 'components/shared/SupportBubble'
import { WorkspaceContext } from 'contexts/WorkspaceContext'
import { MaintenancePage } from 'components/shared/MaintenancePage'
if (process.env.NEXT_PUBLIC_E2E_TEST === 'enabled') enableMocks()
@ -31,27 +33,30 @@ const App = ({ Component, pageProps: { session, ...pageProps } }: AppProps) => {
}, [pathname])
const typebotId = query.typebotId?.toString()
return (
<ChakraProvider theme={customTheme}>
<KBarProvider actions={actions}>
<SessionProvider session={session}>
<UserContext>
{typebotId ? (
<TypebotContext typebotId={typebotId}>
<Component />
<SupportBubble />
</TypebotContext>
) : (
<>
<Component {...pageProps} />
<SupportBubble />
</>
)}
</UserContext>
</SessionProvider>
</KBarProvider>
</ChakraProvider>
)
return <MaintenancePage />
// return (
// <ChakraProvider theme={customTheme}>
// <KBarProvider actions={actions}>
// <SessionProvider session={session}>
// <UserContext>
// {typebotId ? (
// <TypebotContext typebotId={typebotId}>
// <WorkspaceContext>
// <Component />
// <SupportBubble />
// </WorkspaceContext>
// </TypebotContext>
// ) : (
// <WorkspaceContext>
// <Component {...pageProps} />
// <SupportBubble />
// </WorkspaceContext>
// )}
// </UserContext>
// </SessionProvider>
// </KBarProvider>
// </ChakraProvider>
// )
}
export default App

View File

@ -1,13 +0,0 @@
import { Stack } from '@chakra-ui/react'
import { AccountHeader } from 'components/account/AccountHeader'
import { Seo } from 'components/Seo'
import { AccountContent } from 'layouts/account/AccountContent'
const AccountSubscriptionPage = () => (
<Stack>
<Seo title="My account" />
<AccountHeader />
<AccountContent />
</Stack>
)
export default AccountSubscriptionPage

View File

@ -1,16 +1,33 @@
// Forked from https://github.com/nextauthjs/adapters/blob/main/packages/prisma/src/index.ts
import { PrismaClient, Prisma, Invitation, Plan } from 'db'
import {
PrismaClient,
Prisma,
Invitation,
Plan,
WorkspaceRole,
WorkspaceInvitation,
} from 'db'
import { randomUUID } from 'crypto'
import type { Adapter, AdapterUser } from 'next-auth/adapters'
import cuid from 'cuid'
import { got } from 'got'
type InvitationWithWorkspaceId = Invitation & {
typebot: {
workspaceId: string | null
}
}
export function CustomAdapter(p: PrismaClient): Adapter {
return {
createUser: async (data: Omit<AdapterUser, 'id'>) => {
const user = { id: cuid(), email: data.email as string }
const invitations = await p.invitation.findMany({
where: { email: user.email },
include: { typebot: { select: { workspaceId: true } } },
})
const workspaceInvitations = await p.workspaceInvitation.findMany({
where: { email: user.email },
})
const createdUser = await p.user.create({
data: {
@ -18,6 +35,25 @@ export function CustomAdapter(p: PrismaClient): Adapter {
id: user.id,
apiToken: randomUUID(),
plan: process.env.ADMIN_EMAIL === data.email ? Plan.PRO : Plan.FREE,
workspaces:
workspaceInvitations.length > 0
? undefined
: {
create: {
role: WorkspaceRole.ADMIN,
workspace: {
create: {
name: data.name
? `${data.name}'s workspace`
: `My workspace`,
plan:
process.env.ADMIN_EMAIL === data.email
? Plan.TEAM
: Plan.FREE,
},
},
},
},
},
})
if (process.env.USER_CREATED_WEBHOOK_URL)
@ -29,6 +65,8 @@ export function CustomAdapter(p: PrismaClient): Adapter {
})
if (invitations.length > 0)
await convertInvitationsToCollaborations(p, user, invitations)
if (workspaceInvitations.length > 0)
await joinWorkspaces(p, user, workspaceInvitations)
return createdUser
},
getUser: (id) => p.user.findUnique({ where: { id } }),
@ -59,7 +97,7 @@ export function CustomAdapter(p: PrismaClient): Adapter {
oauth_token_secret: data.oauth_token_secret as string,
oauth_token: data.oauth_token as string,
refresh_token_expires_in: data.refresh_token_expires_in as number,
}
},
}) as any
},
unlinkAccount: (provider_providerAccountId) =>
@ -94,7 +132,7 @@ export function CustomAdapter(p: PrismaClient): Adapter {
const convertInvitationsToCollaborations = async (
p: PrismaClient,
{ id, email }: { id: string; email: string },
invitations: Invitation[]
invitations: InvitationWithWorkspaceId[]
) => {
await p.collaboratorsOnTypebots.createMany({
data: invitations.map((invitation) => ({
@ -103,9 +141,54 @@ const convertInvitationsToCollaborations = async (
userId: id,
})),
})
const workspaceInvitations = invitations.reduce<InvitationWithWorkspaceId[]>(
(acc, invitation) =>
acc.some(
(inv) => inv.typebot.workspaceId === invitation.typebot.workspaceId
)
? acc
: [...acc, invitation],
[]
)
for (const invitation of workspaceInvitations) {
if (!invitation.typebot.workspaceId) continue
await p.memberInWorkspace.upsert({
where: {
userId_workspaceId: {
userId: id,
workspaceId: invitation.typebot.workspaceId,
},
},
create: {
userId: id,
workspaceId: invitation.typebot.workspaceId,
role: WorkspaceRole.GUEST,
},
update: {},
})
}
return p.invitation.deleteMany({
where: {
email,
},
})
}
const joinWorkspaces = async (
p: PrismaClient,
{ id, email }: { id: string; email: string },
invitations: WorkspaceInvitation[]
) => {
await p.memberInWorkspace.createMany({
data: invitations.map((invitation) => ({
workspaceId: invitation.workspaceId,
role: invitation.type,
userId: id,
})),
})
return p.workspaceInvitation.deleteMany({
where: {
email,
},
})
}

View File

@ -1,30 +0,0 @@
import { withSentry } from '@sentry/nextjs'
import { Prisma } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const { code } =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const coupon = await prisma.coupon.findFirst({
where: { code, dateRedeemed: null },
})
if (!coupon) return res.status(404).send({ message: 'Coupon not found' })
await prisma.user.update({
where: { id: user.id },
data: coupon.userPropertiesToUpdate as Prisma.UserUncheckedUpdateInput,
})
await prisma.coupon.update({
where: { code },
data: { dateRedeemed: new Date() },
})
return res.send({ message: 'Coupon redeemed 🎊' })
}
}
export default withSentry(handler)

View File

@ -1,35 +1,48 @@
import { withSentry } from '@sentry/nextjs'
import { Prisma } from 'db'
import prisma from 'libs/prisma'
import { Credentials } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { encrypt, methodNotAllowed, notAuthenticated } from 'utils'
import {
badRequest,
encrypt,
forbidden,
methodNotAllowed,
notAuthenticated,
} from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const id = req.query.id.toString()
if (user.id !== id) return res.status(401).send({ message: 'Forbidden' })
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res)
if (req.method === 'GET') {
const credentials = await prisma.credentials.findMany({
where: { ownerId: user.id },
select: { name: true, type: true, ownerId: true, id: true },
where: {
workspace: { id: workspaceId, members: { some: { userId: user.id } } },
},
select: { name: true, type: true, workspaceId: true, id: true },
})
console.log('Hey there', credentials)
return res.send({ credentials })
}
if (req.method === 'POST') {
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Omit<Credentials, 'ownerId'>
) as Credentials
const { encryptedData, iv } = encrypt(data.data)
const workspace = await prisma.workspace.findFirst({
where: { id: workspaceId, members: { some: { userId: user.id } } },
select: { id: true },
})
if (!workspace) return forbidden(res)
const credentials = await prisma.credentials.create({
data: {
...data,
data: encryptedData,
iv,
ownerId: user.id,
} as Prisma.CredentialsUncheckedCreateInput,
workspaceId,
},
})
return res.send({ credentials })
}

View File

@ -2,17 +2,20 @@ import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const id = req.query.id.toString()
if (user.id !== id) return res.status(401).send({ message: 'Forbidden' })
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res)
if (req.method === 'DELETE') {
const credentialsId = req.query.credentialsId.toString()
const credentials = await prisma.credentials.delete({
where: { id: credentialsId },
const credentials = await prisma.credentials.deleteMany({
where: {
id: credentialsId,
workspace: { id: workspaceId, members: { some: { userId: user.id } } },
},
})
return res.send({ credentials })
}

View File

@ -4,7 +4,7 @@ import prisma from 'libs/prisma'
import { googleSheetsScopes } from './consent-url'
import { stringify } from 'querystring'
import { CredentialsType } from 'models'
import { encrypt, notAuthenticated } from 'utils'
import { badRequest, encrypt, notAuthenticated } from 'utils'
import { oauth2Client } from 'libs/google-sheets'
import { withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from 'services/api/utils'
@ -12,11 +12,12 @@ import { getAuthenticatedUser } from 'services/api/utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const { redirectUrl, stepId } = JSON.parse(
const { redirectUrl, stepId, workspaceId } = JSON.parse(
Buffer.from(req.query.state.toString(), 'base64').toString()
)
if (req.method === 'GET') {
const code = req.query.code.toString()
if (!workspaceId) return badRequest(res)
if (!code)
return res.status(400).send({ message: "Bad request, couldn't get code" })
const { tokens } = await oauth2Client.getToken(code)
@ -41,20 +42,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const credentials = {
name: email,
type: CredentialsType.GOOGLE_SHEETS,
ownerId: user.id,
workspaceId,
data: encryptedData,
iv,
} as Prisma.CredentialsUncheckedCreateInput
const { id: credentialsId } = await prisma.credentials.upsert({
create: credentials,
update: credentials,
where: {
name_type_ownerId: {
name: credentials.name,
type: credentials.type,
ownerId: user.id,
},
},
const { id: credentialsId } = await prisma.credentials.create({
data: credentials,
})
const queryParams = stringify({ stepId, credentialsId })
res.redirect(

View File

@ -1,26 +1,38 @@
import { withSentry } from '@sentry/nextjs'
import { CustomDomain, Prisma } from 'db'
import { CustomDomain } from 'db'
import { got, HTTPError } from 'got'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
import {
badRequest,
forbidden,
methodNotAllowed,
notAuthenticated,
} from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const id = req.query.id.toString()
if (user.id !== id) return res.status(401).send({ message: 'Forbidden' })
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res)
if (req.method === 'GET') {
const customDomains = await prisma.customDomain.findMany({
where: { ownerId: user.id },
where: {
workspace: { id: workspaceId, members: { some: { userId: user.id } } },
},
})
return res.send({ customDomains })
}
if (req.method === 'POST') {
const workspace = await prisma.workspace.findFirst({
where: { id: workspaceId, members: { some: { userId: user.id } } },
select: { id: true },
})
if (!workspace) return forbidden(res)
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Omit<CustomDomain, 'ownerId'>
) as CustomDomain
try {
await createDomainOnVercel(data.name)
} catch (err) {
@ -31,8 +43,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const customDomains = await prisma.customDomain.create({
data: {
...data,
ownerId: user.id,
} as Prisma.CustomDomainUncheckedCreateInput,
workspaceId,
},
})
return res.send({ customDomains })
}

View File

@ -1,15 +1,15 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed, notAuthenticated } from 'utils'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils'
import { got } from 'got'
import { getAuthenticatedUser } from 'services/api/utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const id = req.query.id.toString()
if (user.id !== id) return res.status(401).send({ message: 'Forbidden' })
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res)
if (req.method === 'DELETE') {
const domain = req.query.domain.toString()
try {
@ -18,6 +18,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const customDomains = await prisma.customDomain.delete({
where: { name: domain },
})
console.log(
{
name: domain,
workspace: { id: workspaceId },
},
{ some: { userId: user.id } }
)
await deleteDomainOnVercel(domain)
return res.send({ customDomains })
}
return methodNotAllowed(res)

View File

@ -3,7 +3,7 @@ import { DashboardFolder } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
@ -12,11 +12,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const parentFolderId = req.query.parentId
? req.query.parentId.toString()
: null
if (req.method === 'GET') {
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res)
const folders = await prisma.dashboardFolder.findMany({
where: {
ownerId: user.id,
parentFolderId,
workspace: { members: { some: { userId: user.id, workspaceId } } },
},
orderBy: { createdAt: 'desc' },
})
@ -25,9 +28,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Pick<DashboardFolder, 'parentFolderId'>
) as Pick<DashboardFolder, 'parentFolderId' | 'workspaceId'>
const folder = await prisma.dashboardFolder.create({
data: { ...data, ownerId: user.id, name: 'New folder' },
data: { ...data, name: 'New folder' },
})
return res.send(folder)
}

View File

@ -11,14 +11,17 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const id = req.query.id.toString()
if (req.method === 'GET') {
const folder = await prisma.dashboardFolder.findUnique({
where: { id_ownerId: { id, ownerId: user.id } },
const folder = await prisma.dashboardFolder.findFirst({
where: {
id,
workspace: { members: { some: { userId: user.id } } },
},
})
return res.send({ folder })
}
if (req.method === 'DELETE') {
const folders = await prisma.dashboardFolder.delete({
where: { id_ownerId: { id, ownerId: user.id } },
const folders = await prisma.dashboardFolder.deleteMany({
where: { id, workspace: { members: { some: { userId: user.id } } } },
})
return res.send({ folders })
}
@ -26,8 +29,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
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 } },
const folders = await prisma.dashboardFolder.updateMany({
where: {
id,
workspace: { members: { some: { userId: user.id } } },
},
data,
})
return res.send({ typebots: folders })

View File

@ -18,7 +18,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
const credentialsId = req.query.credentialsId as string | undefined
if (!credentialsId) return badRequest(res)
const spreadsheetId = req.query.id.toString()
const spreadsheetId = req.query.id as string
const doc = new GoogleSpreadsheet(spreadsheetId)
const auth = await getAuthenticatedGoogleClient(user.id, credentialsId)
if (!auth)

View File

@ -10,8 +10,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2020-08-27',
})
const { email, currency } =
const { email, currency, plan, workspaceId } =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
console.log(plan, workspaceId)
const session = await stripe.checkout.sessions.create({
success_url: `${req.headers.origin}/typebots?stripe=success`,
cancel_url: `${req.headers.origin}/typebots?stripe=cancel`,
@ -19,12 +21,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
allow_promotion_codes: true,
customer_email: email,
mode: 'subscription',
metadata: { workspaceId, plan },
line_items: [
{
price:
currency === 'eur'
? process.env.STRIPE_PRICE_EUR_ID
: process.env.STRIPE_PRICE_USD_ID,
price: getPrice(plan, currency),
quantity: 1,
},
],
@ -34,4 +34,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return methodNotAllowed(res)
}
const getPrice = (plan: 'pro' | 'team', currency: 'eur' | 'usd') => {
if (plan === 'team')
return currency === 'eur'
? process.env.STRIPE_PRICE_TEAM_EUR_ID
: process.env.STRIPE_PRICE_TEAM_USD_ID
return currency === 'eur'
? process.env.STRIPE_PRICE_EUR_ID
: process.env.STRIPE_PRICE_USD_ID
}
export default withSentry(handler)

View File

@ -1,25 +1,36 @@
import { User } from 'db'
import { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react'
import { methodNotAllowed } from 'utils'
import {
badRequest,
forbidden,
methodNotAllowed,
notAuthenticated,
} from 'utils'
import Stripe from 'stripe'
import { withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from 'services/api/utils'
import prisma from 'libs/prisma'
import { WorkspaceRole } from 'db'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getSession({ req })
if (!session?.user)
return res.status(401).json({ message: 'Not authenticated' })
const user = session.user as User
if (!user.stripeId)
return res.status(401).json({ message: 'Not authenticated' })
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'GET') {
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res)
if (!process.env.STRIPE_SECRET_KEY)
throw Error('STRIPE_SECRET_KEY var is missing')
const workspace = await prisma.workspace.findFirst({
where: {
id: workspaceId,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
})
if (!workspace?.stripeId) return forbidden(res)
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2020-08-27',
})
const session = await stripe.billingPortal.sessions.create({
customer: user.stripeId,
customer: workspace.stripeId,
return_url: req.headers.referer,
})
res.redirect(session.url)

View File

@ -40,36 +40,44 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
switch (event.type) {
case 'checkout.session.completed': {
const session = event.data.object as Stripe.Checkout.Session
const { customer_email } = session
if (!customer_email)
return res.status(500).send(`customer_email not found`)
await prisma.user.update({
where: { email: customer_email },
data: { plan: Plan.PRO, stripeId: session.customer as string },
const { metadata } = session
if (!metadata?.workspaceId || !metadata?.plan)
return res.status(500).send({ message: `customer_email not found` })
await prisma.workspace.update({
where: { id: metadata.workspaceId },
data: {
plan: metadata.plan === 'team' ? Plan.TEAM : Plan.PRO,
stripeId: session.customer as string,
},
})
return res.status(200).send({ message: 'user upgraded in DB' })
return res.status(200).send({ message: 'workspace upgraded in DB' })
}
case 'customer.subscription.deleted': {
const subscription = event.data.object as Stripe.Subscription
await prisma.user.update({
const { metadata } = subscription
if (!metadata.workspaceId)
return res.status(500).send(`workspaceId not found`)
await prisma.workspace.update({
where: {
stripeId: subscription.customer as string,
id: metadata.workspaceId,
},
data: {
plan: Plan.FREE,
},
})
return res.status(200).send({ message: 'user downgraded in DB' })
return res.send({ message: 'workspace downgraded in DB' })
}
default: {
return res.status(304).send({ message: 'event not handled' })
}
}
} catch (err) {
console.error(err)
if (err instanceof Error) {
console.error(err)
return res.status(400).send(`Webhook Error: ${err.message}`)
}
return res.status(500).send(`Error occured: ${err}`)
}
}
return methodNotAllowed(res)

View File

@ -1,28 +1,30 @@
import { withSentry } from '@sentry/nextjs'
import { Prisma } from 'db'
import { Prisma, WorkspaceRole } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { parseNewTypebot } from 'services/typebots/typebots'
import { methodNotAllowed, notAuthenticated } from 'utils'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
try {
if (req.method === 'GET') {
const workspaceId = req.query.workspaceId as string | undefined
const folderId = req.query.allFolders
? undefined
: req.query.folderId
? req.query.folderId.toString()
: null
if (!workspaceId) return badRequest(res)
const typebotIds = req.query.typebotIds as string[] | undefined
if (typebotIds) {
const typebots = await prisma.typebot.findMany({
where: {
OR: [
{
ownerId: user.id,
workspace: { members: { some: { userId: user.id } } },
id: { in: typebotIds },
},
{
@ -42,8 +44,29 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
const typebots = await prisma.typebot.findMany({
where: {
ownerId: user.id,
folderId,
OR: [
{
folderId,
workspace: {
id: workspaceId,
members: {
some: {
userId: user.id,
role: { not: WorkspaceRole.GUEST },
},
},
},
},
{
workspace: {
id: workspaceId,
members: {
some: { userId: user.id, role: WorkspaceRole.GUEST },
},
},
collaborators: { some: { userId: user.id } },
},
],
},
orderBy: { createdAt: 'desc' },
select: { name: true, publishedTypebotId: true, id: true, icon: true },

View File

@ -16,26 +16,19 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
where: canReadTypebot(typebotId, user),
include: {
publishedTypebot: true,
owner: { select: { email: true, name: true, image: true } },
collaborators: { select: { userId: true, type: true } },
webhooks: true,
},
})
if (!typebot) return res.send({ typebot: null })
const {
publishedTypebot,
owner,
collaborators,
webhooks,
...restOfTypebot
} = typebot
const { publishedTypebot, collaborators, webhooks, ...restOfTypebot } =
typebot
const isReadOnly =
collaborators.find((c) => c.userId === user.id)?.type ===
CollaborationType.READ
return res.send({
typebot: restOfTypebot,
publishedTypebot,
owner,
isReadOnly,
webhooks,
})

View File

@ -1,33 +1,28 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canWriteTypebot } from 'services/api/dbRules'
import { canEditGuests } from 'services/api/dbRules'
import { getAuthenticatedUser } from 'services/api/utils'
import { forbidden, methodNotAllowed, notAuthenticated } from 'utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const typebotId = req.query.typebotId as string
const userId = req.query.userId as string
const typebot = await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
})
if (!typebot) return forbidden(res)
if (req.method === 'PUT') {
if (req.method === 'PATCH') {
const data = req.body
await prisma.collaboratorsOnTypebots.upsert({
where: { userId_typebotId: { typebotId, userId } },
create: data,
update: data,
await prisma.collaboratorsOnTypebots.updateMany({
where: { userId, typebot: canEditGuests(user, typebotId) },
data: { type: data.type },
})
return res.send({
message: 'success',
})
}
if (req.method === 'DELETE') {
await prisma.collaboratorsOnTypebots.delete({
where: { userId_typebotId: { typebotId, userId } },
await prisma.collaboratorsOnTypebots.deleteMany({
where: { userId, typebot: canEditGuests(user, typebotId) },
})
return res.send({
message: 'success',

View File

@ -1,6 +1,6 @@
import { withSentry } from '@sentry/nextjs'
import { invitationToCollaborate } from 'assets/emails/invitationToCollaborate'
import { CollaborationType } from 'db'
import { CollaborationType, WorkspaceRole } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canReadTypebot, canWriteTypebot } from 'services/api/dbRules'
@ -30,7 +30,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const typebot = await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
})
if (!typebot) return forbidden(res)
if (!typebot || !typebot.workspaceId) return forbidden(res)
const { email, type } =
(req.body as
| { email: string | undefined; type: CollaborationType | undefined }
@ -40,7 +40,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
where: { email: email.toLowerCase() },
select: { id: true },
})
if (existingUser)
if (existingUser) {
await prisma.collaboratorsOnTypebots.create({
data: {
type,
@ -48,7 +48,21 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
userId: existingUser.id,
},
})
else
await prisma.memberInWorkspace.upsert({
where: {
userId_workspaceId: {
userId: existingUser.id,
workspaceId: typebot.workspaceId,
},
},
create: {
role: WorkspaceRole.GUEST,
userId: existingUser.id,
workspaceId: typebot.workspaceId,
},
update: {},
})
} else
await prisma.invitation.create({
data: { email: email.toLowerCase(), type, typebotId },
})

View File

@ -1,33 +1,32 @@
import { withSentry } from '@sentry/nextjs'
import { Invitation } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canWriteTypebot } from 'services/api/dbRules'
import { canEditGuests } from 'services/api/dbRules'
import { getAuthenticatedUser } from 'services/api/utils'
import { forbidden, methodNotAllowed, notAuthenticated } from 'utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const typebotId = req.query.typebotId as string
const email = req.query.email as string
const typebot = await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
})
if (!typebot) return forbidden(res)
if (req.method === 'PUT') {
const data = req.body
await prisma.invitation.upsert({
where: { email_typebotId: { email, typebotId } },
create: data,
update: data,
if (req.method === 'PATCH') {
const data = req.body as Invitation
await prisma.invitation.updateMany({
where: { email, typebot: canEditGuests(user, typebotId) },
data: { type: data.type },
})
return res.send({
message: 'success',
})
}
if (req.method === 'DELETE') {
await prisma.invitation.delete({
where: { email_typebotId: { email, typebotId } },
await prisma.invitation.deleteMany({
where: {
email,
typebot: canEditGuests(user, typebotId),
},
})
return res.send({
message: 'success',

View File

@ -3,13 +3,19 @@ import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { canReadTypebot, canWriteTypebot } from 'services/api/dbRules'
import { getAuthenticatedUser } from 'services/api/utils'
import { isFreePlan } from 'services/user/user'
import { methodNotAllowed, notAuthenticated } from 'utils'
import { isFreePlan } from 'services/workspace'
import { forbidden, methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const workspaceId = req.query.workspaceId as string | undefined
if (req.method === 'GET') {
const workspace = await prisma.workspace.findFirst({
where: { id: workspaceId, members: { some: { userId: user.id } } },
select: { plan: true },
})
if (!workspace) return forbidden(res)
const typebotId = req.query.typebotId.toString()
const lastResultId = req.query.lastResultId?.toString()
const take = parseInt(req.query.limit?.toString())
@ -24,7 +30,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
where: {
typebot: canReadTypebot(typebotId, user),
answers: { some: {} },
isCompleted: isFreePlan(user) ? true : undefined,
isCompleted: isFreePlan(workspace) ? true : undefined,
},
orderBy: {
createdAt: 'desc',

View File

@ -1,38 +0,0 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const isCountOnly = req.query.count as string | undefined
if (isCountOnly) {
const count = await prisma.collaboratorsOnTypebots.count({
where: { userId: user.id },
})
return res.send({ count })
}
const sharedTypebots = await prisma.collaboratorsOnTypebots.findMany({
where: { userId: user.id },
include: {
typebot: {
select: {
name: true,
publishedTypebotId: true,
id: true,
icon: true,
},
},
},
})
return res.send({
sharedTypebots: sharedTypebots.map((typebot) => ({ ...typebot.typebot })),
})
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -20,7 +20,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
id: webhookId,
typebot: {
OR: [
{ ownerId: user.id },
{ workspace: { members: { some: { userId: user.id } } } },
{
collaborators: {
some: {
@ -40,7 +40,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const typebot = await prisma.typebot.findFirst({
where: {
OR: [
{ id: data.typebotId, ownerId: user.id },
{
id: data.typebotId,
workspace: { members: { some: { userId: user.id } } },
},
{
collaborators: {
some: {

View File

@ -0,0 +1,35 @@
import { withSentry } from '@sentry/nextjs'
import { Workspace } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'GET') {
const workspaces = await prisma.workspace.findMany({
where: { members: { some: { userId: user.id } } },
include: { members: true },
orderBy: { createdAt: 'asc' },
})
console.log(workspaces)
return res.send({ workspaces })
}
if (req.method === 'POST') {
const data = req.body as Workspace
const workspace = await prisma.workspace.create({
data: {
...data,
members: { create: [{ role: 'ADMIN', userId: user.id }] },
},
})
return res.status(200).json({
workspace,
})
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -0,0 +1,28 @@
import { withSentry } from '@sentry/nextjs'
import { Workspace, WorkspaceRole } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'PATCH') {
const id = req.query.workspaceId as string
const updates = req.body as Partial<Workspace>
const updatedWorkspace = await prisma.workspace.updateMany({
where: {
id,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
data: updates,
})
return res.status(200).json({
workspace: updatedWorkspace,
})
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -0,0 +1,47 @@
import { withSentry } from '@sentry/nextjs'
import { WorkspaceInvitation, WorkspaceRole } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { forbidden, methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'POST') {
const data = req.body as Omit<WorkspaceInvitation, 'id' | 'createdAt'>
const existingUser = await prisma.user.findUnique({
where: { email: data.email },
})
const workspace = await prisma.workspace.findFirst({
where: {
id: data.workspaceId,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
})
if (!workspace) return forbidden(res)
if (existingUser) {
await prisma.memberInWorkspace.create({
data: {
role: data.type,
workspaceId: data.workspaceId,
userId: existingUser.id,
},
})
return res.send({
member: {
userId: existingUser.id,
name: existingUser.name,
email: existingUser.email,
role: data.type,
workspaceId: data.workspaceId,
},
})
}
const invitation = await prisma.workspaceInvitation.create({ data })
return res.send({ invitation })
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -0,0 +1,39 @@
import { withSentry } from '@sentry/nextjs'
import { WorkspaceInvitation, WorkspaceRole } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'PATCH') {
const data = req.body as Omit<WorkspaceInvitation, 'createdAt'>
const invitation = await prisma.workspaceInvitation.updateMany({
where: {
id: data.id,
workspace: {
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
},
data,
})
return res.send({ invitation })
}
if (req.method === 'DELETE') {
const id = req.query.id as string
await prisma.workspaceInvitation.deleteMany({
where: {
id,
workspace: {
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
},
})
return res.send({ message: 'success' })
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -0,0 +1,42 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated, notFound } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'GET') {
const id = req.query.workspaceId as string
const workspace = await prisma.workspace.findFirst({
where: {
id,
members: { some: { userId: user.id } },
},
include: {
members: {
include: {
user: true,
},
},
invitations: true,
},
})
if (!workspace) return notFound(res)
return res.send({
members: workspace.members.map((member) => ({
userId: member.userId,
role: member.role,
workspaceId: member.workspaceId,
email: member.user.email,
image: member.user.image,
name: member.user.name,
})),
invitations: workspace.invitations,
})
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -0,0 +1,48 @@
import { withSentry } from '@sentry/nextjs'
import { MemberInWorkspace, WorkspaceRole } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'PATCH') {
const workspaceId = req.query.workspaceId as string
const memberId = req.query.id as string
const updates = req.body as Partial<MemberInWorkspace>
const member = await prisma.memberInWorkspace.updateMany({
where: {
userId: memberId,
workspace: {
id: workspaceId,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
},
data: { role: updates.role },
})
return res.status(200).json({
member,
})
}
if (req.method === 'DELETE') {
const workspaceId = req.query.workspaceId as string
const memberId = req.query.id as string
const member = await prisma.memberInWorkspace.deleteMany({
where: {
userId: memberId,
workspace: {
id: workspaceId,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
},
})
return res.status(200).json({
member,
})
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -10,26 +10,32 @@ import { Spinner, useToast } from '@chakra-ui/react'
import { pay } from 'services/stripe'
import { useUser } from 'contexts/UserContext'
import { NextPageContext } from 'next/types'
import { useWorkspace } from 'contexts/WorkspaceContext'
const DashboardPage = () => {
const [isLoading, setIsLoading] = useState(false)
const { query, isReady } = useRouter()
const { user } = useUser()
const { workspace } = useWorkspace()
const toast = useToast({
position: 'top-right',
status: 'success',
})
useEffect(() => {
const subscribe = query.subscribe?.toString()
if (subscribe && user && user.plan === 'FREE') {
const subscribePlan = query.subscribePlan as 'pro' | 'team' | undefined
if (workspace && subscribePlan && user && user.plan === 'FREE') {
setIsLoading(true)
pay(
pay({
user,
navigator.languages.find((l) => l.includes('fr')) ? 'eur' : 'usd'
)
plan: subscribePlan,
workspaceId: workspace.id,
currency: navigator.languages.find((l) => l.includes('fr'))
? 'eur'
: 'usd',
})
}
}, [query.subscribe, user])
}, [query, user, workspace])
useEffect(() => {
if (!isReady) return
@ -38,7 +44,7 @@ const DashboardPage = () => {
if (stripeStatus === 'success')
toast({
title: 'Typebot Pro',
title: 'Payment successful',
description: "You've successfully subscribed 🎉",
})
if (couponCode) {

View File

@ -1,47 +0,0 @@
import React from 'react'
import { Flex, Heading, Stack } from '@chakra-ui/layout'
import { DashboardHeader } from 'components/dashboard/DashboardHeader'
import { Seo } from 'components/Seo'
import { BackButton } from 'components/dashboard/FolderContent/BackButton'
import { useSharedTypebots } from 'services/user/sharedTypebots'
import { useUser } from 'contexts/UserContext'
import { useToast, Wrap } from '@chakra-ui/react'
import { ButtonSkeleton } from 'components/dashboard/FolderContent/FolderButton'
import { TypebotButton } from 'components/dashboard/FolderContent/TypebotButton'
const SharedTypebotsPage = () => {
const { user } = useUser()
const toast = useToast({
position: 'top-right',
status: 'error',
})
const { sharedTypebots, isLoading } = useSharedTypebots({
userId: user?.id,
onError: (e) =>
toast({ title: "Couldn't fetch shared bots", description: e.message }),
})
return (
<Stack minH="100vh">
<Seo title="My typebots" />
<DashboardHeader />
<Flex w="full" flex="1" justify="center">
<Stack w="1000px" spacing={6}>
<Heading as="h1">Shared with me</Heading>
<Stack>
<Flex>
<BackButton id={null} />
</Flex>
<Wrap spacing={4}>
{isLoading && <ButtonSkeleton />}
{sharedTypebots?.map((typebot) => (
<TypebotButton key={typebot.id} typebot={typebot} isReadOnly />
))}
</Wrap>
</Stack>
</Stack>
</Flex>
</Stack>
)
}
export default SharedTypebotsPage