Skip to content

Commit

Permalink
🐛 (workspace) Fix members invitation when having unlimited plan
Browse files Browse the repository at this point in the history
Closes #279
  • Loading branch information
baptisteArno committed Feb 13, 2023
1 parent 46e9271 commit 0dba994
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

4 comments on commit 0dba994

@vercel
Copy link

@vercel vercel bot commented on 0dba994 Feb 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

app.yvon.earth
ar.nigerias.io
bot.enreso.org
bot.rslabs.pro
bots.bridge.ai
chat.hayuri.id
chicken.cr8.ai
gollum.riku.ai
gsbulletin.com
panther.cr7.ai
panther.cr8.ai
penguin.cr8.ai
talk.gocare.io
test.bot.gives
ticketfute.com
unicorn.cr8.ai
apo.nigerias.io
apr.nigerias.io
aso.nigerias.io
bot.ageenda.com
bot.artiweb.app
bot.devitus.com
bot.jesopizz.it
bot.reeplai.com
bot.scayver.com
bot.tc-mail.com
chat.lalmon.com
chat.sureb4.com
eventhub.com.au
fitness.riku.ai
games.klujo.com
help.taxtree.io
sakuranembro.it
typebot.aloe.do
bot.contakit.com
bot.piccinato.co
bot.sv-energy.it
botc.ceox.com.br
clo.closeer.work
cockroach.cr8.ai
faqs.nigerias.io
feedback.ofx.one
form.syncwin.com
haymanevents.com
kw.wpwakanda.com
myrentalhost.com
stan.vselise.com
start.taxtree.io
typebot.aloe.bot
voicehelp.cr8.ai
zap.fundviser.in
app.chatforms.net
bot.hostnation.de
bot.maitempah.com
bot.phuonghub.com
bot.reviewzer.com
bot.rihabilita.it
cares.urlabout.me
chat.gaswadern.de
fmm.wpwakanda.com
tarian.theiofoundation.org
ted.meujalecobrasil.com.br
type.dericsoncalari.com.br
bot.pinpointinteractive.com
bot.polychromes-project.com
bot.seidinembroseanchetu.it
chatbot.berbelanjabiz.trade
designguide.techyscouts.com
liveconvert2.kandalearn.com
presente.empresarias.com.mx
sell.sellthemotorhome.co.uk
anamnese.odontopavani.com.br
austin.channelautomation.com
bot.marketingplusmindset.com
bot.seidibergamoseanchetu.it
desabafe.sergiolimajr.com.br
download.venturemarketing.in
piazzatorre.barrettamario.it
type.cookieacademyonline.com
bot.brigadeirosemdrama.com.br
forms.escoladeautomacao.com.br
onboarding.libertydreamcare.ie
type.talitasouzamarques.com.br
agendamento.sergiolimajr.com.br
anamnese.clinicamegasjdr.com.br
bookings.littlepartymonkeys.com
bot.comercializadoraomicron.com
elevateyourmind.groovepages.com
viewer-v2-typebot-io.vercel.app
yourfeedback.comebackreward.com
gerador.verificadordehospedes.com
personal-trainer.barrettamario.it
preagendamento.sergiolimajr.com.br
studiotecnicoimmobiliaremerelli.it
download.thailandmicespecialist.com
register.thailandmicespecialist.com
bot.studiotecnicoimmobiliaremerelli.it
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
viewer-v2-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 0dba994 Feb 13, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 0dba994 Feb 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 0dba994 Feb 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

docs – ./apps/docs

docs.typebot.io
docs-git-main-typebot-io.vercel.app
docs-typebot-io.vercel.app

Please sign in to comment.