Browse Source

审批支持邮件通知

moell 8 months ago
parent
commit
5bafeb93d5

+ 58 - 0
app/Mail/ProjectAction.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Mail;
+
+use App\Models\Container;
+use App\Models\Enums\ObjectAction;
+use App\Models\Project;
+use App\Models\Task;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Mail\Mailable;
+use Illuminate\Mail\Mailables\Content;
+use Illuminate\Mail\Mailables\Envelope;
+use Illuminate\Queue\SerializesModels;
+
+class ProjectAction extends Mailable
+{
+    use Queueable, SerializesModels;
+
+    /**
+     * Create a new message instance.
+     */
+    public function __construct(public Project $project, protected ObjectAction $objectAction, public array $actions = [])
+    {
+
+    }
+
+    /**
+     * Get the message envelope.
+     */
+    public function envelope(): Envelope
+    {
+        return new Envelope(
+            subject: $this->project->name,
+        );
+    }
+
+    /**
+     * Get the message content definition.
+     */
+    public function content(): Content
+    {
+        return new Content(
+            //view: 'view.name',
+            markdown: 'emails.actions.project',
+        );
+    }
+
+    /**
+     * Get the attachments for the message.
+     *
+     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
+     */
+    public function attachments(): array
+    {
+        return [];
+    }
+}

+ 34 - 0
app/Services/Notification/ActionEmail/ActionEmailService.php

@@ -3,12 +3,15 @@
 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;
@@ -48,10 +51,17 @@ class ActionEmailService
             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));
@@ -71,6 +81,30 @@ class ActionEmailService
 
     protected function dispatch(array $userIds, Mailable $mailable)
     {
+        if ($this->objectAction->isApproval()) {
+            $approval = Approval::query()->find($this->action->additional_id);
+            if (! $approval) {
+                return;
+            }
+
+            $approvalFlow = $approval->approvalFlow()->first();
+
+            $nextApprovalUsers = $approvalFlow?->nodes[$this->action->extra_fields['next']]['approval_users'] ?? [];
+
+            $userIds = match ($this->objectAction) {
+                ObjectAction::APPROVAL_REQUEST => $nextApprovalUsers,
+                ObjectAction::APPROVED_TO_NEXT_NODE => [$approval->created_by, ...$nextApprovalUsers],
+                ObjectAction::APPROVAL_APPROVED, ObjectAction::APPROVAL_REJECTED => [$approval->created_by],
+                default => [],
+            };
+        }
+
+        $userIds = array_filter(array_unique($userIds));
+
+        if (! $userIds) {
+            return;
+        }
+
         $users = User::query()->whereIn("id", $userIds)->get();
         if ($users->isEmpty()) {
             return;

+ 10 - 0
resources/views/emails/actions/project.blade.php

@@ -0,0 +1,10 @@
+<x-mail::message>
+# Project Name: {{ $project->name }}
+
+<x-mail::panel>{{ sprintf("%s %s by %s", data_get($actions, '0.created_at'), data_get($actions, '0.action_label'), data_get($actions, '0.created_by.name')) }}</x-mail::panel>
+
+<x-email.history :actions="$actions"></x-email.history>
+
+Thanks,<br>
+{{ config('app.name') }}
+</x-mail::message>