| 1 | 'use server' |
| 2 | |
| 3 | import { eq } from 'drizzle-orm' |
| 4 | import z from 'zod |
| 5 | import { authedActionClient } |
| 6 | import { db } |
| 7 | import { members } |
| 8 | import { |
| 9 | projectClientAssignments, |
| 10 | projectInvitations, |
| 11 | projectMemberAssignments, |
| 12 | } from '@/server/db/schema/project' |
| 13 | |
| 14 | const acceptInviteSchema = z |
| 15 | invitationId: z.string |
| 16 | projectId: z.string |
| 17 | type: z.enum |
| 18 | }) |
| 19 | |
| 20 | export const acceptInviteAction = |
| 21 | .inputSchema(acceptInviteSchema) |
| 22 | .action(async ( |
| 23 | // Find the member record for this user in the org |
| 24 | // (Better Auth just created it via acceptInvitation) |
| 25 | const memberRecords = |
| 26 | .select() |
| 27 | .from(members) |
| 28 | .where(eq(members |
| 29 | |
| 30 | // Find which member belongs to the same org as the project |
| 31 | // We pick the most recently created one since it was just added |
| 32 | const sortedMembers = |
| 33 | (a, b) |
| 34 | ) |
| 35 | const member = |
| 36 | |
| 37 | if (!member) |
| 38 | throw new Error( |
| 39 | } |
| 40 | |
| 41 | await db.transaction |
| 42 | if (type === |
| 43 | await tx |
| 44 | .insert(projectClientAssignments) |
| 45 | .values({ projectId |
| 46 | .onConflictDoNothing() |
| 47 | } else { |
| 48 | await tx |
| 49 | .insert(projectMemberAssignments) |
| 50 | .values({ projectId |
| 51 | .onConflictDoNothing() |
| 52 | } |
| 53 | |
| 54 | // Clean up the invitation link |
| 55 | await tx |
| 56 | .delete(projectInvitations) |
| 57 | .where(eq(projectInvitations |
| 58 | }) |
| 59 | |
| 60 | return { success: |
| 61 | }) |