| 1 | 'use server' |
| 2 | |
| 3 | import { and, eq } from 'drizzle-orm' |
| 4 | import { headers } |
| 5 | import { authedActionClient } |
| 6 | import { auth } |
| 7 | import { db } |
| 8 | import { members } |
| 9 | import { |
| 10 | projectClientAssignments, |
| 11 | projectMemberAssignments, |
| 12 | } from '@/server/db/schema/project' |
| 13 | import { |
| 14 | assignClientToProjectSchema, |
| 15 | removeClientFromOrgSchema, |
| 16 | removeClientFromProjectSchema, |
| 17 | } from './common' |
| 18 | |
| 19 | export const assignClientToProjectAction = |
| 20 | .inputSchema(assignClientToProjectSchema) |
| 21 | .action( |
| 22 | async ({ |
| 23 | parsedInput: { memberId, |
| 24 | ctx: { role, |
| 25 | }) => { |
| 26 | if (!role.authorize |
| 27 | throw new Error( |
| 28 | } |
| 29 | |
| 30 | const [member] |
| 31 | .select({ |
| 32 | id: members.id |
| 33 | organizationId: members.organizationId |
| 34 | role: members.role |
| 35 | }) |
| 36 | .from(members) |
| 37 | .where(eq(members |
| 38 | |
| 39 | if (!member || |
| 40 | throw new Error( |
| 41 | } |
| 42 | |
| 43 | if (member.role |
| 44 | throw new Error( |
| 45 | } |
| 46 | |
| 47 | const [assignment] |
| 48 | .insert(projectClientAssignments) |
| 49 | .values({ projectId |
| 50 | .onConflictDoNothing() |
| 51 | .returning() |
| 52 | |
| 53 | if (!assignment) |
| 54 | throw new Error( |
| 55 | } |
| 56 | |
| 57 | return { success: |
| 58 | } |
| 59 | ) |
| 60 | |
| 61 | export const removeClientFromProjectAction = |
| 62 | .inputSchema(removeClientFromProjectSchema) |
| 63 | .action( |
| 64 | async ({ parsedInput |
| 65 | if (!role.authorize |
| 66 | throw new Error( |
| 67 | } |
| 68 | |
| 69 | const [assignment] |
| 70 | .select({ |
| 71 | id: projectClientAssignments.id |
| 72 | memberId: projectClientAssignments.memberId |
| 73 | }) |
| 74 | .from(projectClientAssignments) |
| 75 | .innerJoin(members, |
| 76 | .where( |
| 77 | and( |
| 78 | eq(projectClientAssignments.id |
| 79 | eq(members.organizationId |
| 80 | ) |
| 81 | ) |
| 82 | |
| 83 | if (!assignment) |
| 84 | throw new Error( |
| 85 | } |
| 86 | |
| 87 | await db |
| 88 | .delete(projectClientAssignments) |
| 89 | .where(eq(projectClientAssignments |
| 90 | |
| 91 | return { success: |
| 92 | } |
| 93 | ) |
| 94 | |
| 95 | export const removeClientFromOrgAction = |
| 96 | .inputSchema(removeClientFromOrgSchema) |
| 97 | .action(async ( |
| 98 | if (!role.authorize |
| 99 | throw new Error( |
| 100 | } |
| 101 | |
| 102 | const [member] |
| 103 | .select({ |
| 104 | id: members.id |
| 105 | organizationId: members.organizationId |
| 106 | role: members.role |
| 107 | }) |
| 108 | .from(members) |
| 109 | .where(eq(members |
| 110 | |
| 111 | if (!member || |
| 112 | throw new Error( |
| 113 | } |
| 114 | |
| 115 | if (member.role |
| 116 | throw new Error( |
| 117 | } |
| 118 | |
| 119 | // Remove all project assignments first |
| 120 | await db |
| 121 | .delete(projectClientAssignments) |
| 122 | .where(eq(projectClientAssignments |
| 123 | |
| 124 | await db |
| 125 | .delete(projectMemberAssignments) |
| 126 | .where(eq(projectMemberAssignments |
| 127 | |
| 128 | // Remove from org |
| 129 | await auth.api |
| 130 | headers: await headers |
| 131 | body: { memberIdOrEmail: memberId |
| 132 | }) |
| 133 | |
| 134 | return { success: |
| 135 | }) |