feat: Implement value change logging functionality with models, traits, and migration; add tests for logging behavior and value change mapping
This commit is contained in:
9
app/Domains/Logging/Enums/ValueChangeActorType.php
Normal file
9
app/Domains/Logging/Enums/ValueChangeActorType.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Logging\Enums;
|
||||
|
||||
enum ValueChangeActorType: string
|
||||
{
|
||||
case User = 'user';
|
||||
case System = 'system';
|
||||
}
|
||||
62
app/Domains/Logging/Models/Concerns/LogsValueChanges.php
Normal file
62
app/Domains/Logging/Models/Concerns/LogsValueChanges.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Logging\Models\Concerns;
|
||||
|
||||
use App\Domains\Logging\Enums\ValueChangeActorType;
|
||||
use App\Domains\Logging\Models\ValueChange;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use LogicException;
|
||||
|
||||
trait LogsValueChanges
|
||||
{
|
||||
public static function bootLogsValueChanges(): void
|
||||
{
|
||||
static::updated(function (Model $model): void {
|
||||
$changedAttributes = array_values(array_intersect(
|
||||
$model->getLoggedAttributes(),
|
||||
array_keys($model->getChanges()),
|
||||
));
|
||||
|
||||
if ($changedAttributes === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$userId = Auth::id();
|
||||
$actorType = $userId === null
|
||||
? ValueChangeActorType::System
|
||||
: ValueChangeActorType::User;
|
||||
|
||||
foreach ($changedAttributes as $attribute) {
|
||||
$model->valueChanges()->create([
|
||||
'attribute' => $attribute,
|
||||
'old_value' => $model->getRawOriginal($attribute),
|
||||
'new_value' => $model->getAttributes()[$attribute] ?? null,
|
||||
'changed_at' => now(),
|
||||
'actor_type' => $actorType,
|
||||
'user_id' => $userId,
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** @return array<int, string> */
|
||||
public function getLoggedAttributes(): array
|
||||
{
|
||||
if (! property_exists($this, 'loggedAttributes')) {
|
||||
throw new LogicException(sprintf(
|
||||
'The [%s] model must define a $loggedAttributes property.',
|
||||
static::class,
|
||||
));
|
||||
}
|
||||
|
||||
return array_values(array_unique($this->loggedAttributes));
|
||||
}
|
||||
|
||||
/** @return MorphMany<ValueChange, $this> */
|
||||
public function valueChanges(): MorphMany
|
||||
{
|
||||
return $this->morphMany(ValueChange::class, 'trackable');
|
||||
}
|
||||
}
|
||||
47
app/Domains/Logging/Models/ValueChange.php
Normal file
47
app/Domains/Logging/Models/ValueChange.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Logging\Models;
|
||||
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Logging\Enums\ValueChangeActorType;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
|
||||
#[Fillable([
|
||||
'trackable_type',
|
||||
'trackable_id',
|
||||
'attribute',
|
||||
'old_value',
|
||||
'new_value',
|
||||
'changed_at',
|
||||
'actor_type',
|
||||
'user_id',
|
||||
])]
|
||||
class ValueChange extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
/** @return MorphTo<Model, $this> */
|
||||
public function trackable(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
/** @return BelongsTo<User, $this> */
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'trackable_id' => 'integer',
|
||||
'changed_at' => 'datetime',
|
||||
'actor_type' => ValueChangeActorType::class,
|
||||
'user_id' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user