ActionEmailService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 App\Services\Notification\Traits\ObjectActionApprovalNotificationUserHelper;
  19. use Illuminate\Contracts\Mail\Mailable;
  20. use Illuminate\Support\Facades\Mail;
  21. class ActionEmailService
  22. {
  23. use ObjectActionApprovalNotificationUserHelper;
  24. protected ObjectAction $objectAction;
  25. protected array $actions = [];
  26. public function __construct(
  27. protected Action $action
  28. )
  29. {
  30. $this->objectAction = ObjectAction::tryFrom($this->action->action);
  31. }
  32. public function send()
  33. {
  34. if (! ConfigRepository::openEmailNotification()) {
  35. return;
  36. }
  37. ConfigRepository::emailDynamicSetting();
  38. $actionObjectType = ActionObjectType::tryFrom($this->action->object_type);
  39. $actionObjectModel = $actionObjectType->modelBuilder()->find($this->action->object_id);
  40. $this->actions = ActionRepository::objectEmailActions($actionObjectType, $actionObjectModel->id, $this->action->id);
  41. match ($actionObjectType) {
  42. ActionObjectType::REQUIREMENT => $this->requirement($actionObjectModel),
  43. ActionObjectType::TASK => $this->task($actionObjectModel),
  44. ActionObjectType::CONTAINER => $this->container($actionObjectModel),
  45. ActionObjectType::PROJECT => $this->project($actionObjectModel),
  46. default => null,
  47. };
  48. }
  49. protected function project(Project $project)
  50. {
  51. $this->dispatch([], new ProjectAction($project, $this->objectAction, $this->actions));
  52. }
  53. protected function container(Container $container)
  54. {
  55. $this->dispatch($container->mailto ?? [], new ContainerAction($container, $this->objectAction, $this->actions));
  56. }
  57. protected function requirement(Requirement $requirement)
  58. {
  59. $this->dispatch($requirement->mailto ?? [], new RequirementAction($requirement, $this->objectAction, $this->actions));
  60. }
  61. protected function task(Task $task)
  62. {
  63. $userIds = array_filter([$task->assign, ...$task->mailto]);
  64. $this->dispatch($userIds, new TaskAction($task, $this->objectAction, $this->actions));
  65. }
  66. protected function dispatch(array $userIds, Mailable $mailable)
  67. {
  68. if ($this->objectAction->isApproval()) {
  69. $approval = Approval::query()->find($this->action->additional_id);
  70. if (! $approval) {
  71. return;
  72. }
  73. list($approvalUserIDs, $notifiedUserIDs) = $this->objectActionApprovalNotificationUserIDs($approval, $this->action);
  74. $userIds = [...$approvalUserIDs, ...$notifiedUserIDs];
  75. }
  76. $userIds = array_filter(array_unique($userIds));
  77. if (! $userIds) {
  78. return;
  79. }
  80. $users = User::query()->whereIn("id", $userIds)->get();
  81. if ($users->isEmpty()) {
  82. return;
  83. }
  84. foreach ($users as $user) {
  85. Mail::to($user)->send($mailable);
  86. }
  87. }
  88. }