From c9e7b7f7cc45486d8b4d11b12fe4a132bca92563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Drunen?= Date: Sat, 9 Dec 2023 23:33:41 +0100 Subject: [PATCH] Associate activity with user by API key as fallback --- app/Events/ImportCreated.php | 10 +++++++++- app/Events/ImportDeleted.php | 10 +++++++++- app/Events/RecurringCreated.php | 2 ++ app/Events/RecurringDeleted.php | 2 ++ app/Events/TagCreated.php | 2 ++ app/Events/TagDeleted.php | 2 ++ app/Events/TransactionCreated.php | 2 ++ app/Events/TransactionDeleted.php | 2 ++ tests/Feature/Api/TransactionControllerTest.php | 11 +++++++++++ 9 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/Events/ImportCreated.php b/app/Events/ImportCreated.php index b7b09f9d..59d4c314 100644 --- a/app/Events/ImportCreated.php +++ b/app/Events/ImportCreated.php @@ -17,9 +17,17 @@ class ImportCreated public function __construct(Import $import) { + $userId = null; + + if (Auth::check()) { + $userId = Auth::user()->id; + } elseif (request()->get('apiKey')) { + $userId = request()->get('apiKey')->user_id; + } + Activity::create([ 'space_id' => $import->space_id, - 'user_id' => Auth::user()->id, + 'user_id' => $userId, 'entity_id' => $import->id, 'entity_type' => 'import', 'action' => 'import.created' diff --git a/app/Events/ImportDeleted.php b/app/Events/ImportDeleted.php index a070c91d..4ddda22e 100644 --- a/app/Events/ImportDeleted.php +++ b/app/Events/ImportDeleted.php @@ -17,9 +17,17 @@ class ImportDeleted public function __construct(Import $import) { + $userId = null; + + if (Auth::check()) { + $userId = Auth::user()->id; + } elseif (request()->get('apiKey')) { + $userId = request()->get('apiKey')->user_id; + } + Activity::create([ 'space_id' => $import->space_id, - 'user_id' => Auth::user()->id, + 'user_id' => $userId, 'entity_id' => $import->id, 'entity_type' => 'import', 'action' => 'import.deleted' diff --git a/app/Events/RecurringCreated.php b/app/Events/RecurringCreated.php index a93e954c..86fcb2b5 100644 --- a/app/Events/RecurringCreated.php +++ b/app/Events/RecurringCreated.php @@ -21,6 +21,8 @@ public function __construct(Recurring $recurring) if (Auth::check()) { $userId = Auth::user()->id; + } elseif (request()->get('apiKey')) { + $userId = request()->get('apiKey')->user_id; } Activity::create([ diff --git a/app/Events/RecurringDeleted.php b/app/Events/RecurringDeleted.php index 33981931..edf3d46b 100644 --- a/app/Events/RecurringDeleted.php +++ b/app/Events/RecurringDeleted.php @@ -21,6 +21,8 @@ public function __construct(Recurring $recurring) if (Auth::check()) { $userId = Auth::user()->id; + } elseif (request()->get('apiKey')) { + $userId = request()->get('apiKey')->user_id; } Activity::create([ diff --git a/app/Events/TagCreated.php b/app/Events/TagCreated.php index 857bae79..9b704a1f 100644 --- a/app/Events/TagCreated.php +++ b/app/Events/TagCreated.php @@ -21,6 +21,8 @@ public function __construct(Tag $tag) if (Auth::check()) { $userId = Auth::user()->id; + } elseif (request()->get('apiKey')) { + $userId = request()->get('apiKey')->user_id; } Activity::create([ diff --git a/app/Events/TagDeleted.php b/app/Events/TagDeleted.php index 2e31d76e..bedf44c8 100644 --- a/app/Events/TagDeleted.php +++ b/app/Events/TagDeleted.php @@ -21,6 +21,8 @@ public function __construct(Tag $tag) if (Auth::check()) { $userId = Auth::user()->id; + } elseif (request()->get('apiKey')) { + $userId = request()->get('apiKey')->user_id; } Activity::create([ diff --git a/app/Events/TransactionCreated.php b/app/Events/TransactionCreated.php index 6a2691f3..4b3b9b46 100644 --- a/app/Events/TransactionCreated.php +++ b/app/Events/TransactionCreated.php @@ -22,6 +22,8 @@ public function __construct($transaction) if (Auth::check()) { $userId = Auth::user()->id; + } elseif (request()->get('apiKey')) { + $userId = request()->get('apiKey')->user_id; } if ($transaction instanceof Earning) { diff --git a/app/Events/TransactionDeleted.php b/app/Events/TransactionDeleted.php index 98ccfcb0..7475153b 100644 --- a/app/Events/TransactionDeleted.php +++ b/app/Events/TransactionDeleted.php @@ -22,6 +22,8 @@ public function __construct($transaction) if (Auth::check()) { $userId = Auth::user()->id; + } elseif (request()->get('apiKey')) { + $userId = request()->get('apiKey')->user_id; } if ($transaction instanceof Earning) { diff --git a/tests/Feature/Api/TransactionControllerTest.php b/tests/Feature/Api/TransactionControllerTest.php index 6ab31f4d..fa75fece 100644 --- a/tests/Feature/Api/TransactionControllerTest.php +++ b/tests/Feature/Api/TransactionControllerTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Api; +use App\Models\Activity; use App\Models\ApiKey; use App\Models\Earning; use App\Models\Space; @@ -46,5 +47,15 @@ public function testStoreEndpoint(): void 'amount' => 1050, ], ); + + $this->assertDatabaseHas( + Activity::class, + [ + 'space_id' => $space->id, + 'user_id' => $user->id, + 'entity_type' => 'earning', // TODO: create enum + 'action' => 'transaction.created', // TODO: also create enum + ], + ); } }