Skip to content

Commit 233174f

Browse files
committed
Save actions log.
1 parent c2db5a2 commit 233174f

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/Actions/Action.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Binaryk\LaravelRestify\Http\Requests\ActionRequest;
66
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
7+
use Binaryk\LaravelRestify\Models\ActionLog;
78
use Binaryk\LaravelRestify\Restify;
89
use Binaryk\LaravelRestify\Traits\AuthorizedToSee;
910
use Binaryk\LaravelRestify\Traits\Make;
@@ -147,16 +148,22 @@ public function handleRequest(ActionRequest $request)
147148
$request->collectRepositories($this, static::$chunkCount, function ($models) use ($request, &$response) {
148149
Transaction::run(function () use ($models, $request, &$response) {
149150
$response = $this->handle($request, $models);
151+
152+
$models->each(fn(Model $model) => ActionLog::forRepositoryAction($this, $model, $request->user())->save());
150153
});
151154
});
152155
} else {
153156
Transaction::run(function () use ($request, &$response) {
154157
$response = $this->handle(
155158
$request,
156-
tap($request->modelQuery(), function ($query) use ($request) {
159+
$model = tap($request->modelQuery(), function ($query) use ($request) {
157160
static::indexQuery($request, $query);
158161
})->firstOrFail()
159162
);
163+
164+
Restify::actionLog()::forRepositoryAction(
165+
$this, $model, $request->user()
166+
)->save();
160167
});
161168
}
162169

src/Models/ActionLog.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Binaryk\LaravelRestify\Models;
44

5+
use Binaryk\LaravelRestify\Actions\Action;
56
use Illuminate\Contracts\Auth\Authenticatable;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Support\Str;
@@ -105,4 +106,26 @@ public static function forRepositoryDestroy(Model $model, Authenticatable $user
105106
'updated_at' => now(),
106107
]);
107108
}
109+
110+
public static function forRepositoryAction(Action $action, Model $model, Authenticatable $user = null): self
111+
{
112+
return new static([
113+
'batch_id' => (string) Str::uuid(),
114+
'user_id' => optional($user)->getAuthIdentifier(),
115+
'name' => $action->uriKey(),
116+
'actionable_type' => $model->getMorphClass(),
117+
'actionable_id' => $model->getKey(),
118+
'target_type' => $model->getMorphClass(),
119+
'target_id' => $model->getKey(),
120+
'model_type' => $model->getMorphClass(),
121+
'model_id' => $model->getKey(),
122+
'fields' => '',
123+
'status' => static::STATUS_FINISHED,
124+
'original' => $model->toArray(),
125+
'changes' => null,
126+
'exception' => '',
127+
'created_at' => now(),
128+
'updated_at' => now(),
129+
]);
130+
}
108131
}

tests/Feature/ActionLogTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Binaryk\LaravelRestify\Tests\Feature;
44

5+
use Binaryk\LaravelRestify\Actions\Action;
56
use Binaryk\LaravelRestify\Models\ActionLog;
67
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
78
use Binaryk\LaravelRestify\Tests\IntegrationTest;
@@ -89,4 +90,33 @@ public function test_can_create_log_for_repository_deleting()
8990

9091
$this->assertDatabaseCount('action_logs', 1);
9192
}
93+
94+
public function test_can_create_log_for_repository_custom_action()
95+
{
96+
$this->authenticate();
97+
98+
$user = User::factory()->create();
99+
100+
$action = new class extends Action {
101+
public static $uriKey = 'test action';
102+
};
103+
104+
$log = ActionLog::forRepositoryAction(
105+
$action,
106+
$user,
107+
$this->authenticatedAs
108+
);
109+
110+
$log->save();
111+
112+
$this->assertInstanceOf(ActionLog::class, $log);
113+
114+
$this->assertDatabaseHas('action_logs', [
115+
'name' => 'test action',
116+
'actionable_type' => User::class,
117+
'actionable_id' => 1,
118+
]);
119+
120+
$this->assertDatabaseCount('action_logs', 1);
121+
}
92122
}

0 commit comments

Comments
 (0)