Browse Source

Add task update activity log

pull/72/head
Nafies Luthfi 5 years ago
parent
commit
27fc9a2b29
  1. 1
      app/Entities/Projects/Task.php
  2. 15
      app/Events/Tasks/Updated.php
  3. 52
      app/Listeners/Tasks/LogTaskUpdateActivity.php
  4. 3
      app/Providers/EventServiceProvider.php
  5. 28
      tests/Unit/Models/ActivityTest.php

1
app/Entities/Projects/Task.php

@ -13,6 +13,7 @@ class Task extends Model
*/
protected $dispatchesEvents = [
'created' => 'App\Events\Tasks\Created',
'updated' => 'App\Events\Tasks\Updated',
];
protected $guarded = ['id', 'created_at', 'updated_at'];

15
app/Events/Tasks/Updated.php

@ -0,0 +1,15 @@
<?php
namespace App\Events\Tasks;
use App\Entities\Projects\Task;
class Updated
{
public $task;
public function __construct(Task $task)
{
$this->task = $task;
}
}

52
app/Listeners/Tasks/LogTaskUpdateActivity.php

@ -0,0 +1,52 @@
<?php
namespace App\Listeners\Tasks;
use App\Entities\Users\Activity;
use App\Events\Tasks\Updated;
class LogTaskUpdateActivity
{
public function handle(Updated $event)
{
$task = $event->task;
$originalTask = $task->getOriginal();
$attributeChanges = $task->getChanges();
$attributeKeys = array_keys($task->getChanges());
$activityEntry = [
'type' => 'task_updated',
'parent_id' => null,
'user_id' => auth()->id(),
'object_id' => $task->id,
'object_type' => 'tasks',
'data' => [
'before' => $this->getBeforeValues($originalTask, $attributeKeys),
'after' => $this->getAfterValues($task->toArray(), $attributeKeys),
'notes' => null,
],
];
Activity::create($activityEntry);
}
private function getBeforeValues(array $originalTask, array $attributeKeys)
{
$beforeValues = [];
foreach ($attributeKeys as $attributeKey) {
$beforeValues[$attributeKey] = $originalTask[$attributeKey];
}
return $beforeValues;
}
private function getAfterValues(array $task, array $attributeKeys)
{
$afterValues = [];
foreach ($attributeKeys as $attributeKey) {
$afterValues[$attributeKey] = $task[$attributeKey];
}
return $afterValues;
}
}

3
app/Providers/EventServiceProvider.php

@ -31,6 +31,9 @@ class EventServiceProvider extends ServiceProvider
'App\Events\Tasks\Created' => [
'App\Listeners\Tasks\LogTaskCreationActivity',
],
'App\Events\Tasks\Updated' => [
'App\Listeners\Tasks\LogTaskUpdateActivity',
],
];
/**

28
tests/Unit/Models/ActivityTest.php

@ -134,4 +134,32 @@ class ActivityTest extends TestCase
'data' => null,
]);
}
/** @test */
public function it_records_task_data_update_activities()
{
$admin = $this->adminUserSigningIn();
$project = factory(Project::class)->create();
$job = factory(Job::class)->create(['project_id' => $project->id]);
$task = factory(Task::class)->create([
'name' => 'New Task',
'job_id' => $job->id,
]);
$task->name = 'Updated task';
$task->save();
$this->seeInDatabase('user_activities', [
'type' => 'task_updated',
'parent_id' => null,
'user_id' => $admin->id,
'object_id' => $task->id,
'object_type' => 'tasks',
'data' => json_encode([
'before' => ['name' => 'New Task'],
'after' => ['name' => 'Updated task'],
'notes' => null,
]),
]);
}
}
Loading…
Cancel
Save