123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace App\Services\Notification\ActionEmail;
- use App\Mail\ContainerAction;
- use App\Mail\ProjectAction;
- use App\Mail\RequirementAction;
- use App\Mail\TaskAction;
- use App\Models\Action;
- use App\Models\Approval;
- use App\Models\Container;
- use App\Models\Enums\ActionObjectType;
- use App\Models\Enums\ObjectAction;
- use App\Models\Project;
- use App\Models\Requirement;
- use App\Models\Task;
- use App\Models\User;
- use App\Repositories\ActionRepository;
- use App\Repositories\ConfigRepository;
- use App\Services\Notification\Traits\ObjectActionApprovalNotificationUserHelper;
- use Illuminate\Contracts\Mail\Mailable;
- use Illuminate\Support\Facades\Mail;
- class ActionEmailService
- {
- use ObjectActionApprovalNotificationUserHelper;
- 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),
- ActionObjectType::PROJECT => $this->project($actionObjectModel),
- default => null,
- };
- }
- protected function project(Project $project)
- {
- $this->dispatch([], new ProjectAction($project, $this->objectAction, $this->actions));
- }
- 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)
- {
- if ($this->objectAction->isApproval()) {
- $approval = Approval::query()->find($this->action->additional_id);
- if (! $approval) {
- return;
- }
- list($approvalUserIDs, $notifiedUserIDs) = $this->objectActionApprovalNotificationUserIDs($approval, $this->action);
- $userIds = [...$approvalUserIDs, ...$notifiedUserIDs];
- }
- $userIds = array_filter(array_unique($userIds));
- if (! $userIds) {
- return;
- }
- $users = User::query()->whereIn("id", $userIds)->get();
- if ($users->isEmpty()) {
- return;
- }
- foreach ($users as $user) {
- Mail::to($user)->send($mailable);
- }
- }
- }
|