Skip to content

Commit

Permalink
feat(web): add domain limits
Browse files Browse the repository at this point in the history
Close #36
  • Loading branch information
IKatsuba committed May 8, 2024
1 parent a1368ff commit 8dd9e0c
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions apps/web/app/api/workspaces/[workspaceSlug]/domains/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,34 @@ import { createAdminClient } from '@saasfy/supabase/server';

export const POST = withWorkspaceUser(
['owner', 'member'] as const,
async ({ req, params, workspace }) => {
async ({ req, params, workspace: { id: workspaceId } }) => {
const { name } = await req.json();

const supabase = createAdminClient();

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

const maxDomains = workspace?.plans?.max_domains || 1;

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

const domain = await supabase
.from('domains')
.insert({ slug: name, workspace_id: workspace.id })
.insert({ slug: name, workspace_id: workspaceId })
.select('*')
.single();

Expand Down

0 comments on commit 8dd9e0c

Please sign in to comment.