Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🛂 (billing) Add isPastDue field in workspace #1046

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message 'Typebot not found' is still used for both a missing typebot and forbidden read access. Consider implementing the previously suggested changes to provide clearer feedback to the client.

- throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
+ if (!typebot) {
+   throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' });
+ } else if (await isReadTypebotForbidden(typebot, user)) {
+   throw new TRPCError({ code: 'FORBIDDEN', message: 'Read access to typebot is forbidden' });
+ }

Commitable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (!typebot || (await isReadTypebotForbidden(typebot, user)))
if (!typebot) {
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' });
} else if (await isReadTypebotForbidden(typebot, user)) {
throw new TRPCError({ code: 'FORBIDDEN', message: 'Read access to typebot is forbidden' });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const processTelemetryEvent = authenticatedProcedure
client.capture({
distinctId: event.userId,
event: event.name,
properties: event.data,
properties: 'data' in event ? event.data : undefined,
groups,
})
})
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,
},
},
},
},
},
})
baptisteArno marked this conversation as resolved.
Show resolved Hide resolved
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,
},
},
},
},
},
Comment on lines 32 to 47
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The workspace object is included in the query, but there is no logic that uses the isQuarantined or isPastDue fields. If these fields are intended to affect the unpublishing process, the code should be updated to include the necessary checks. Otherwise, remove the unused fields to optimize performance.

})
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,
Comment on lines 79 to 84
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous comment about the missing closing bracket for the select block in collaborators is still valid. The closing bracket is indeed missing, which will cause a syntax error.

-              type: true,
+              type: true
+            }

Commitable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
id: true,
customDomain: true,
publicId: true,
workspaceId: true,
collaborators: {
select: {
userId: true,
id: true,
customDomain: true,
publicId: true,
collaborators: {
select: {
userId: true,
type: 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,
baptisteArno marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading