7 changed files with 182 additions and 2 deletions
-
8app/Entities/Projects/Project.php
-
12app/Entities/Users/Activity.php
-
20app/Events/Projects/Updated.php
-
61app/Listeners/Projects/LogProjectUpdateActivity.php
-
4app/Providers/EventServiceProvider.php
-
40database/migrations/2021_03_05_221708_create_user_activities_table.php
-
39tests/Unit/Models/ActivityTest.php
@ -0,0 +1,12 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Entities\Users; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
|
||||
|
class Activity extends Model |
||||
|
{ |
||||
|
protected $table = 'user_activities'; |
||||
|
|
||||
|
protected $fillable = ['type', 'parent_id', 'user_id', 'object_id', 'object_type', 'data']; |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Events\Projects; |
||||
|
|
||||
|
use App\Entities\Projects\Project; |
||||
|
|
||||
|
class Updated |
||||
|
{ |
||||
|
public $project; |
||||
|
|
||||
|
/** |
||||
|
* Create a new event instance. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function __construct(Project $project) |
||||
|
{ |
||||
|
$this->project = $project; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Listeners\Projects; |
||||
|
|
||||
|
use App\Entities\Users\Activity; |
||||
|
use App\Events\Projects\Updated; |
||||
|
|
||||
|
class LogProjectUpdateActivity |
||||
|
{ |
||||
|
/** |
||||
|
* Handle the event. |
||||
|
* |
||||
|
* @param Updated $event |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function handle(Updated $event) |
||||
|
{ |
||||
|
// dd($event->project->fresh()->toArray(), $event->project->getOriginal(), $event->project->getDirty());
|
||||
|
$project = $event->project; |
||||
|
// dd($project->getOriginal(), $project->getChanges());
|
||||
|
$originalProject = $project->getOriginal(); |
||||
|
$attributeChanges = $project->getChanges(); |
||||
|
$attributeKeys = array_keys($project->getChanges()); |
||||
|
// dd($attributeChanges, $attributeKeys);
|
||||
|
|
||||
|
$activityEntry = [ |
||||
|
'type' => 'project_updated', |
||||
|
'parent_id' => null, |
||||
|
'user_id' => auth()->id(), |
||||
|
'object_id' => $project->id, |
||||
|
'object_type' => 'projects', |
||||
|
'data' => json_encode([ |
||||
|
'before' => $this->getBeforeValues($originalProject, $attributeKeys), |
||||
|
'after' => $this->getAfterValues($project->toArray(), $attributeKeys), |
||||
|
'notes' => null, |
||||
|
]), |
||||
|
]; |
||||
|
|
||||
|
Activity::create($activityEntry); |
||||
|
} |
||||
|
|
||||
|
private function getBeforeValues(array $originalProject, array $attributeKeys) |
||||
|
{ |
||||
|
$beforeValues = []; |
||||
|
foreach ($attributeKeys as $attributeKey) { |
||||
|
$beforeValues[$attributeKey] = $originalProject[$attributeKey]; |
||||
|
} |
||||
|
|
||||
|
return $beforeValues; |
||||
|
} |
||||
|
|
||||
|
private function getAfterValues(array $project, array $attributeKeys) |
||||
|
{ |
||||
|
$afterValues = []; |
||||
|
foreach ($attributeKeys as $attributeKey) { |
||||
|
$afterValues[$attributeKey] = $project[$attributeKey]; |
||||
|
} |
||||
|
|
||||
|
return $afterValues; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
<?php |
||||
|
|
||||
|
use Illuminate\Database\Migrations\Migration; |
||||
|
use Illuminate\Database\Schema\Blueprint; |
||||
|
use Illuminate\Support\Facades\Schema; |
||||
|
|
||||
|
class CreateUserActivitiesTable extends Migration |
||||
|
{ |
||||
|
/** |
||||
|
* Run the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function up() |
||||
|
{ |
||||
|
Schema::create('user_activities', function (Blueprint $table) { |
||||
|
$table->increments('id'); |
||||
|
$table->string('type'); |
||||
|
$table->unsignedInteger('parent_id')->nullable()->comment('Parent Activity ID'); |
||||
|
$table->unsignedInteger('user_id')->nullable(); |
||||
|
$table->unsignedInteger('object_id'); |
||||
|
$table->string('object_type', 60); |
||||
|
$table->text('data')->nullable(); |
||||
|
$table->timestamps(); |
||||
|
|
||||
|
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); |
||||
|
$table->foreign('parent_id')->references('id')->on('activities')->onDelete('cascade'); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Reverse the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function down() |
||||
|
{ |
||||
|
Schema::dropIfExists('user_activities'); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,39 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace Tests\Unit\Models; |
||||
|
|
||||
|
use App\Entities\Projects\Project; |
||||
|
use Illuminate\Foundation\Testing\RefreshDatabase; |
||||
|
use Tests\TestCase; |
||||
|
|
||||
|
class ActivityTest extends TestCase |
||||
|
{ |
||||
|
use RefreshDatabase; |
||||
|
|
||||
|
/** @test */ |
||||
|
public function it_records_project_data_update() |
||||
|
{ |
||||
|
$admin = $this->adminUserSigningIn(); |
||||
|
$project = factory(Project::class)->create(['name' => 'New Project']); |
||||
|
|
||||
|
$project->name = 'Updated project'; |
||||
|
$project->save(); |
||||
|
|
||||
|
$this->seeInDatabase('user_activities', [ |
||||
|
'type' => 'project_updated', |
||||
|
'parent_id' => null, |
||||
|
'user_id' => $admin->id, |
||||
|
'object_id' => $project->id, |
||||
|
'object_type' => 'projects', |
||||
|
'data' => json_encode([ |
||||
|
'before' => [ |
||||
|
'name' => 'New Project', |
||||
|
], |
||||
|
'after' => [ |
||||
|
'name' => 'Updated project', |
||||
|
], |
||||
|
'notes' => null, |
||||
|
]), |
||||
|
]); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue