Skip to content

Commit

Permalink
🐛 (workspace) Fix members invitation when having unlimited plan
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno authored and jmgoncalves97 committed Jan 17, 2025
1 parent 5696ae5 commit bdbd51e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,15 @@ export const convertInvitationsToCollaborations = async (
)
for (const invitation of workspaceInvitations) {
if (!invitation.typebot.workspaceId) continue
await p.memberInWorkspace.upsert({
where: {
userId_workspaceId: {
await p.memberInWorkspace.createMany({
data: [
{
userId: id,
workspaceId: invitation.typebot.workspaceId,
role: WorkspaceRole.GUEST,
},
},
create: {
userId: id,
workspaceId: invitation.typebot.workspaceId,
role: WorkspaceRole.GUEST,
},
update: {},
],
skipDuplicates: true,
})
}
return p.invitation.deleteMany({
Expand Down
1 change: 1 addition & 0 deletions apps/builder/src/features/auth/api/joinWorkspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const joinWorkspaces = async (
role: invitation.type,
userId: id,
})),
skipDuplicates: true,
}),
p.workspaceInvitation.deleteMany({
where: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import {
import { UnlockPlanAlertInfo } from '@/components/UnlockPlanAlertInfo'
import { WorkspaceInvitation, WorkspaceRole } from 'db'
import React from 'react'
import { getSeatsLimit } from 'utils/pricing'
import { getSeatsLimit, isSeatsLimitReached } from 'utils/pricing'
import { AddMemberForm } from './AddMemberForm'
import { checkCanInviteMember } from './helpers'
import { MemberItem } from './MemberItem'
import { useUser } from '@/features/account'
import { useWorkspace } from '../../WorkspaceProvider'
Expand All @@ -20,6 +19,7 @@ import { updateMemberQuery } from '../../queries/updateMemberQuery'
import { deleteInvitationQuery } from '../../queries/deleteInvitationQuery'
import { updateInvitationQuery } from '../../queries/updateInvitationQuery'
import { Member } from '../../types'
import { isDefined } from 'utils'

export const MembersList = () => {
const { user } = useUser()
Expand Down Expand Up @@ -88,11 +88,16 @@ export const MembersList = () => {
members.filter((member) => member.role !== WorkspaceRole.GUEST).length +
invitations.length

const canInviteNewMember = checkCanInviteMember({
plan: workspace?.plan,
customSeatsLimit: workspace?.customSeatsLimit,
currentMembersCount,
})
const seatsLimit = workspace ? getSeatsLimit(workspace) : undefined

const canInviteNewMember =
workspace &&
!isSeatsLimitReached({
plan: workspace?.plan,
customSeatsLimit: workspace?.customSeatsLimit,
existingMembersCount: currentMembersCount,
existingInvitationsCount: invitations.length,
})

return (
<Stack w="full" spacing={3}>
Expand All @@ -104,12 +109,12 @@ export const MembersList = () => {
`}
/>
)}
{workspace && (
{isDefined(seatsLimit) && (
<Heading fontSize="2xl">
Members{' '}
{getSeatsLimit(workspace) === -1
{seatsLimit === -1
? ''
: `(${currentMembersCount}/${getSeatsLimit(workspace)})`}
: `(${currentMembersCount + invitations.length}/${seatsLimit})`}
</Heading>
)}
{workspace?.id && canEdit && (
Expand Down

This file was deleted.

29 changes: 18 additions & 11 deletions apps/builder/src/pages/api/workspaces/[workspaceId]/invitations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Workspace, WorkspaceInvitation, WorkspaceRole } from 'db'
import { WorkspaceInvitation, WorkspaceRole } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { forbidden, methodNotAllowed, notAuthenticated } from 'utils/api'
import { getAuthenticatedUser } from '@/features/auth/api'
import { getSeatsLimit } from 'utils/pricing'
import { sendWorkspaceMemberInvitationEmail } from 'emails'
import { env } from 'utils'
import { isSeatsLimitReached } from 'utils/pricing'

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
Expand All @@ -23,7 +23,22 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
})
if (!workspace) return forbidden(res)

if (await checkIfSeatsLimitReached(workspace))
const [existingMembersCount, existingInvitationsCount] =
await prisma.$transaction([
prisma.memberInWorkspace.count({
where: { workspaceId: workspace.id },
}),
prisma.workspaceInvitation.count({
where: { workspaceId: workspace.id },
}),
])
if (
isSeatsLimitReached({
existingMembersCount,
existingInvitationsCount,
...workspace,
})
)
return res.status(400).send('Seats limit reached')
if (existingUser) {
await prisma.memberInWorkspace.create({
Expand Down Expand Up @@ -66,12 +81,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
methodNotAllowed(res)
}

const checkIfSeatsLimitReached = async (workspace: Workspace) => {
const existingMembersCount = await prisma.memberInWorkspace.count({
where: { workspaceId: workspace.id },
})

return existingMembersCount >= getSeatsLimit(workspace)
}

export default handler
16 changes: 16 additions & 0 deletions packages/utils/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ export const getSeatsLimit = ({
return seatsLimit[plan].totalIncluded
}

export const isSeatsLimitReached = ({
existingMembersCount,
existingInvitationsCount,
plan,
customSeatsLimit,
}: { existingMembersCount: number; existingInvitationsCount: number } & Pick<
Workspace,
'plan' | 'customSeatsLimit'
>) => {
const seatsLimit = getSeatsLimit({ plan, customSeatsLimit })
return (
seatsLimit !== infinity &&
seatsLimit <= existingMembersCount + existingInvitationsCount
)
}

export const computePrice = (
plan: Plan,
selectedTotalChatsIndex: number,
Expand Down

0 comments on commit bdbd51e

Please sign in to comment.