| 1 | 'use server' |
| 2 | |
| 3 | import { and, count, eq, sql } from 'drizzle-orm' |
| 4 | import { headers } |
| 5 | import { authedActionClient } |
| 6 | import { auth } |
| 7 | import { db } |
| 8 | import { settings as |
| 9 | import { members, teamMembers |
| 10 | import { pendingMemberRates } |
| 11 | import { |
| 12 | addTeamMemberSchema, |
| 13 | changeOrgMemberRoleSchema, |
| 14 | createTeamSchema, |
| 15 | deleteTeamSchema, |
| 16 | inviteOrgMemberSchema, |
| 17 | removeOrgMemberSchema, |
| 18 | removeTeamMemberSchema, |
| 19 | renameTeamSchema, |
| 20 | } from './common' |
| 21 | |
| 22 | export const inviteOrgMemberAction = |
| 23 | .inputSchema(inviteOrgMemberSchema) |
| 24 | .action( |
| 25 | async ({ |
| 26 | parsedInput: { |
| 27 | organizationId, |
| 28 | email, |
| 29 | role: inviteRole, |
| 30 | hourlyRate, |
| 31 | currency, |
| 32 | setAsOrgDefault, |
| 33 | }, |
| 34 | ctx: { role, |
| 35 | }) => { |
| 36 | if (!role.authorize |
| 37 | throw new Error( |
| 38 | } |
| 39 | |
| 40 | if (orgMember.organizationId |
| 41 | throw new Error( |
| 42 | } |
| 43 | |
| 44 | const result = |
| 45 | headers: await headers |
| 46 | body: { |
| 47 | email, |
| 48 | role: inviteRole, |
| 49 | organizationId, |
| 50 | }, |
| 51 | }) |
| 52 | |
| 53 | if (hourlyRate !== |
| 54 | await db.insert |
| 55 | invitationId: result.id |
| 56 | organizationId, |
| 57 | email, |
| 58 | hourlyRate, |
| 59 | currency, |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | if (setAsOrgDefault && |
| 64 | await db |
| 65 | .insert(settingsTable) |
| 66 | .values({ |
| 67 | organizationId, |
| 68 | memberRate: hourlyRate, |
| 69 | currency, |
| 70 | }) |
| 71 | .onConflictDoUpdate({ |
| 72 | target: [settingsTable.organizationId |
| 73 | targetWhere: sql`${settingsTable |
| 74 | set: { |
| 75 | memberRate: hourlyRate, |
| 76 | currency, |
| 77 | }, |
| 78 | }) |
| 79 | } |
| 80 | |
| 81 | return { success: |
| 82 | } |
| 83 | ) |
| 84 | |
| 85 | export const removeOrgMemberAction = |
| 86 | .inputSchema(removeOrgMemberSchema) |
| 87 | .action(async ( |
| 88 | if (!role.authorize |
| 89 | throw new Error( |
| 90 | } |
| 91 | |
| 92 | const [target] |
| 93 | .select({ id: |
| 94 | .from(members) |
| 95 | .where(eq(members |
| 96 | |
| 97 | if (!target || |
| 98 | throw new Error( |
| 99 | } |
| 100 | |
| 101 | await auth.api |
| 102 | headers: await headers |
| 103 | body: { memberIdOrEmail: memberId |
| 104 | }) |
| 105 | |
| 106 | return { success: |
| 107 | }) |
| 108 | |
| 109 | export const changeOrgMemberRoleAction = |
| 110 | .inputSchema(changeOrgMemberRoleSchema) |
| 111 | .action( |
| 112 | async ({ |
| 113 | parsedInput: { memberId, |
| 114 | ctx: { role, |
| 115 | }) => { |
| 116 | if (!role.authorize |
| 117 | throw new Error( |
| 118 | } |
| 119 | |
| 120 | const [target] |
| 121 | .select({ |
| 122 | id: members.id |
| 123 | organizationId: members.organizationId |
| 124 | userId: members.userId |
| 125 | }) |
| 126 | .from(members) |
| 127 | .where(eq(members |
| 128 | |
| 129 | if (!target || |
| 130 | throw new Error( |
| 131 | } |
| 132 | |
| 133 | await auth.api |
| 134 | headers: await headers |
| 135 | body: { |
| 136 | memberId, |
| 137 | role: newRole, |
| 138 | organizationId: orgMember.organizationId |
| 139 | }, |
| 140 | }) |
| 141 | |
| 142 | return { success: |
| 143 | } |
| 144 | ) |
| 145 | |
| 146 | export const createTeamAction = |
| 147 | .inputSchema(createTeamSchema) |
| 148 | .action( |
| 149 | async ({ |
| 150 | parsedInput: { organizationId, |
| 151 | ctx: { role, |
| 152 | }) => { |
| 153 | if (!role.authorize |
| 154 | throw new Error( |
| 155 | } |
| 156 | if (orgMember.organizationId |
| 157 | throw new Error( |
| 158 | } |
| 159 | |
| 160 | const team = |
| 161 | headers: await headers |
| 162 | body: { name, |
| 163 | }) |
| 164 | |
| 165 | return team |
| 166 | } |
| 167 | ) |
| 168 | |
| 169 | export const renameTeamAction = |
| 170 | .inputSchema(renameTeamSchema) |
| 171 | .action( |
| 172 | async ({ parsedInput |
| 173 | if (!role.authorize |
| 174 | throw new Error( |
| 175 | } |
| 176 | |
| 177 | const [team] |
| 178 | .select({ id: |
| 179 | .from(teams) |
| 180 | .where(eq(teams |
| 181 | |
| 182 | if (!team || |
| 183 | throw new Error( |
| 184 | } |
| 185 | |
| 186 | await db.update |
| 187 | |
| 188 | return { success: |
| 189 | } |
| 190 | ) |
| 191 | |
| 192 | export const deleteTeamAction = |
| 193 | .inputSchema(deleteTeamSchema) |
| 194 | .action(async ( |
| 195 | if (!role.authorize |
| 196 | throw new Error( |
| 197 | } |
| 198 | |
| 199 | const [team] |
| 200 | .select({ id: |
| 201 | .from(teams) |
| 202 | .where(eq(teams |
| 203 | |
| 204 | if (!team || |
| 205 | throw new Error( |
| 206 | } |
| 207 | |
| 208 | const [record] |
| 209 | .select({ teamCount: |
| 210 | .from(teams) |
| 211 | .where(eq(teams |
| 212 | |
| 213 | if (!record || |
| 214 | throw new Error( |
| 215 | } |
| 216 | |
| 217 | await db.delete |
| 218 | |
| 219 | return { success: |
| 220 | }) |
| 221 | |
| 222 | export const addTeamMemberAction = |
| 223 | .inputSchema(addTeamMemberSchema) |
| 224 | .action( |
| 225 | async ({ parsedInput |
| 226 | if (!role.authorize |
| 227 | throw new Error( |
| 228 | } |
| 229 | |
| 230 | const [team] |
| 231 | .select({ id: |
| 232 | .from(teams) |
| 233 | .where(eq(teams |
| 234 | |
| 235 | if (!team || |
| 236 | throw new Error( |
| 237 | } |
| 238 | |
| 239 | // Verify user is an org member |
| 240 | const [member] |
| 241 | .select({ id: |
| 242 | .from(members) |
| 243 | .where( |
| 244 | and( |
| 245 | eq(members.userId |
| 246 | eq(members.organizationId |
| 247 | ) |
| 248 | ) |
| 249 | |
| 250 | if (!member) |
| 251 | throw new Error( |
| 252 | } |
| 253 | |
| 254 | await auth.api |
| 255 | headers: await headers |
| 256 | body: { teamId, |
| 257 | }) |
| 258 | |
| 259 | return { success: |
| 260 | } |
| 261 | ) |
| 262 | |
| 263 | export const removeTeamMemberAction = |
| 264 | .inputSchema(removeTeamMemberSchema) |
| 265 | .action( |
| 266 | async ({ parsedInput |
| 267 | if (!role.authorize |
| 268 | throw new Error( |
| 269 | } |
| 270 | |
| 271 | const [tm] |
| 272 | .select({ |
| 273 | id: teamMembers.id |
| 274 | teamId: teamMembers.teamId |
| 275 | }) |
| 276 | .from(teamMembers) |
| 277 | .where(eq(teamMembers |
| 278 | |
| 279 | if (!tm) |
| 280 | throw new Error( |
| 281 | } |
| 282 | |
| 283 | const [team] |
| 284 | .select({ organizationId: |
| 285 | .from(teams) |
| 286 | .where(eq(teams |
| 287 | |
| 288 | if (!team || |
| 289 | throw new Error( |
| 290 | } |
| 291 | |
| 292 | await db.delete |
| 293 | |
| 294 | return { success: |
| 295 | } |
| 296 | ) |