| 1 | import { eq } from 'drizzle-orm' |
| 2 | import type { Metadata |
| 3 | import { headers } |
| 4 | import { redirect } |
| 5 | import { createMetadata } |
| 6 | import { auth } |
| 7 | import { db } |
| 8 | import { organizations } |
| 9 | import { projectInvitations, projects |
| 10 | import { AcceptInviteClient } |
| 11 | |
| 12 | export const metadata: |
| 13 | title: 'Accept invitation', |
| 14 | description: 'Join your team on Saturn.', |
| 15 | openGraph: { |
| 16 | images: ['/api/og?page=Invitation'], |
| 17 | }, |
| 18 | twitter: { |
| 19 | images: ['/api/og?page=Invitation'], |
| 20 | }, |
| 21 | }) |
| 22 | |
| 23 | export default async function |
| 24 | params, |
| 25 | }: PageProps< |
| 26 | const { id: |
| 27 | |
| 28 | let invitation: |
| 29 | try { |
| 30 | const result = |
| 31 | headers: await headers |
| 32 | query: { id: invitationId |
| 33 | }) |
| 34 | if (!result) |
| 35 | redirect( |
| 36 | `/error/404?message=${encodeURIComponent('Invitation not found |
| 37 | ) |
| 38 | } |
| 39 | invitation = result |
| 40 | } catch (e) |
| 41 | console.error(e |
| 42 | redirect( |
| 43 | `/error/404?message=${encodeURIComponent('Invitation not found or expired |
| 44 | ) |
| 45 | } |
| 46 | |
| 47 | if (invitation.status |
| 48 | redirect( |
| 49 | `/error/403?message=${encodeURIComponent('This invitation has already been used |
| 50 | ) |
| 51 | } |
| 52 | |
| 53 | // Look up org name |
| 54 | const [org] |
| 55 | .select({ name: |
| 56 | .from(organizations) |
| 57 | .where(eq(organizations |
| 58 | |
| 59 | // Look up linked project (if any) |
| 60 | const [projectLink] |
| 61 | .select({ |
| 62 | projectId: projectInvitations.projectId |
| 63 | type: projectInvitations.type |
| 64 | }) |
| 65 | .from(projectInvitations) |
| 66 | .where(eq(projectInvitations |
| 67 | |
| 68 | let projectInfo: |
| 69 | if (projectLink) { |
| 70 | const [p] |
| 71 | .select({ name: |
| 72 | .from(projects) |
| 73 | .where(eq(projects |
| 74 | projectInfo = p |
| 75 | } |
| 76 | |
| 77 | const roleLabels: |
| 78 | owner: 'Owner', |
| 79 | admin: 'Admin', |
| 80 | member: 'Team Member', |
| 81 | client: 'Client', |
| 82 | } |
| 83 | |
| 84 | return ( |
| 85 | <main className='container flex grow flex-col items-center justify-center self-center p-4 md:p-6 |
| 86 | <AcceptInviteClient |
| 87 | assignmentType={projectLink?.type |
| 88 | email={invitation.email |
| 89 | invitationId={invitationId} |
| 90 | organizationName={org?.name |
| 91 | organizationSlug={org?.slug |
| 92 | projectId={projectLink?.projectId |
| 93 | projectName={projectInfo?.name |
| 94 | projectSlug={projectInfo?.slug |
| 95 | roleLabel={roleLabels[invitation |
| 96 | /> |
| 97 | </main> |
| 98 | ) |
| 99 | } |