-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): add member invite compatibility
- Loading branch information
Showing
44 changed files
with
873 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"root": true, | ||
"ignorePatterns": ["**/*"], | ||
"plugins": ["@nx"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": { | ||
"@nx/enforce-module-boundaries": [ | ||
"error", | ||
{ | ||
"enforceBuildableLibDependency": true, | ||
"allow": [], | ||
"depConstraints": [ | ||
{ | ||
"sourceTag": "*", | ||
"onlyDependOnLibsWithTags": ["*"] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"extends": ["plugin:@nx/typescript"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"extends": ["plugin:@nx/javascript"], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { withUser } from '@saasfy/api/server'; | ||
import { createWorkspaceUser } from '@saasfy/crud/workspace-users/server'; | ||
import { createAdminClient } from '@saasfy/supabase/server'; | ||
|
||
export const POST = withUser<{ inviteId: string }>(async ({ req, user, params }) => { | ||
const supabase = createAdminClient(); | ||
|
||
const { data: invite, error } = await supabase | ||
.from('workspace_invites') | ||
.select('*, workspaces(*)') | ||
.eq('id', params.inviteId) | ||
.eq('email', user.email!) | ||
.single(); | ||
|
||
if (!invite || error) { | ||
return Response.json( | ||
{ | ||
errors: ['Invitation not found'], | ||
invite: null, | ||
}, | ||
{ | ||
status: 400, | ||
}, | ||
); | ||
} | ||
|
||
if (!invite.workspaces) { | ||
return Response.json( | ||
{ | ||
errors: ['Invalid invitation'], | ||
invite: null, | ||
}, | ||
{ | ||
status: 400, | ||
}, | ||
); | ||
} | ||
|
||
const { errors, workspaceUser } = await createWorkspaceUser({ | ||
workspace_id: invite.workspaces.id, | ||
user_id: user.id, | ||
role: invite.role, | ||
}); | ||
|
||
await supabase.from('workspace_invites').update({ status: 'accepted' }).eq('id', params.inviteId); | ||
|
||
return Response.json( | ||
{ | ||
errors, | ||
workspaceUser, | ||
}, | ||
{ | ||
status: errors ? 400 : 200, | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { withUser } from '@saasfy/api/server'; | ||
import { createAdminClient } from '@saasfy/supabase/server'; | ||
|
||
export const POST = withUser<{ inviteId: string }>(async ({ req, user, params }) => { | ||
const supabase = createAdminClient(); | ||
|
||
const { data: invite, error } = await supabase | ||
.from('workspace_invites') | ||
.select('*, workspaces(*)') | ||
.eq('id', params.inviteId) | ||
.eq('email', user.email!) | ||
.single(); | ||
|
||
if (!invite || error) { | ||
return Response.json( | ||
{ | ||
errors: ['Invitation not found'], | ||
invite: null, | ||
}, | ||
{ | ||
status: 400, | ||
}, | ||
); | ||
} | ||
|
||
if (!invite.workspaces) { | ||
return Response.json( | ||
{ | ||
errors: ['Invalid invitation'], | ||
invite: null, | ||
}, | ||
{ | ||
status: 400, | ||
}, | ||
); | ||
} | ||
|
||
const { error: deleteError } = await supabase | ||
.from('workspace_invites') | ||
.update({ status: 'declined' }) | ||
.eq('id', params.inviteId); | ||
|
||
return Response.json( | ||
{ | ||
errors: deleteError ? [deleteError.message] : null, | ||
}, | ||
{ | ||
status: deleteError ? 400 : 200, | ||
}, | ||
); | ||
}); |
21 changes: 21 additions & 0 deletions
21
apps/web/app/api/workspaces/[workspaceSlug]/invites/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { withWorkspaceOwner } from '@saasfy/api/server'; | ||
import { createInvite } from '@saasfy/invites/server'; | ||
|
||
export const POST = withWorkspaceOwner(async ({ req, workspace, user }) => { | ||
const data = await req.json(); | ||
|
||
const { errors, invite } = await createInvite({ | ||
...data, | ||
workspace_id: workspace.id, | ||
}); | ||
|
||
return Response.json( | ||
{ | ||
errors, | ||
invite, | ||
}, | ||
{ | ||
status: errors ? 400 : 200, | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import { useParams, useRouter } from 'next/navigation'; | ||
|
||
import { Button } from '@saasfy/ui/button'; | ||
import { useToast } from '@saasfy/ui/use-toast'; | ||
|
||
export function AcceptButton() { | ||
const { id } = useParams(); | ||
const router = useRouter(); | ||
const { toast } = useToast(); | ||
|
||
return ( | ||
<Button | ||
onClick={async function handleClick() { | ||
const response = await fetch(`/api/invites/${id}/accept`, { | ||
method: 'POST', | ||
}); | ||
|
||
if (response.ok) { | ||
router.push('/'); | ||
|
||
toast({ | ||
title: 'Success', | ||
description: 'Invitation accepted.', | ||
}); | ||
} else { | ||
toast({ | ||
title: 'Error', | ||
description: 'Failed to accept invitation.', | ||
variant: 'destructive', | ||
}); | ||
} | ||
}} | ||
> | ||
Accept Invitation | ||
</Button> | ||
); | ||
} | ||
|
||
export function DeclineButton() { | ||
const { id } = useParams(); | ||
const router = useRouter(); | ||
const { toast } = useToast(); | ||
|
||
return ( | ||
<Button | ||
variant="outline" | ||
onClick={async function handleClick() { | ||
const response = await fetch(`/api/invites/${id}/decline`, { | ||
method: 'POST', | ||
}); | ||
|
||
if (response.ok) { | ||
router.push('/'); | ||
|
||
toast({ | ||
title: 'Success', | ||
description: 'Invitation declined.', | ||
}); | ||
} else { | ||
toast({ | ||
title: 'Error', | ||
description: 'Failed to decline invitation.', | ||
variant: 'destructive', | ||
}); | ||
} | ||
}} | ||
> | ||
Decline | ||
</Button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.