🐛 (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) {
|
for (const invitation of workspaceInvitations) {
|
||||||
if (!invitation.typebot.workspaceId) continue
|
if (!invitation.typebot.workspaceId) continue
|
||||||
await p.memberInWorkspace.upsert({
|
await p.memberInWorkspace.createMany({
|
||||||
where: {
|
data: [
|
||||||
userId_workspaceId: {
|
{
|
||||||
userId: id,
|
userId: id,
|
||||||
workspaceId: invitation.typebot.workspaceId,
|
workspaceId: invitation.typebot.workspaceId,
|
||||||
|
role: WorkspaceRole.GUEST,
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
create: {
|
skipDuplicates: true,
|
||||||
userId: id,
|
|
||||||
workspaceId: invitation.typebot.workspaceId,
|
|
||||||
role: WorkspaceRole.GUEST,
|
|
||||||
},
|
|
||||||
update: {},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return p.invitation.deleteMany({
|
return p.invitation.deleteMany({
|
||||||
|
@ -12,6 +12,7 @@ export const joinWorkspaces = async (
|
|||||||
role: invitation.type,
|
role: invitation.type,
|
||||||
userId: id,
|
userId: id,
|
||||||
})),
|
})),
|
||||||
|
skipDuplicates: true,
|
||||||
}),
|
}),
|
||||||
p.workspaceInvitation.deleteMany({
|
p.workspaceInvitation.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
|
@ -8,9 +8,8 @@ import {
|
|||||||
import { UnlockPlanAlertInfo } from '@/components/UnlockPlanAlertInfo'
|
import { UnlockPlanAlertInfo } from '@/components/UnlockPlanAlertInfo'
|
||||||
import { WorkspaceInvitation, WorkspaceRole } from 'db'
|
import { WorkspaceInvitation, WorkspaceRole } from 'db'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { getSeatsLimit } from 'utils/pricing'
|
import { getSeatsLimit, isSeatsLimitReached } from 'utils/pricing'
|
||||||
import { AddMemberForm } from './AddMemberForm'
|
import { AddMemberForm } from './AddMemberForm'
|
||||||
import { checkCanInviteMember } from './helpers'
|
|
||||||
import { MemberItem } from './MemberItem'
|
import { MemberItem } from './MemberItem'
|
||||||
import { useUser } from '@/features/account'
|
import { useUser } from '@/features/account'
|
||||||
import { useWorkspace } from '../../WorkspaceProvider'
|
import { useWorkspace } from '../../WorkspaceProvider'
|
||||||
@ -20,6 +19,7 @@ import { updateMemberQuery } from '../../queries/updateMemberQuery'
|
|||||||
import { deleteInvitationQuery } from '../../queries/deleteInvitationQuery'
|
import { deleteInvitationQuery } from '../../queries/deleteInvitationQuery'
|
||||||
import { updateInvitationQuery } from '../../queries/updateInvitationQuery'
|
import { updateInvitationQuery } from '../../queries/updateInvitationQuery'
|
||||||
import { Member } from '../../types'
|
import { Member } from '../../types'
|
||||||
|
import { isDefined } from 'utils'
|
||||||
|
|
||||||
export const MembersList = () => {
|
export const MembersList = () => {
|
||||||
const { user } = useUser()
|
const { user } = useUser()
|
||||||
@ -88,11 +88,16 @@ export const MembersList = () => {
|
|||||||
members.filter((member) => member.role !== WorkspaceRole.GUEST).length +
|
members.filter((member) => member.role !== WorkspaceRole.GUEST).length +
|
||||||
invitations.length
|
invitations.length
|
||||||
|
|
||||||
const canInviteNewMember = checkCanInviteMember({
|
const seatsLimit = workspace ? getSeatsLimit(workspace) : undefined
|
||||||
plan: workspace?.plan,
|
|
||||||
customSeatsLimit: workspace?.customSeatsLimit,
|
const canInviteNewMember =
|
||||||
currentMembersCount,
|
workspace &&
|
||||||
})
|
!isSeatsLimitReached({
|
||||||
|
plan: workspace?.plan,
|
||||||
|
customSeatsLimit: workspace?.customSeatsLimit,
|
||||||
|
existingMembersCount: currentMembersCount,
|
||||||
|
existingInvitationsCount: invitations.length,
|
||||||
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack w="full" spacing={3}>
|
<Stack w="full" spacing={3}>
|
||||||
@ -104,12 +109,12 @@ export const MembersList = () => {
|
|||||||
`}
|
`}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{workspace && (
|
{isDefined(seatsLimit) && (
|
||||||
<Heading fontSize="2xl">
|
<Heading fontSize="2xl">
|
||||||
Members{' '}
|
Members{' '}
|
||||||
{getSeatsLimit(workspace) === -1
|
{seatsLimit === -1
|
||||||
? ''
|
? ''
|
||||||
: `(${currentMembersCount}/${getSeatsLimit(workspace)})`}
|
: `(${currentMembersCount + invitations.length}/${seatsLimit})`}
|
||||||
</Heading>
|
</Heading>
|
||||||
)}
|
)}
|
||||||
{workspace?.id && canEdit && (
|
{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 prisma from '@/lib/prisma'
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { forbidden, methodNotAllowed, notAuthenticated } from 'utils/api'
|
import { forbidden, methodNotAllowed, notAuthenticated } from 'utils/api'
|
||||||
import { getAuthenticatedUser } from '@/features/auth/api'
|
import { getAuthenticatedUser } from '@/features/auth/api'
|
||||||
import { getSeatsLimit } from 'utils/pricing'
|
|
||||||
import { sendWorkspaceMemberInvitationEmail } from 'emails'
|
import { sendWorkspaceMemberInvitationEmail } from 'emails'
|
||||||
import { env } from 'utils'
|
import { env } from 'utils'
|
||||||
|
import { isSeatsLimitReached } from 'utils/pricing'
|
||||||
|
|
||||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
const user = await getAuthenticatedUser(req)
|
const user = await getAuthenticatedUser(req)
|
||||||
@ -23,7 +23,22 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
})
|
})
|
||||||
if (!workspace) return forbidden(res)
|
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')
|
return res.status(400).send('Seats limit reached')
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
await prisma.memberInWorkspace.create({
|
await prisma.memberInWorkspace.create({
|
||||||
@ -66,12 +81,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
methodNotAllowed(res)
|
methodNotAllowed(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkIfSeatsLimitReached = async (workspace: Workspace) => {
|
|
||||||
const existingMembersCount = await prisma.memberInWorkspace.count({
|
|
||||||
where: { workspaceId: workspace.id },
|
|
||||||
})
|
|
||||||
|
|
||||||
return existingMembersCount >= getSeatsLimit(workspace)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default handler
|
export default handler
|
||||||
|
@ -120,6 +120,22 @@ export const getSeatsLimit = ({
|
|||||||
return seatsLimit[plan].totalIncluded
|
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 = (
|
export const computePrice = (
|
||||||
plan: Plan,
|
plan: Plan,
|
||||||
selectedTotalChatsIndex: number,
|
selectedTotalChatsIndex: number,
|
||||||
|
Reference in New Issue
Block a user