feat: add Snipe-IT asset synchronization
This commit is contained in:
70
app/Http/Controllers/Admin/SnipeItController.php
Normal file
70
app/Http/Controllers/Admin/SnipeItController.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Domain\Inventory\Models\SnipeAsset;
|
||||
use App\Domain\Inventory\Models\SyncRun;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Integrations\SnipeIt\SnipeItClient;
|
||||
use App\Jobs\SyncSnipeAssets;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Throwable;
|
||||
|
||||
class SnipeItController extends Controller
|
||||
{
|
||||
public function index(SnipeItClient $client): Response
|
||||
{
|
||||
return Inertia::render('admin/snipe-it', [
|
||||
'configured' => $client->isConfigured(),
|
||||
'baseUrl' => $client->configuredUrl(),
|
||||
'assets' => [
|
||||
'total' => SnipeAsset::query()->count(),
|
||||
'active' => SnipeAsset::query()->where('active', true)->count(),
|
||||
'requestable' => SnipeAsset::query()
|
||||
->where('active', true)
|
||||
->where('requestable', true)
|
||||
->count(),
|
||||
],
|
||||
'syncRuns' => SyncRun::query()
|
||||
->latest('started_at')
|
||||
->limit(10)
|
||||
->get()
|
||||
->map(fn (SyncRun $run): array => [
|
||||
'id' => $run->id,
|
||||
'status' => $run->status->value,
|
||||
'started_at' => $run->started_at->toIso8601String(),
|
||||
'finished_at' => $run->finished_at?->toIso8601String(),
|
||||
'records_processed' => $run->records_processed,
|
||||
'records_created' => $run->records_created,
|
||||
'records_updated' => $run->records_updated,
|
||||
'records_disabled' => $run->records_disabled,
|
||||
'error_message' => $run->error_message,
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function testConnection(SnipeItClient $client): RedirectResponse
|
||||
{
|
||||
$startedAt = hrtime(true);
|
||||
|
||||
try {
|
||||
$page = $client->listHardwarePage(0, 1);
|
||||
$duration = (int) round((hrtime(true) - $startedAt) / 1_000_000);
|
||||
|
||||
return back()->with('success', "Ligação confirmada em {$duration} ms. O Snipe-IT reporta {$page->total} equipamentos.");
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
|
||||
return back()->with('error', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function sync(): RedirectResponse
|
||||
{
|
||||
SyncSnipeAssets::dispatch();
|
||||
|
||||
return back()->with('success', 'A sincronização de equipamentos foi colocada na fila.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user