Skip to content

Commit

Permalink
Merge pull request #47 from Saasfy/max-members
Browse files Browse the repository at this point in the history
feat(web): add user limits
  • Loading branch information
IKatsuba authored May 8, 2024
2 parents 133fe56 + 22d2ae1 commit a1368ff
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
20 changes: 20 additions & 0 deletions apps/web/app/api/invites/[inviteId]/accept/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ export const POST = withUser<{ inviteId: string }>(async ({ req, user, params })
);
}

const { data: workspace } = await supabase
.from('workspaces')
.select('*, plans(*), workspace_users(id)')
.eq('id', invite.workspaces.id)
.single();

const maxUsers = workspace?.plans?.max_users || 0;

if (workspace?.workspace_users?.length ?? 0 >= maxUsers) {
return Response.json(
{
errors: ['Workspace is full'],
workspace: null,
},
{
status: 400,
},
);
}

const { errors, workspaceUser } = await createWorkspaceUser({
workspace_id: invite.workspaces.id,
user_id: user.id,
Expand Down
45 changes: 45 additions & 0 deletions apps/web/app/api/workspaces/[workspaceSlug]/members/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { withWorkspaceOwner } from '@saasfy/api/server';
import { createWorkspaceUser } from '@saasfy/crud/workspace-users/server';
import { createAdminClient } from '@saasfy/supabase/server';

export const POST = withWorkspaceOwner(async ({ req, workspace: { id: workspaceId }, user }) => {
const data = await req.json();

const supabase = createAdminClient();

const { data: workspace } = await supabase
.from('workspaces')
.select('*, plans(*), workspace_users(id)')
.eq('id', workspaceId)
.single();

const maxUsers = workspace?.plans?.max_users || 0;

if (workspace?.workspace_users?.length ?? 0 >= maxUsers) {
return Response.json(
{
errors: ['Workspace is full'],
workspace: null,
},
{
status: 400,
},
);
}

const { errors, workspaceUser } = await createWorkspaceUser({
...data,
workspace_id: workspaceId,
user_id: user.id,
});

return Response.json(
{
errors,
workspaceUser,
},
{
status: errors ? 400 : 200,
},
);
});
22 changes: 0 additions & 22 deletions apps/web/app/api/workspaces/[workspaceSlug]/users/route.ts

This file was deleted.

0 comments on commit a1368ff

Please sign in to comment.