| 1 | 'use server' |
| 2 | |
| 3 | import { render } from '@react-email/render' |
| 4 | import { and, eq |
| 5 | import { headers } |
| 6 | import { authService } |
| 7 | import { projectsService } |
| 8 | // import { PROJECT_TEAM_CACHE_TAG } from '@/api/team/service' |
| 9 | import TeamAssignedToProjectEmail from '@/emails/templates/team-assigned-to-project |
| 10 | import { sendEmailsToRecipients } |
| 11 | import { authedActionClient } |
| 12 | import { todayDateOnly } |
| 13 | import { auth } |
| 14 | import { db } |
| 15 | import { settings as |
| 16 | import { members, teamMembers |
| 17 | import { |
| 18 | projectClientAssignments, |
| 19 | projectInvitations, |
| 20 | projectMemberAssignments, |
| 21 | projectTeamAssignments, |
| 22 | } from '@/server/db/schema/project' |
| 23 | import { memberRates } |
| 24 | import { |
| 25 | addExistingMemberToProjectSchema, |
| 26 | assignTeamSchema, |
| 27 | linkInvitationSchema, |
| 28 | removeClientSchema, |
| 29 | removeMemberSchema, |
| 30 | unassignTeamSchema, |
| 31 | } from './common' |
| 32 | |
| 33 | export const linkInvitationToProjectAction = |
| 34 | .inputSchema(linkInvitationSchema) |
| 35 | .action( |
| 36 | async ({ |
| 37 | parsedInput: { invitationId, |
| 38 | ctx: { role, |
| 39 | }) => { |
| 40 | if (!role.authorize |
| 41 | throw new Error( |
| 42 | } |
| 43 | const hasProjectAccess = |
| 44 | orgMember.organizationId, |
| 45 | projectId, |
| 46 | user.id |
| 47 | ) |
| 48 | if (!hasProjectAccess.success |
| 49 | throw new Error( |
| 50 | } |
| 51 | |
| 52 | const [record] |
| 53 | .insert(projectInvitations) |
| 54 | .values({ invitationId |
| 55 | .onConflictDoNothing() |
| 56 | .returning() |
| 57 | |
| 58 | return record |
| 59 | } |
| 60 | ) |
| 61 | |
| 62 | async function removeFromOrgIfNoAssignments |
| 63 | const [memberAssignment] |
| 64 | .select({ id: |
| 65 | .from(projectMemberAssignments) |
| 66 | .where(eq(projectMemberAssignments |
| 67 | .limit(1) |
| 68 | |
| 69 | const [clientAssignment] |
| 70 | .select({ id: |
| 71 | .from(projectClientAssignments) |
| 72 | .where(eq(projectClientAssignments |
| 73 | .limit(1) |
| 74 | |
| 75 | if (!(memberAssignment |
| 76 | await auth.api |
| 77 | headers: await headers |
| 78 | body: { memberIdOrEmail: memberId |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | export const removeMemberAction = |
| 84 | .inputSchema(removeMemberSchema) |
| 85 | .action( |
| 86 | async ({ |
| 87 | parsedInput: { assignmentId |
| 88 | ctx: { role, |
| 89 | }) => { |
| 90 | if (!role.authorize |
| 91 | throw new Error( |
| 92 | } |
| 93 | |
| 94 | const [assignment] |
| 95 | .select({ |
| 96 | memberId: projectMemberAssignments.memberId |
| 97 | projectId: projectMemberAssignments.projectId |
| 98 | }) |
| 99 | .from(projectMemberAssignments) |
| 100 | .where(eq(projectMemberAssignments |
| 101 | |
| 102 | if (!assignment) |
| 103 | throw new Error( |
| 104 | } |
| 105 | const hasProjectAccess = |
| 106 | orgMember.organizationId, |
| 107 | assignment.projectId, |
| 108 | user.id |
| 109 | ) |
| 110 | if (!hasProjectAccess.success |
| 111 | throw new Error( |
| 112 | } |
| 113 | |
| 114 | await db |
| 115 | .delete(projectMemberAssignments) |
| 116 | .where(eq(projectMemberAssignments |
| 117 | |
| 118 | // If member has no remaining project assignments, remove from org |
| 119 | if (assignment) { |
| 120 | await removeFromOrgIfNoAssignments(assignment |
| 121 | } |
| 122 | |
| 123 | return { success: |
| 124 | } |
| 125 | ) |
| 126 | |
| 127 | export const removeClientAction = |
| 128 | .inputSchema(removeClientSchema) |
| 129 | .action( |
| 130 | async ({ |
| 131 | parsedInput: { assignmentId |
| 132 | ctx: { role, |
| 133 | }) => { |
| 134 | if (!role.authorize |
| 135 | throw new Error( |
| 136 | } |
| 137 | |
| 138 | const [assignment] |
| 139 | .select({ |
| 140 | memberId: projectClientAssignments.memberId |
| 141 | projectId: projectClientAssignments.projectId |
| 142 | }) |
| 143 | .from(projectClientAssignments) |
| 144 | .where(eq(projectClientAssignments |
| 145 | |
| 146 | if (!assignment) |
| 147 | throw new Error( |
| 148 | } |
| 149 | const hasProjectAccess = |
| 150 | orgMember.organizationId, |
| 151 | assignment.projectId, |
| 152 | user.id |
| 153 | ) |
| 154 | if (!hasProjectAccess.success |
| 155 | throw new Error( |
| 156 | } |
| 157 | |
| 158 | await db |
| 159 | .delete(projectClientAssignments) |
| 160 | .where(eq(projectClientAssignments |
| 161 | |
| 162 | // If member has no remaining project assignments, remove from org |
| 163 | if (assignment) { |
| 164 | await removeFromOrgIfNoAssignments(assignment |
| 165 | } |
| 166 | |
| 167 | return { success: |
| 168 | } |
| 169 | ) |
| 170 | |
| 171 | export const assignTeamAction = |
| 172 | .inputSchema(assignTeamSchema) |
| 173 | .action( |
| 174 | async ({ |
| 175 | parsedInput: { projectId, |
| 176 | ctx: { user, |
| 177 | }) => { |
| 178 | if (!role.authorize |
| 179 | throw new Error( |
| 180 | } |
| 181 | const hasProjectAccess = |
| 182 | orgMember.organizationId, |
| 183 | projectId, |
| 184 | user.id |
| 185 | ) |
| 186 | if (!hasProjectAccess.success |
| 187 | throw new Error( |
| 188 | } |
| 189 | |
| 190 | const [assignment] |
| 191 | .insert(projectTeamAssignments) |
| 192 | .values({ projectId |
| 193 | .onConflictDoNothing() |
| 194 | .returning() |
| 195 | |
| 196 | if (!assignment) |
| 197 | throw new Error( |
| 198 | } |
| 199 | |
| 200 | // Send emails to all team members |
| 201 | const [team] |
| 202 | .select({ id: |
| 203 | .from(teams) |
| 204 | .where(eq(teams |
| 205 | |
| 206 | const projectDetails = |
| 207 | |
| 208 | if (team && |
| 209 | const tMembers = |
| 210 | .select({ name: |
| 211 | .from(teamMembers) |
| 212 | .innerJoin(users, |
| 213 | .where(eq(teamMembers |
| 214 | |
| 215 | await sendEmailsToRecipients(tMembers |
| 216 | const html = |
| 217 | TeamAssignedToProjectEmail({ |
| 218 | recipientName: recipient.name |
| 219 | teamName: team.name |
| 220 | projectName: projectDetails.projectName |
| 221 | organizationName: projectDetails.orgName |
| 222 | assignedByName: user.name |
| 223 | orgSlug: projectDetails.orgSlug |
| 224 | projectSlug: projectDetails.projectSlug |
| 225 | }) |
| 226 | ) |
| 227 | return { |
| 228 | to: recipient.email |
| 229 | subject: `Your team "${team |
| 230 | html, |
| 231 | } |
| 232 | }) |
| 233 | } |
| 234 | |
| 235 | return assignment |
| 236 | } |
| 237 | ) |
| 238 | |
| 239 | export const unassignTeamAction = |
| 240 | .inputSchema(unassignTeamSchema) |
| 241 | .action( |
| 242 | async ({ |
| 243 | parsedInput: { assignmentId |
| 244 | ctx: { role, |
| 245 | }) => { |
| 246 | if (!role.authorize |
| 247 | throw new Error( |
| 248 | } |
| 249 | |
| 250 | const [teamAssignment] |
| 251 | .select({ projectId: |
| 252 | .from(projectTeamAssignments) |
| 253 | .where(eq(projectTeamAssignments |
| 254 | if (!teamAssignment) |
| 255 | throw new Error( |
| 256 | } |
| 257 | const hasProjectAccess = |
| 258 | orgMember.organizationId, |
| 259 | teamAssignment.projectId, |
| 260 | user.id |
| 261 | ) |
| 262 | if (!hasProjectAccess.success |
| 263 | throw new Error( |
| 264 | } |
| 265 | |
| 266 | await db |
| 267 | .delete(projectTeamAssignments) |
| 268 | .where(eq(projectTeamAssignments |
| 269 | |
| 270 | return { success: |
| 271 | } |
| 272 | ) |
| 273 | |
| 274 | export const addExistingMemberToProjectAction = |
| 275 | .inputSchema(addExistingMemberToProjectSchema) |
| 276 | .action( |
| 277 | async ({ |
| 278 | parsedInput: { |
| 279 | email, |
| 280 | projectId, |
| 281 | organizationId, |
| 282 | type, |
| 283 | payRate, |
| 284 | payCurrency, |
| 285 | payFrequency, |
| 286 | billingRate, |
| 287 | billingCurrency, |
| 288 | billingFrequency, |
| 289 | setAsOrgDefault, |
| 290 | }, |
| 291 | ctx: { role, |
| 292 | }) => { |
| 293 | if (!role.authorize |
| 294 | throw new Error( |
| 295 | } |
| 296 | if (orgMember.organizationId |
| 297 | throw new Error( |
| 298 | } |
| 299 | const hasProjectAccess = |
| 300 | orgMember.organizationId, |
| 301 | projectId, |
| 302 | user.id |
| 303 | ) |
| 304 | if (!hasProjectAccess.success |
| 305 | throw new Error( |
| 306 | } |
| 307 | |
| 308 | const [targetUser] |
| 309 | .select({ id: |
| 310 | .from(users) |
| 311 | .where(eq(users |
| 312 | |
| 313 | if (!targetUser) |
| 314 | throw new Error( |
| 315 | } |
| 316 | |
| 317 | const [member] |
| 318 | .select({ id: |
| 319 | .from(members) |
| 320 | .where( |
| 321 | and( |
| 322 | eq(members.userId |
| 323 | eq(members.organizationId |
| 324 | ) |
| 325 | ) |
| 326 | |
| 327 | if (!member) |
| 328 | throw new Error( |
| 329 | } |
| 330 | |
| 331 | // Insert into the appropriate project assignment table |
| 332 | if (type === |
| 333 | await db |
| 334 | .insert(projectClientAssignments) |
| 335 | .values({ projectId |
| 336 | .onConflictDoNothing() |
| 337 | } else { |
| 338 | await db |
| 339 | .insert(projectMemberAssignments) |
| 340 | .values({ projectId |
| 341 | .onConflictDoNothing() |
| 342 | } |
| 343 | |
| 344 | // Set member rate if provided. Billing columns fall back to pay values. |
| 345 | if (payRate !== |
| 346 | await db |
| 347 | .insert(memberRates) |
| 348 | .values({ |
| 349 | memberId: member.id |
| 350 | payRate, |
| 351 | payCurrency, |
| 352 | payFrequency: payFrequency ?? |
| 353 | billingRate: billingRate ?? |
| 354 | billingCurrency: billingCurrency ?? |
| 355 | billingFrequency: billingFrequency ?? |
| 356 | effectiveFrom: todayDateOnly(), |
| 357 | }) |
| 358 | .onConflictDoNothing() |
| 359 | } |
| 360 | |
| 361 | // Update workspace wide defaults if checkbox was checked |
| 362 | if (setAsOrgDefault && |
| 363 | const defaults = |
| 364 | payRate, |
| 365 | payCurrency, |
| 366 | payFrequency: payFrequency ?? |
| 367 | billingRate: billingRate ?? |
| 368 | billingCurrency: billingCurrency ?? |
| 369 | billingFrequency: billingFrequency ?? |
| 370 | } |
| 371 | await db |
| 372 | .insert(settingsTable) |
| 373 | .values({ |
| 374 | organizationId, |
| 375 | ...defaults, |
| 376 | }) |
| 377 | .onConflictDoUpdate({ |
| 378 | target: [settingsTable.organizationId |
| 379 | targetWhere: sql`${settingsTable |
| 380 | set: defaults, |
| 381 | }) |
| 382 | } |
| 383 | |
| 384 | return { success: |
| 385 | } |
| 386 | ) |