2022-03-29 11:20:15 +02:00
|
|
|
import { CollaborationType, Prisma, User } from 'db'
|
|
|
|
|
|
|
|
const parseWhereFilter = (
|
2022-04-21 09:18:35 -07:00
|
|
|
typebotIds: string[] | string,
|
2022-03-29 11:20:15 +02:00
|
|
|
user: User,
|
|
|
|
type: 'read' | 'write'
|
|
|
|
): Prisma.TypebotWhereInput => ({
|
|
|
|
OR: [
|
|
|
|
{
|
2022-04-21 09:18:35 -07:00
|
|
|
id: typeof typebotIds === 'string' ? typebotIds : { in: typebotIds },
|
2022-03-29 11:20:15 +02:00
|
|
|
ownerId:
|
|
|
|
(type === 'read' && user.email === process.env.ADMIN_EMAIL) ||
|
|
|
|
process.env.NEXT_PUBLIC_E2E_TEST
|
|
|
|
? undefined
|
|
|
|
: user.id,
|
|
|
|
},
|
|
|
|
{
|
2022-04-21 09:18:35 -07:00
|
|
|
id: typeof typebotIds === 'string' ? typebotIds : { in: typebotIds },
|
2022-03-29 11:20:15 +02:00
|
|
|
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')
|
2022-04-21 09:18:35 -07:00
|
|
|
|
|
|
|
export const canReadTypebots = (typebotIds: string[], user: User) =>
|
|
|
|
parseWhereFilter(typebotIds, user, 'read')
|
|
|
|
|
|
|
|
export const canWriteTypebots = (typebotIds: string[], user: User) =>
|
|
|
|
parseWhereFilter(typebotIds, user, 'write')
|