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