ActionEmailService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Services\Notification\ActionEmail;
  3. use App\Mail\ContainerAction;
  4. use App\Mail\ProjectAction;
  5. use App\Mail\RequirementAction;
  6. use App\Mail\TaskAction;
  7. use App\Models\Action;
  8. use App\Models\Approval;
  9. use App\Models\Container;
  10. use App\Models\Enums\ActionObjectType;
  11. use App\Models\Enums\ObjectAction;
  12. use App\Models\Project;
  13. use App\Models\Requirement;
  14. use App\Models\Task;
  15. use App\Models\User;
  16. use App\Repositories\ActionRepository;
  17. use App\Repositories\ConfigRepository;
  18. use Illuminate\Contracts\Mail\Mailable;
  19. use Illuminate\Support\Facades\Mail;
  20. class ActionEmailService
  21. {
  22. protected ObjectAction $objectAction;
  23. protected array $actions = [];
  24. public function __construct(
  25. protected Action $action
  26. )
  27. {
  28. $this->objectAction = ObjectAction::tryFrom($this->action->action);
  29. }
  30. public function send()
  31. {
  32. if (! ConfigRepository::openEmailNotification()) {
  33. return;
  34. }
  35. ConfigRepository::emailDynamicSetting();
  36. $actionObjectType = ActionObjectType::tryFrom($this->action->object_type);
  37. $actionObjectModel = $actionObjectType->modelBuilder()->find($this->action->object_id);
  38. $this->actions = ActionRepository::objectEmailActions($actionObjectType, $actionObjectModel->id, $this->action->id);
  39. match ($actionObjectType) {
  40. ActionObjectType::REQUIREMENT => $this->requirement($actionObjectModel),
  41. ActionObjectType::TASK => $this->task($actionObjectModel),
  42. ActionObjectType::CONTAINER => $this->container($actionObjectModel),
  43. ActionObjectType::PROJECT => $this->project($actionObjectModel),
  44. default => null,
  45. };
  46. }
  47. protected function project(Project $project)
  48. {
  49. $this->dispatch([], new ProjectAction($project, $this->objectAction, $this->actions));
  50. }
  51. protected function container(Container $container)
  52. {
  53. $this->dispatch($container->mailto, new ContainerAction($container, $this->objectAction, $this->actions));
  54. }
  55. protected function requirement(Requirement $requirement)
  56. {
  57. $this->dispatch($requirement->mailto, new RequirementAction($requirement, $this->objectAction, $this->actions));
  58. }
  59. protected function task(Task $task)
  60. {
  61. $userIds = array_filter([$task->assign, ...$task->mailto]);
  62. $this->dispatch($userIds, new TaskAction($task, $this->objectAction, $this->actions));
  63. }
  64. protected function dispatch(array $userIds, Mailable $mailable)
  65. {
  66. if ($this->objectAction->isApproval()) {
  67. $approval = Approval::query()->find($this->action->additional_id);
  68. if (! $approval) {
  69. return;
  70. }
  71. $approvalFlow = $approval->approvalFlow()->first();
  72. dump($this->action->extra_fields);
  73. $nextApprovalUsers = $approvalFlow?->nodes[$this->action->extra_fields['next']]['approval_users'] ?? [];
  74. $userIds = match ($this->objectAction) {
  75. ObjectAction::APPROVAL_REQUEST => $nextApprovalUsers,
  76. ObjectAction::APPROVED_TO_NEXT_NODE => [$approval->created_by, ...$nextApprovalUsers],
  77. ObjectAction::APPROVAL_APPROVED, ObjectAction::APPROVAL_REJECTED => [$approval->created_by],
  78. default => [],
  79. };
  80. }
  81. $userIds = array_filter(array_unique($userIds));
  82. if (! $userIds) {
  83. return;
  84. }
  85. $users = User::query()->whereIn("id", $userIds)->get();
  86. if ($users->isEmpty()) {
  87. return;
  88. }
  89. foreach ($users as $user) {
  90. Mail::to($user)->send($mailable);
  91. }
  92. }
  93. }