Skip to content

Commit

Permalink
馃泜 (billing) Add isPastDue field in workspace
Browse files Browse the repository at this point in the history
Closes #1039
  • Loading branch information
baptisteArno committed Nov 21, 2023
1 parent 94886ca commit 55cb6e8
Show file tree
Hide file tree
Showing 25 changed files with 339 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ export const getLinkedTypebots = authenticatedProcedure
variables: true,
name: true,
createdAt: true,
workspaceId: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
},
},
},
},
collaborators: {
select: {
type: true,
Expand Down Expand Up @@ -97,7 +107,17 @@ export const getLinkedTypebots = authenticatedProcedure
variables: true,
name: true,
createdAt: true,
workspaceId: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
},
},
},
},
collaborators: {
select: {
type: true,
Expand Down
11 changes: 11 additions & 0 deletions apps/builder/src/features/collaboration/api/getCollaborators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ export const getCollaborators = authenticatedProcedure
},
include: {
collaborators: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
},
},
},
},
},
})
if (
Expand Down
13 changes: 12 additions & 1 deletion apps/builder/src/features/results/api/deleteResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,19 @@ export const deleteResults = authenticatedProcedure
id: typebotId,
},
select: {
workspaceId: true,
groups: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
role: true,
},
},
},
},
collaborators: {
select: {
userId: true,
Expand Down
12 changes: 11 additions & 1 deletion apps/builder/src/features/results/api/getResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ export const getResult = authenticatedProcedure
},
select: {
id: true,
workspaceId: true,
groups: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
},
},
},
},
collaborators: {
select: {
userId: true,
Expand Down
12 changes: 11 additions & 1 deletion apps/builder/src/features/results/api/getResultLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ export const getResultLogs = authenticatedProcedure
},
select: {
id: true,
workspaceId: true,
groups: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
},
},
},
},
collaborators: {
select: {
userId: true,
Expand Down
12 changes: 11 additions & 1 deletion apps/builder/src/features/results/api/getResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ export const getResults = authenticatedProcedure
},
select: {
id: true,
workspaceId: true,
groups: true,
collaborators: {
select: {
userId: true,
type: true,
},
},
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
},
},
},
},
},
})
if (!typebot || (await isReadTypebotForbidden(typebot, user)))
Expand Down
13 changes: 12 additions & 1 deletion apps/builder/src/features/typebot/api/deleteTypebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ export const deleteTypebot = authenticatedProcedure
},
select: {
id: true,
workspaceId: true,
groups: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
role: true,
},
},
},
},
collaborators: {
select: {
userId: true,
Expand Down
11 changes: 11 additions & 0 deletions apps/builder/src/features/typebot/api/getPublishedTypebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ export const getPublishedTypebot = authenticatedProcedure
include: {
collaborators: true,
publishedTypebot: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
},
},
},
},
},
})
if (
Expand Down
11 changes: 11 additions & 0 deletions apps/builder/src/features/typebot/api/getTypebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ export const getTypebot = authenticatedProcedure
},
include: {
collaborators: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
},
},
},
},
},
})
if (
Expand Down
8 changes: 8 additions & 0 deletions apps/builder/src/features/typebot/api/publishTypebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export const publishTypebot = authenticatedProcedure
workspace: {
select: {
plan: true,
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
role: true,
},
},
},
},
},
Expand Down
12 changes: 12 additions & 0 deletions apps/builder/src/features/typebot/api/unpublishTypebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ export const unpublishTypebot = authenticatedProcedure
include: {
collaborators: true,
publishedTypebot: true,
workspace: {
select: {
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
role: true,
},
},
},
},
},
})
if (!existingTypebot?.publishedTypebot)
Expand Down
12 changes: 10 additions & 2 deletions apps/builder/src/features/typebot/api/updateTypebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export const updateTypebot = authenticatedProcedure
id: true,
customDomain: true,
publicId: true,
workspaceId: true,
collaborators: {
select: {
userId: true,
Expand All @@ -88,7 +87,16 @@ export const updateTypebot = authenticatedProcedure
},
workspace: {
select: {
id: true,
plan: true,
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
role: true,
},
},
},
},
updatedAt: true,
Expand Down Expand Up @@ -160,7 +168,7 @@ export const updateTypebot = authenticatedProcedure
selectedThemeTemplateId: typebot.selectedThemeTemplateId,
events: typebot.events ?? undefined,
groups: typebot.groups
? await sanitizeGroups(existingTypebot.workspaceId)(typebot.groups)
? await sanitizeGroups(existingTypebot.workspace.id)(typebot.groups)
: undefined,
theme: typebot.theme ? typebot.theme : undefined,
settings: typebot.settings
Expand Down
37 changes: 18 additions & 19 deletions apps/builder/src/features/typebot/helpers/isReadTypebotForbidden.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import prisma from '@typebot.io/lib/prisma'
import { env } from '@typebot.io/env'
import { CollaboratorsOnTypebots, User } from '@typebot.io/prisma'
import { Typebot } from '@typebot.io/schemas'
import {
CollaboratorsOnTypebots,
User,
Workspace,
MemberInWorkspace,
} from '@typebot.io/prisma'

