2
0

fix(team): 🛂 Improve collab permissions

This commit is contained in:
Baptiste Arnaud
2022-03-29 11:20:15 +02:00
parent 8d6330f6fd
commit bb194b1dbb
14 changed files with 101 additions and 56 deletions

View File

@ -0,0 +1,33 @@
import { CollaborationType, Prisma, User } from 'db'
const parseWhereFilter = (
typebotId: string,
user: User,
type: 'read' | 'write'
): Prisma.TypebotWhereInput => ({
OR: [
{
id: typebotId,
ownerId:
(type === 'read' && user.email === process.env.ADMIN_EMAIL) ||
process.env.NEXT_PUBLIC_E2E_TEST
? undefined
: user.id,
},
{
id: typebotId,
collaborators: {
some: {
userId: user.id,
type: type === 'write' ? CollaborationType.WRITE : undefined,
},
},
},
],
})
export const canReadTypebot = (typebotId: string, user: User) =>
parseWhereFilter(typebotId, user, 'read')
export const canWriteTypebot = (typebotId: string, user: User) =>
parseWhereFilter(typebotId, user, 'write')