diff --git a/database/migrations/2023_11_17_002135_add_audited_by_column_to_approvals_table.php b/database/migrations/2023_11_17_002135_add_audited_by_column_to_approvals_table.php index 90e890c..b350fbe 100644 --- a/database/migrations/2023_11_17_002135_add_audited_by_column_to_approvals_table.php +++ b/database/migrations/2023_11_17_002135_add_audited_by_column_to_approvals_table.php @@ -4,7 +4,8 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration { +return new class extends Migration +{ /** * Run the migrations. */ diff --git a/src/Concerns/MustBeApproved.php b/src/Concerns/MustBeApproved.php index 183353a..e5d9aff 100644 --- a/src/Concerns/MustBeApproved.php +++ b/src/Concerns/MustBeApproved.php @@ -58,7 +58,6 @@ protected static function insertApprovalRequest($model) if ($noNeedToProceed) { return false; } - } /** diff --git a/src/Events/ModelApproved.php b/src/Events/ModelApproved.php index 9200565..46af1d9 100644 --- a/src/Events/ModelApproved.php +++ b/src/Events/ModelApproved.php @@ -2,13 +2,17 @@ namespace Cjmellor\Approval\Events; +use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Events\Dispatchable; class ModelApproved { use Dispatchable; - public function __construct() - { + public function __construct( + public Model $approval, + public Authenticatable|null $user, + ) { } } diff --git a/src/Events/ModelRejected.php b/src/Events/ModelRejected.php index 9d76bc7..d80c54f 100644 --- a/src/Events/ModelRejected.php +++ b/src/Events/ModelRejected.php @@ -2,13 +2,17 @@ namespace Cjmellor\Approval\Events; +use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Events\Dispatchable; class ModelRejected { use Dispatchable; - public function __construct() - { + public function __construct( + public Model $approval, + public Authenticatable|null $user, + ) { } } diff --git a/src/Events/ModelSetPending.php b/src/Events/ModelSetPending.php index b137580..fb1bb11 100644 --- a/src/Events/ModelSetPending.php +++ b/src/Events/ModelSetPending.php @@ -2,13 +2,17 @@ namespace Cjmellor\Approval\Events; +use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Events\Dispatchable; class ModelSetPending { use Dispatchable; - public function __construct() - { + public function __construct( + public Model $approval, + public Authenticatable|null $user, + ) { } } diff --git a/src/Scopes/ApprovalStateScope.php b/src/Scopes/ApprovalStateScope.php index 507f09f..46b35e0 100644 --- a/src/Scopes/ApprovalStateScope.php +++ b/src/Scopes/ApprovalStateScope.php @@ -127,9 +127,9 @@ protected function addApprove(Builder $builder): void protected function updateApprovalState(Builder $builder, ApprovalStatus $state): int { match ($state) { - ApprovalStatus::Approved => Event::dispatch(new ModelApproved()), - ApprovalStatus::Pending => Event::dispatch(new ModelSetPending()), - ApprovalStatus::Rejected => Event::dispatch(new ModelRejected()), + ApprovalStatus::Approved => Event::dispatch(new ModelApproved($builder->getModel(), auth()->user())), + ApprovalStatus::Pending => Event::dispatch(new ModelSetPending($builder->getModel(), auth()->user())), + ApprovalStatus::Rejected => Event::dispatch(new ModelRejected($builder->getModel(), auth()->user())), }; $auditedData = ['state' => $state]; diff --git a/tests/Feature/Scopes/ApprovalStateScopeTest.php b/tests/Feature/Scopes/ApprovalStateScopeTest.php index 6fba2d3..119c4fa 100644 --- a/tests/Feature/Scopes/ApprovalStateScopeTest.php +++ b/tests/Feature/Scopes/ApprovalStateScopeTest.php @@ -7,6 +7,7 @@ use Cjmellor\Approval\Models\Approval; use Cjmellor\Approval\Tests\Models\FakeModel; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Schema; test('Check if an Approval Model is approved', closure: function (): void { $this->approvalData = [ @@ -201,7 +202,7 @@ }); test(description: 'The model approver is listed correctly', closure: function () { - Schema::create('fake_users', callback: function (\Illuminate\Database\Schema\Blueprint $table) { + Schema::create('fake_users', callback: function (Illuminate\Database\Schema\Blueprint $table) { $table->id(); $table->string(column: 'name'); $table->string(column: 'email')->unique(); @@ -211,7 +212,9 @@ class FakeUser extends \Illuminate\Foundation\Auth\User { protected $guarded = []; + protected $table = 'fake_users'; + public $timestamps = false; }