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