🐛 (workspace) Fix members invitation when having unlimited plan
Closes #279
This commit is contained in:
@ -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: {
|
||||
userId: id,
|
||||
workspaceId: invitation.typebot.workspaceId,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
await p.memberInWorkspace.createMany({
|
||||
data: [
|
||||
{
|
||||
userId: id,
|
||||
workspaceId: invitation.typebot.workspaceId,
|
||||
role: WorkspaceRole.GUEST,
|
||||
},
|
||||
update: {},
|
||||
],
|
||||
skipDuplicates: true,
|
||||
})
|
||||
}
|
||||
return p.invitation.deleteMany({
|
||||
|
@ -12,6 +12,7 @@ export const joinWorkspaces = async (
|
||||
role: invitation.type,
|
||||
userId: id,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
}),
|
||||
p.workspaceInvitation.deleteMany({
|
||||
where: {
|
||||
|
@ -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'
|
||||
@ -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()
|
||||
@ -88,10 +88,15 @@ export const MembersList = () => {
|
||||
members.filter((member) => member.role !== WorkspaceRole.GUEST).length +
|
||||
invitations.length
|
||||
|
||||
const canInviteNewMember = checkCanInviteMember({
|
||||
const seatsLimit = workspace ? getSeatsLimit(workspace) : undefined
|
||||
|
||||
const canInviteNewMember =
|
||||
workspace &&
|
||||
!isSeatsLimitReached({
|
||||
plan: workspace?.plan,
|
||||
customSeatsLimit: workspace?.customSeatsLimit,
|
||||
currentMembersCount,
|
||||
existingMembersCount: currentMembersCount,
|
||||
existingInvitationsCount: invitations.length,
|
||||
})
|
||||
|
||||
return (
|
||||
@ -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 && (
|
||||
|
@ -1,23 +0,0 @@
|
||||
import { Plan } from 'db'
|
||||
import { getSeatsLimit } from 'utils/pricing'
|
||||
|
||||
export function checkCanInviteMember({
|
||||
plan,
|
||||
customSeatsLimit,
|
||||
currentMembersCount,
|
||||
}: {
|
||||
plan?: Plan
|
||||
customSeatsLimit?: number | null
|
||||
currentMembersCount?: number
|
||||
}) {
|
||||
if (!plan || !currentMembersCount) return false
|
||||
|
||||
const seatsLimit = getSeatsLimit({
|
||||
plan,
|
||||
customSeatsLimit: customSeatsLimit ?? null,
|
||||
})
|
||||
|
||||
if (seatsLimit === -1) return true
|
||||
|
||||
return seatsLimit > currentMembersCount
|
||||
}
|
@ -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)
|
||||
@ -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({
|
||||
@ -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
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user