export const isReadTypebotForbidden = async (
typebot: Pick<Typebot, 'workspaceId'> & {
typebot: {
collaborators: Pick<CollaboratorsOnTypebots, 'userId'>[]
} & {
workspace: Pick<Workspace, 'isQuarantined' | 'isPastDue'> & {
members: Pick<MemberInWorkspace, 'userId'>[]
}
},
user: Pick<User, 'email' | 'id'>
) => {
if (
env.ADMIN_EMAIL === user.email ||
typebot.collaborators.find(
) =>
typebot.workspace.isQuarantined ||
typebot.workspace.isPastDue ||
(env.ADMIN_EMAIL !== user.email &&
!typebot.collaborators.some(
(collaborator) => collaborator.userId === user.id
)
)
return false
const memberInWorkspace = await prisma.memberInWorkspace.findFirst({
where: {
workspaceId: typebot.workspaceId,
userId: user.id,
},
})
return memberInWorkspace === null
}
) &&
!typebot.workspace.members.some((member) => member.userId === user.id))
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import prisma from '@typebot.io/lib/prisma'
import {
CollaborationType,
CollaboratorsOnTypebots,
MemberInWorkspace,
User,
Workspace,
} from '@typebot.io/prisma'
import { Typebot } from '@typebot.io/schemas'
import { isNotDefined } from '@typebot.io/lib'

export const isWriteTypebotForbidden = async (
typebot: Pick<Typebot, 'workspaceId'> & {
typebot: {
collaborators: Pick<CollaboratorsOnTypebots, 'userId' | 'type'>[]
} & {
workspace: Pick<Workspace, 'isQuarantined' | 'isPastDue'> & {
members: Pick<MemberInWorkspace, 'userId' | 'role'>[]
}
},
user: Pick<User, 'id'>
) => {
if (
typebot.collaborators.find(
(collaborator) => collaborator.userId === user.id
)?.type === CollaborationType.WRITE
return (
typebot.workspace.isQuarantined ||
typebot.workspace.isPastDue ||
!(
typebot.collaborators.find(
(collaborator) => collaborator.userId === user.id
)?.type === CollaborationType.WRITE &&
typebot.workspace.members.some(
(m) => m.userId === user.id && m.role !== 'GUEST'
)
)
)
return false
const memberInWorkspace = await prisma.memberInWorkspace.findFirst({
where: {
workspaceId: typebot.workspaceId,
userId: user.id,
},
})
return isNotDefined(memberInWorkspace) || memberInWorkspace.role === 'GUEST'
}
14 changes: 13 additions & 1 deletion apps/builder/src/features/upload/api/generateUploadUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,19 @@ const parseFilePath = async ({
id: input.typebotId,
},
select: {
workspaceId: true,
workspace: {
select: {
plan: true,
isQuarantined: true,
isPastDue: true,
members: {
select: {
userId: true,
role: true,
},
},
},
},
collaborators: {
select: {
userId: true,
Expand Down
Loading

0 comments on commit 55cb6e8

Please sign in to comment.