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