Skip to content

Commit

Permalink
Associate activity with user by API key as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
range-of-motion committed Dec 10, 2023
1 parent 96885de commit c9e7b7f
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 2 deletions.
10 changes: 9 additions & 1 deletion app/Events/ImportCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 9 additions & 1 deletion app/Events/ImportDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions app/Events/RecurringCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
2 changes: 2 additions & 0 deletions app/Events/RecurringDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
2 changes: 2 additions & 0 deletions app/Events/TagCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
2 changes: 2 additions & 0 deletions app/Events/TagDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
2 changes: 2 additions & 0 deletions app/Events/TransactionCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions app/Events/TransactionDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/Api/TransactionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature\Api;

use App\Models\Activity;
use App\Models\ApiKey;
use App\Models\Earning;
use App\Models\Space;
Expand Down Expand Up @@ -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
],
);
}
}

0 comments on commit c9e7b7f

Please sign in to comment.