12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace App\Services\Notification\ActionEmail;
- use App\Mail\ContainerAction;
- use App\Mail\RequirementAction;
- use App\Mail\TaskAction;
- use App\Models\Action;
- use App\Models\Container;
- use App\Models\Enums\ActionObjectType;
- use App\Models\Enums\ObjectAction;
- use App\Models\Requirement;
- use App\Models\Task;
- use App\Models\User;
- use App\Repositories\ActionRepository;
- use App\Repositories\ConfigRepository;
- use Illuminate\Contracts\Mail\Mailable;
- use Illuminate\Support\Facades\Mail;
- class ActionEmailService
- {
- protected ObjectAction $objectAction;
- protected array $actions = [];
- public function __construct(
- protected Action $action
- )
- {
- $this->objectAction = ObjectAction::tryFrom($this->action->action);
- }
- public function send()
- {
- if (! ConfigRepository::openEmailNotification()) {
- return;
- }
- ConfigRepository::emailDynamicSetting();
- $actionObjectType = ActionObjectType::tryFrom($this->action->object_type);
- $actionObjectModel = $actionObjectType->modelBuilder()->find($this->action->object_id);
- $this->actions = ActionRepository::objectEmailActions($actionObjectType, $actionObjectModel->id, $this->action->id);
- match ($actionObjectType) {
- ActionObjectType::REQUIREMENT => $this->requirement($actionObjectModel),
- ActionObjectType::TASK => $this->task($actionObjectModel),
- ActionObjectType::CONTAINER => $this->container($actionObjectModel),
- };
- }
- protected function container(Container $container)
- {
- $this->dispatch($container->mailto, new ContainerAction($container, $this->objectAction, $this->actions));
- }
- protected function requirement(Requirement $requirement)
- {
- $this->dispatch($requirement->mailto, new RequirementAction($requirement, $this->objectAction, $this->actions));
- }
- protected function task(Task $task)
- {
- $userIds = array_filter([$task->assign, ...$task->mailto]);
- $this->dispatch($userIds, new TaskAction($task, $this->objectAction, $this->actions));
- }
- protected function dispatch(array $userIds, Mailable $mailable)
- {
- $users = User::query()->whereIn("id", $userIds)->get();
- if ($users->isEmpty()) {
- return;
- }
- foreach ($users as $user) {
- Mail::to($user)->send($mailable);
- }
- }
- }
|