♻️ (builder) Remove barrel export and flatten folder arch
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
import { Invitation, PrismaClient, WorkspaceRole } from '@typebot.io/prisma'
|
||||
|
||||
export type InvitationWithWorkspaceId = Invitation & {
|
||||
typebot: {
|
||||
workspaceId: string | null
|
||||
}
|
||||
}
|
||||
|
||||
export const convertInvitationsToCollaborations = async (
|
||||
p: PrismaClient,
|
||||
{ id, email }: { id: string; email: string },
|
||||
invitations: InvitationWithWorkspaceId[]
|
||||
) => {
|
||||
await p.collaboratorsOnTypebots.createMany({
|
||||
data: invitations.map((invitation) => ({
|
||||
typebotId: invitation.typebotId,
|
||||
type: invitation.type,
|
||||
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.createMany({
|
||||
data: [
|
||||
{
|
||||
userId: id,
|
||||
workspaceId: invitation.typebot.workspaceId,
|
||||
role: WorkspaceRole.GUEST,
|
||||
},
|
||||
],
|
||||
skipDuplicates: true,
|
||||
})
|
||||
}
|
||||
return p.invitation.deleteMany({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import prisma from '@/lib/prisma'
|
||||
import { setUser } from '@sentry/nextjs'
|
||||
import { User } from '@typebot.io/prisma'
|
||||
import { NextApiRequest } from 'next'
|
||||
import { getSession } from 'next-auth/react'
|
||||
|
||||
export const getAuthenticatedUser = async (
|
||||
req: NextApiRequest
|
||||
): Promise<User | undefined> => {
|
||||
const bearerToken = extractBearerToken(req)
|
||||
if (bearerToken) return authenticateByToken(bearerToken)
|
||||
const session = await getSession({ req })
|
||||
if (!session?.user || !('id' in session.user)) return
|
||||
const user = session.user as User
|
||||
setUser({ id: user.id, email: user.email ?? undefined })
|
||||
return session?.user as User
|
||||
}
|
||||
|
||||
const authenticateByToken = async (
|
||||
apiToken: string
|
||||
): Promise<User | undefined> => {
|
||||
if (typeof window !== 'undefined') return
|
||||
const user = (await prisma.user.findFirst({
|
||||
where: { apiTokens: { some: { token: apiToken } } },
|
||||
})) as User
|
||||
setUser({ id: user.id, email: user.email ?? undefined })
|
||||
return user
|
||||
}
|
||||
|
||||
const extractBearerToken = (req: NextApiRequest) =>
|
||||
req.headers['authorization']?.slice(7)
|
||||
@@ -1,22 +0,0 @@
|
||||
import { PrismaClient, WorkspaceInvitation } from '@typebot.io/prisma'
|
||||
import { InvitationWithWorkspaceId } from './convertInvitationsToCollaborations'
|
||||
|
||||
export const getNewUserInvitations = async (
|
||||
p: PrismaClient,
|
||||
email: string
|
||||
): Promise<{
|
||||
invitations: InvitationWithWorkspaceId[]
|
||||
workspaceInvitations: WorkspaceInvitation[]
|
||||
}> => {
|
||||
const [invitations, workspaceInvitations] = await p.$transaction([
|
||||
p.invitation.findMany({
|
||||
where: { email },
|
||||
include: { typebot: { select: { workspaceId: true } } },
|
||||
}),
|
||||
p.workspaceInvitation.findMany({
|
||||
where: { email },
|
||||
}),
|
||||
])
|
||||
|
||||
return { invitations, workspaceInvitations }
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
export * from './convertInvitationsToCollaborations'
|
||||
export * from './getAuthenticatedUser'
|
||||
export * from './getNewUserInvitations'
|
||||
export * from './joinWorkspaces'
|
||||
@@ -1,23 +0,0 @@
|
||||
import { PrismaClient, WorkspaceInvitation } from '@typebot.io/prisma'
|
||||
|
||||
export const joinWorkspaces = async (
|
||||
p: PrismaClient,
|
||||
{ id, email }: { id: string; email: string },
|
||||
invitations: WorkspaceInvitation[]
|
||||
) => {
|
||||
await p.$transaction([
|
||||
p.memberInWorkspace.createMany({
|
||||
data: invitations.map((invitation) => ({
|
||||
workspaceId: invitation.workspaceId,
|
||||
role: invitation.type,
|
||||
userId: id,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
}),
|
||||
p.workspaceInvitation.deleteMany({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
}),
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user