| 1 | 'use server' |
| 2 | |
| 3 | import { render } from '@react-email/components' |
| 4 | import { and, eq |
| 5 | import { headers } |
| 6 | import { authService } |
| 7 | import { organizationsService } |
| 8 | import { projectsService } |
| 9 | import { requirementsService } |
| 10 | import { teamService } |
| 11 | import RequirementChangeRequestedEmail from '@/emails/templates/requirement-change-requested |
| 12 | import RequirementSentForSignEmail from '@/emails/templates/requirement-sent-for-sign |
| 13 | import RequirementSignedEmail from '@/emails/templates/requirement-signed |
| 14 | import ThreadNewMessageEmail from '@/emails/templates/thread-new-message |
| 15 | import { baseUrl } |
| 16 | import { sendEmailsToRecipients } |
| 17 | import { authedActionClient } |
| 18 | import { titleToSlug } |
| 19 | import { db } |
| 20 | import { |
| 21 | requirementChangeRequests, |
| 22 | requirementRecipients, |
| 23 | requirementSignatures, |
| 24 | requirements, |
| 25 | threadMessages, |
| 26 | threads, |
| 27 | } from '@/server/db/schema' |
| 28 | import { |
| 29 | addThreadReplySchema, |
| 30 | createRequirementSchema, |
| 31 | createThreadSchema, |
| 32 | requestChangesSchema, |
| 33 | resolveChangeRequestSchema, |
| 34 | sendForSignSchema, |
| 35 | signRequirementSchema, |
| 36 | updateRequirementSchema, |
| 37 | } from './common' |
| 38 | |
| 39 | export const createRequirementAction = |
| 40 | .inputSchema(createRequirementSchema) |
| 41 | .action(async ( |
| 42 | const { orgSlug, |
| 43 | const { orgMember, |
| 44 | const organization = |
| 45 | if (!organization) |
| 46 | throw new Error( |
| 47 | } |
| 48 | const hasProjectAccess = |
| 49 | organization.id, |
| 50 | projectId, |
| 51 | orgMember.userId |
| 52 | ) |
| 53 | if (hasProjectAccess.success |
| 54 | throw new Error( |
| 55 | hasProjectAccess.error ?? |
| 56 | ) |
| 57 | } |
| 58 | const settings = |
| 59 | organization.id, |
| 60 | projectId |
| 61 | ) |
| 62 | if (!role.authorize |
| 63 | throw new Error( |
| 64 | } |
| 65 | const { slugified, |
| 66 | const [requirementWithSlug] |
| 67 | .select({ id: |
| 68 | .from(requirements) |
| 69 | .where( |
| 70 | and( |
| 71 | eq(requirements.projectId |
| 72 | eq(requirements.slug |
| 73 | ) |
| 74 | ) |
| 75 | const [requirement] |
| 76 | .insert(requirements) |
| 77 | .values({ |
| 78 | projectId, |
| 79 | authorId: orgMember.id |
| 80 | title, |
| 81 | slug: requirementWithSlug ? |
| 82 | body: body || |
| 83 | status: |
| 84 | settings.clientInvolvement.requirements |
| 85 | ? 'client_accepted' |
| 86 | : 'draft', |
| 87 | }) |
| 88 | .returning() |
| 89 | |
| 90 | return requirement |
| 91 | }) |
| 92 | |
| 93 | export const updateRequirementAction = |
| 94 | .inputSchema(updateRequirementSchema) |
| 95 | .action(async ( |
| 96 | const { orgSlug, |
| 97 | const { orgMember, |
| 98 | const organization = |
| 99 | if (!organization) |
| 100 | throw new Error( |
| 101 | } |
| 102 | const settings = |
| 103 | organization.id, |
| 104 | projectId |
| 105 | ) |
| 106 | const hasProjectAccess = |
| 107 | organization.id, |
| 108 | projectId, |
| 109 | orgMember.userId |
| 110 | ) |
| 111 | if (hasProjectAccess.success |
| 112 | throw new Error( |
| 113 | hasProjectAccess.error ?? |
| 114 | ) |
| 115 | } |
| 116 | if (!role.authorize |
| 117 | throw new Error( |
| 118 | } |
| 119 | const requirement = |
| 120 | requirementId, |
| 121 | projectId, |
| 122 | await headers() |
| 123 | ) |
| 124 | if (!requirement) |
| 125 | throw new Error( |
| 126 | } |
| 127 | if ( |
| 128 | requirement.status === |
| 129 | settings.clientInvolvement.requirements |
| 130 | ) { |
| 131 | throw new Error( |
| 132 | 'You cannot update a requirement that has been accepted by the client' |
| 133 | ) |
| 134 | } |
| 135 | const [updatedRequirement] |
| 136 | .update(requirements) |
| 137 | .set({ title |
| 138 | .where(eq(requirements |
| 139 | .returning() |
| 140 | return updatedRequirement |
| 141 | }) |
| 142 | |
| 143 | export const sendForSignAction = |
| 144 | .inputSchema(sendForSignSchema) |
| 145 | .action(async ( |
| 146 | const { orgSlug, |
| 147 | const { orgMember, |
| 148 | const organization = |
| 149 | if (!organization) |
| 150 | throw new Error( |
| 151 | } |
| 152 | const hasProjectAccess = |
| 153 | organization.id, |
| 154 | projectId, |
| 155 | orgMember.userId |
| 156 | ) |
| 157 | if (hasProjectAccess.success |
| 158 | throw new Error( |
| 159 | hasProjectAccess.error ?? |
| 160 | ) |
| 161 | } |
| 162 | if (!role.authorize |
| 163 | throw new Error( |
| 164 | 'You do not have permission to send requirements for sign' |
| 165 | ) |
| 166 | } |
| 167 | const settings = |
| 168 | organization.id, |
| 169 | projectId |
| 170 | ) |
| 171 | if (settings.clientInvolvement |
| 172 | throw new Error( |
| 173 | 'Client involvement is disabled for requirements in this project' |
| 174 | ) |
| 175 | } |
| 176 | const project = |
| 177 | if (!project) |
| 178 | throw new Error( |
| 179 | } |
| 180 | const requirement = |
| 181 | requirementId, |
| 182 | projectId, |
| 183 | await headers() |
| 184 | ) |
| 185 | if (!requirement) |
| 186 | throw new Error( |
| 187 | } |
| 188 | |
| 189 | await db.transaction |
| 190 | await tx |
| 191 | .update(requirements) |
| 192 | .set({ |
| 193 | status: 'submitted_to_client', |
| 194 | }) |
| 195 | .where(eq(requirements |
| 196 | const recipientsToSend: |
| 197 | for (const recipient |
| 198 | const clientMember = |
| 199 | organization.id, |
| 200 | recipient |
| 201 | ) |
| 202 | if (!clientMember) |
| 203 | continue |
| 204 | } |
| 205 | recipientsToSend.push({ |
| 206 | email: clientMember.users |
| 207 | name: clientMember.users |
| 208 | }) |
| 209 | await tx |
| 210 | .insert(requirementRecipients) |
| 211 | .values({ |
| 212 | requirementId, |
| 213 | clientMemberId: clientMember.members |
| 214 | }) |
| 215 | .onConflictDoNothing() |
| 216 | } |
| 217 | |
| 218 | await sendEmailsToRecipients(recipientsToSend |
| 219 | const html = |
| 220 | RequirementSentForSignEmail({ |
| 221 | recipientName: recipient.name |
| 222 | requirementTitle: requirement.title |
| 223 | projectName: project.name |
| 224 | senderName: orgMember.user |
| 225 | orgSlug: orgSlug ?? |
| 226 | projectSlug: project.slug |
| 227 | requirementId: requirement.slug |
| 228 | }) |
| 229 | ) |
| 230 | return { |
| 231 | to: recipient.email |
| 232 | subject: `Sign required: "${requirement |
| 233 | html, |
| 234 | } |
| 235 | }) |
| 236 | }) |
| 237 | |
| 238 | return requirement |
| 239 | }) |
| 240 | |
| 241 | export const signRequirementAction = |
| 242 | .inputSchema(signRequirementSchema) |
| 243 | .action(async ( |
| 244 | const { orgSlug, |
| 245 | const { orgMember |
| 246 | const organization = |
| 247 | if (!organization) |
| 248 | throw new Error( |
| 249 | } |
| 250 | const hasProjectAccess = |
| 251 | organization.id, |
| 252 | projectId, |
| 253 | orgMember.userId |
| 254 | ) |
| 255 | if (hasProjectAccess.success |
| 256 | throw new Error( |
| 257 | hasProjectAccess.error ?? |
| 258 | ) |
| 259 | } |
| 260 | const settings = |
| 261 | organization.id, |
| 262 | projectId |
| 263 | ) |
| 264 | if (settings.clientInvolvement |
| 265 | throw new Error( |
| 266 | 'Client involvement is disabled for requirements in this project' |
| 267 | ) |
| 268 | } |
| 269 | const project = |
| 270 | if (!project) |
| 271 | throw new Error( |
| 272 | } |
| 273 | const requirement = |
| 274 | requirementId, |
| 275 | projectId, |
| 276 | await headers() |
| 277 | ) |
| 278 | if (!requirement) |
| 279 | throw new Error( |
| 280 | } |
| 281 | if (requirement.status |
| 282 | throw new Error( |
| 283 | } |
| 284 | const recipients = |
| 285 | const isRecipient = |
| 286 | (r) => |
| 287 | ) |
| 288 | if (!isRecipient) |
| 289 | throw new Error( |
| 290 | } |
| 291 | const existingSignatures = |
| 292 | await requirementsService.getSignatures |
| 293 | if (existingSignatures.some |
| 294 | throw new Error( |
| 295 | } |
| 296 | |
| 297 | await db.transaction |
| 298 | await tx.insert |
| 299 | requirementId, |
| 300 | clientMemberId: orgMember.id |
| 301 | mediaId, |
| 302 | }) |
| 303 | |
| 304 | const totalRecipients = |
| 305 | const totalSigned = |
| 306 | if (totalSigned >= |
| 307 | await tx |
| 308 | .update(requirements) |
| 309 | .set({ status: |
| 310 | .where(eq(requirements |
| 311 | } |
| 312 | |
| 313 | const admins = |
| 314 | const emailsToSend: |
| 315 | for (const admin |
| 316 | emailsToSend.push({ |
| 317 | email: admin.users |
| 318 | name: admin.users |
| 319 | }) |
| 320 | } |
| 321 | await sendEmailsToRecipients(emailsToSend |
| 322 | const html = |
| 323 | RequirementSignedEmail({ |
| 324 | recipientName: recipient.name |
| 325 | requirementTitle: requirement.title |
| 326 | projectName: project.name |
| 327 | signerName: orgMember.user |
| 328 | signedAt: new Date |
| 329 | month: 'long', |
| 330 | day: 'numeric', |
| 331 | year: 'numeric', |
| 332 | hour: 'numeric', |
| 333 | minute: '2-digit', |
| 334 | }), |
| 335 | orgSlug, |
| 336 | projectSlug: project.slug |
| 337 | requirementId: requirement.slug |
| 338 | }) |
| 339 | ) |
| 340 | return { |
| 341 | to: recipient.email |
| 342 | subject: `Requirement signed: "${requirement |
| 343 | html, |
| 344 | } |
| 345 | }) |
| 346 | }) |
| 347 | |
| 348 | return { success: |
| 349 | }) |
| 350 | |
| 351 | export const createThreadAction = |
| 352 | .inputSchema(createThreadSchema) |
| 353 | .action(async ( |
| 354 | const { orgSlug, |
| 355 | parsedInput |
| 356 | const { orgMember, |
| 357 | const organization = |
| 358 | if (!organization) |
| 359 | throw new Error( |
| 360 | } |
| 361 | const hasProjectAccess = |
| 362 | organization.id, |
| 363 | projectId, |
| 364 | orgMember.userId |
| 365 | ) |
| 366 | if (hasProjectAccess.success |
| 367 | throw new Error( |
| 368 | hasProjectAccess.error ?? |
| 369 | ) |
| 370 | } |
| 371 | if (!role.authorize |
| 372 | throw new Error( |
| 373 | } |
| 374 | const project = |
| 375 | if (!project) |
| 376 | throw new Error( |
| 377 | } |
| 378 | const requirement = |
| 379 | requirementId, |
| 380 | projectId, |
| 381 | await headers() |
| 382 | ) |
| 383 | if (!requirement) |
| 384 | throw new Error( |
| 385 | } |
| 386 | const thread = |
| 387 | const [thread] |
| 388 | .insert(threads) |
| 389 | .values({ |
| 390 | projectId, |
| 391 | entityId: requirementId, |
| 392 | selectedText, |
| 393 | createdByMemberId: orgMember.id |
| 394 | }) |
| 395 | .returning() |
| 396 | const receipients = |
| 397 | const emailsToSend: |
| 398 | for (const recipient |
| 399 | emailsToSend.push({ |
| 400 | email: recipient.users |
| 401 | name: recipient.users |
| 402 | }) |
| 403 | } |
| 404 | await sendEmailsToRecipients(emailsToSend |
| 405 | const html = |
| 406 | ThreadNewMessageEmail({ |
| 407 | contextName: requirement.title |
| 408 | contextType: 'requirement', |
| 409 | projectName: project.name |
| 410 | orgSlug, |
| 411 | projectSlug: project.slug |
| 412 | threadLink: `${baseUrl} |
| 413 | messagePreview: threadBody, |
| 414 | senderName: orgMember.user |
| 415 | threadTitle: selectedText, |
| 416 | recipientName: recipient.name |
| 417 | }) |
| 418 | ) |
| 419 | return { |
| 420 | to: recipient.email |
| 421 | subject: `New thread in "${requirement |
| 422 | html, |
| 423 | } |
| 424 | }) |
| 425 | await tx.insert |
| 426 | threadId: thread!.id |
| 427 | body: threadBody, |
| 428 | authorMemberId: orgMember.id |
| 429 | }) |
| 430 | return thread |
| 431 | }) |
| 432 | return thread ?? |
| 433 | }) |
| 434 | |
| 435 | export const requestChangesAction = |
| 436 | .inputSchema(requestChangesSchema) |
| 437 | .action(async ( |
| 438 | const { |
| 439 | orgSlug, |
| 440 | requirementId, |
| 441 | projectId, |
| 442 | description, |
| 443 | referencedThreadIds, |
| 444 | } = parsedInput |
| 445 | const { orgMember, |
| 446 | const organization = |
| 447 | if (!organization) |
| 448 | throw new Error( |
| 449 | } |
| 450 | const hasProjectAccess = |
| 451 | organization.id, |
| 452 | projectId, |
| 453 | orgMember.userId |
| 454 | ) |
| 455 | if (hasProjectAccess.success |
| 456 | throw new Error( |
| 457 | hasProjectAccess.error ?? |
| 458 | ) |
| 459 | } |
| 460 | if (!role.authorize |
| 461 | throw new Error( |
| 462 | } |
| 463 | const settings = |
| 464 | organization.id, |
| 465 | projectId |
| 466 | ) |
| 467 | if (settings.clientInvolvement |
| 468 | throw new Error( |
| 469 | 'Client involvement is disabled for requirements in this project' |
| 470 | ) |
| 471 | } |
| 472 | const project = |
| 473 | if (!project) |
| 474 | throw new Error( |
| 475 | } |
| 476 | const requirement = |
| 477 | requirementId, |
| 478 | projectId, |
| 479 | await headers() |
| 480 | ) |
| 481 | if (!requirement) |
| 482 | throw new Error( |
| 483 | } |
| 484 | const [recipientRow] |
| 485 | .select({ id: |
| 486 | .from(requirementRecipients) |
| 487 | .where( |
| 488 | and( |
| 489 | eq(requirementRecipients.requirementId |
| 490 | eq(requirementRecipients.clientMemberId |
| 491 | ) |
| 492 | ) |
| 493 | .limit(1) |
| 494 | if (!recipientRow) |
| 495 | throw new Error( |
| 496 | } |
| 497 | await db.transaction |
| 498 | await tx.insert |
| 499 | requirementId, |
| 500 | description: description ?? |
| 501 | referencedThreadIds, |
| 502 | requestedByMemberId: orgMember.id |
| 503 | }) |
| 504 | await tx |
| 505 | .update(requirements) |
| 506 | .set({ |
| 507 | status: 'changes_requested', |
| 508 | }) |
| 509 | .where(eq(requirements |
| 510 | const receipients = |
| 511 | const emailsToSend: |
| 512 | for (const recipient |
| 513 | emailsToSend.push({ |
| 514 | email: recipient.users |
| 515 | name: recipient.users |
| 516 | }) |
| 517 | } |
| 518 | await sendEmailsToRecipients(emailsToSend |
| 519 | const html = |
| 520 | RequirementChangeRequestedEmail({ |
| 521 | recipientName: recipient.name |
| 522 | requirementTitle: requirement.title |
| 523 | projectName: project.name |
| 524 | requesterName: orgMember.user |
| 525 | description: description ?? |
| 526 | orgSlug, |
| 527 | projectSlug: project.slug |
| 528 | requirementId: requirement.slug |
| 529 | }) |
| 530 | ) |
| 531 | return { |
| 532 | to: recipient.email |
| 533 | subject: `Changes requested on "${requirement |
| 534 | html, |
| 535 | } |
| 536 | }) |
| 537 | }) |
| 538 | return null |
| 539 | }) |
| 540 | |
| 541 | export const resolveChangeRequestAction = |
| 542 | .inputSchema(resolveChangeRequestSchema) |
| 543 | .action(async ( |
| 544 | const { orgSlug, |
| 545 | parsedInput |
| 546 | const { orgMember, |
| 547 | const organization = |
| 548 | if (!organization) |
| 549 | throw new Error( |
| 550 | } |
| 551 | const hasProjectAccess = |
| 552 | organization.id, |
| 553 | projectId, |
| 554 | orgMember.userId |
| 555 | ) |
| 556 | if (hasProjectAccess.success |
| 557 | throw new Error( |
| 558 | hasProjectAccess.error ?? |
| 559 | ) |
| 560 | } |
| 561 | const project = |
| 562 | if (!project) |
| 563 | throw new Error( |
| 564 | } |
| 565 | const requirement = |
| 566 | requirementId, |
| 567 | projectId, |
| 568 | await headers() |
| 569 | ) |
| 570 | if (!requirement) |
| 571 | throw new Error( |
| 572 | } |
| 573 | const changeRequest = |
| 574 | changeRequestId, |
| 575 | requirementId |
| 576 | ) |
| 577 | if (!changeRequest) |
| 578 | throw new Error( |
| 579 | } |
| 580 | const settings = |
| 581 | organization.id, |
| 582 | projectId |
| 583 | ) |
| 584 | if (settings.clientInvolvement |
| 585 | throw new Error( |
| 586 | 'Client involvement is disabled for requirements in this project' |
| 587 | ) |
| 588 | } |
| 589 | if (resolution === |
| 590 | if (!role.authorize |
| 591 | throw new Error( |
| 592 | } |
| 593 | } else if (!role |
| 594 | throw new Error( |
| 595 | } |
| 596 | const updatedChangeRequest = |
| 597 | const [updatedChangeRequest] |
| 598 | .update(requirementChangeRequests) |
| 599 | .set({ status: |
| 600 | .returning() |
| 601 | .where(eq(requirementChangeRequests |
| 602 | return updatedChangeRequest |
| 603 | }) |
| 604 | return updatedChangeRequest |
| 605 | }) |
| 606 | |
| 607 | export const addThreadReplyAction = |
| 608 | .inputSchema(addThreadReplySchema) |
| 609 | .action(async ( |
| 610 | const { orgSlug, |
| 611 | parsedInput |
| 612 | const { orgMember, |
| 613 | const organization = |
| 614 | if (!organization) |
| 615 | throw new Error( |
| 616 | } |
| 617 | const hasProjectAccess = |
| 618 | organization.id, |
| 619 | projectId, |
| 620 | orgMember.userId |
| 621 | ) |
| 622 | if (hasProjectAccess.success |
| 623 | throw new Error( |
| 624 | hasProjectAccess.error ?? |
| 625 | ) |
| 626 | } |
| 627 | if (!role.authorize |
| 628 | throw new Error( |
| 629 | } |
| 630 | const project = |
| 631 | if (!project) |
| 632 | throw new Error( |
| 633 | } |
| 634 | const requirement = |
| 635 | requirementId, |
| 636 | projectId, |
| 637 | await headers() |
| 638 | ) |
| 639 | if (!requirement) |
| 640 | throw new Error( |
| 641 | } |
| 642 | const thread = |
| 643 | if (!thread) |
| 644 | throw new Error( |
| 645 | } |
| 646 | const threadMessage = |
| 647 | const [threadMessage] |
| 648 | .insert(threadMessages) |
| 649 | .values({ |
| 650 | threadId, |
| 651 | body: replyBody, |
| 652 | authorMemberId: orgMember.id |
| 653 | }) |
| 654 | .returning() |
| 655 | const receipients = |
| 656 | const emailsToSend: |
| 657 | for (const recipient |
| 658 | emailsToSend.push({ |
| 659 | email: recipient.users |
| 660 | name: recipient.users |
| 661 | }) |
| 662 | } |
| 663 | await sendEmailsToRecipients(emailsToSend |
| 664 | const html = |
| 665 | ThreadNewMessageEmail({ |
| 666 | recipientName: recipient.name |
| 667 | senderName: orgMember.user |
| 668 | threadTitle: thread.selectedText |
| 669 | messagePreview: replyBody, |
| 670 | contextType: 'requirement', |
| 671 | contextName: requirement.title |
| 672 | projectName: project.name |
| 673 | orgSlug, |
| 674 | projectSlug: project.slug |
| 675 | threadLink: `${baseUrl} |
| 676 | }) |
| 677 | ) |
| 678 | return { |
| 679 | to: recipient.email |
| 680 | subject: `New reply in thread "${thread |
| 681 | html, |
| 682 | } |
| 683 | }) |
| 684 | return threadMessage |
| 685 | }) |
| 686 | return threadMessage |
| 687 | }) |