Browse Source

渲染任务邮件

moell 9 months ago
parent
commit
9795ac5b0e

+ 3 - 3
app/Mail/TaskAction.php

@@ -18,9 +18,9 @@ class TaskAction extends Mailable
     /**
      * Create a new message instance.
      */
-    public function __construct(protected Task $task, protected ObjectAction $objectAction)
+    public function __construct(public Task $task, protected ObjectAction $objectAction, public array $actions = [])
     {
-        //
+
     }
 
     /**
@@ -29,7 +29,7 @@ class TaskAction extends Mailable
     public function envelope(): Envelope
     {
         return new Envelope(
-            subject: 'Task Action',
+            subject: $this->task->name,
         );
     }
 

+ 27 - 0
app/Repositories/ActionRepository.php

@@ -147,6 +147,33 @@ class ActionRepository
         return $items;
     }
 
+    public static function objectEmailActions(ActionObjectType $actionObjectType, string $objectId, int $lastActionId): array
+    {
+        $actions = Action::query()
+            ->with(['createdBy'])
+            ->where("object_type", $actionObjectType->value)
+            ->where("object_id", $objectId)
+            ->where("id", "<=", $lastActionId)
+            ->orderByDesc("created_at")
+            ->limit(5)
+            ->get();
+
+        return self::actionsFormat($actions);
+
+    }
+
+    protected static function actionsFormat(Collection $actions): array
+    {
+        $objectNames = self::objectNamesGroupByType($actions);
+
+        $items = [];
+        foreach ($actions as $action) {
+            $items[] = self::actionFormat($action->toArray(), $objectNames);
+        }
+
+        return $items;
+    }
+
     public static function actionFormat(array $action, array $objectNames): array
     {
         $labelKey = sprintf("action-labels.label.%s", $action['action']);

+ 6 - 1
app/Services/Notification/ActionEmail/ActionEmailService.php

@@ -10,6 +10,7 @@ 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;
@@ -18,6 +19,8 @@ class ActionEmailService
 {
     protected ObjectAction $objectAction;
 
+    protected array $actions = [];
+
     public function __construct(
         protected Action $action
     )
@@ -37,6 +40,8 @@ class ActionEmailService
 
         $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),
@@ -52,7 +57,7 @@ class ActionEmailService
     {
         $userIds = array_filter([$task->assign, ...$task->mailto]);
 
-        $this->dispatch($userIds, new TaskAction($task, $this->objectAction));
+        $this->dispatch($userIds, new TaskAction($task, $this->objectAction, $this->actions));
     }
 
     protected function dispatch(array $userIds, Mailable $mailable)

+ 3 - 3
resources/views/components/email/history.blade.php

@@ -4,13 +4,13 @@
             <h2>History</h2>
         </td>
     </tr>
-    @for($i = 1; $i <= 5; $i++)
+    @foreach($actions as $index => $action)
         <tr>
             <td  align="left" style="padding: 0 0 10px 10px;">
-                {{ $i }}. {{ uniqid() }}
+                {{ $index + 1 }}. {{ sprintf("%s %s by %s", $action['created_at'], $action['action_label'], data_get($action, 'created_by.name')) }}
             </td>
         </tr>
-    @endfor
+    @endforeach
     <tr>
         <td  align="left" style="padding: 0 0 10px 10px;">
             <a href="">More...</a>

+ 14 - 9
resources/views/emails/actions/task.blade.php

@@ -1,24 +1,29 @@
 <x-mail::message>
-# Task Name: 22116HK/A.I.P. No. AI-MC-022 (AR) Vibration Monitoring System
+# Task Name: {{ $task->name }}
 
-<x-mail::panel>{{ date("Y-m-d H:i:s") }} approved by admin</x-mail::panel>
+<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>
 
-## Approval Status
-Approved
+## Task Status
+{{ __(sprintf("model-enums.task.status.%s", $task->status)) }}
 
+@if($task->state)
 ## State
-Shared
+{{ $task->state }}
+@endif
 
 ## Assign
-admin
+{{ $task?->assignTo?->name }}
 
+@if($task->suitability)
 ## Suitability
-S3
+{{ $task->suitability }}
+@endif
 
 ## Note
-This is note
 
-<x-email.history></x-email.history>
+{{ $task->description }}
+
+<x-email.history :actions="$actions"></x-email.history>
 
 Thanks,<br>
 {{ config('app.name') }}

+ 8 - 0
routes/web.php

@@ -16,3 +16,11 @@ use Illuminate\Support\Facades\Route;
 Route::get('/', function () {
     return view('welcome');
 });
+
+Route::middleware(['auth:sanctum'])->group(function () {
+    Route::get('/email', function() {
+        $task = \App\Models\Task::query()->first();
+
+        return (new \App\Mail\TaskAction($task, \App\Models\Enums\ObjectAction::DONE))->render();
+    });
+});