Build live HickLab3D overview dashboard
Deploy HickLab3D Dashboard / Deploy to production (push) Successful in 24s
Deploy HickLab3D Dashboard / Deploy to production (push) Successful in 24s
This commit is contained in:
@@ -2,19 +2,121 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\EtsyConnection;
|
||||
use App\Models\Filament;
|
||||
use App\Models\User;
|
||||
use App\Services\Bambuddy;
|
||||
use App\Services\DryerClient;
|
||||
use App\Services\Etsy;
|
||||
use App\Services\Parcel2Go;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\View\View;
|
||||
use Throwable;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request): View
|
||||
public function __invoke(Request $request, Etsy $etsy, Bambuddy $bambuddy, Parcel2Go $parcel2Go, DryerClient $dryer): View
|
||||
{
|
||||
$isAdmin = $request->user()->role === 'admin';
|
||||
$filamentCount = Filament::count();
|
||||
$recentFilaments = Filament::with(['prices' => fn ($query) => $query->orderByDesc('recordedAt')->limit(1)])
|
||||
->orderByDesc('id')->limit(4)->get();
|
||||
$materials = Filament::query()->select('material')->selectRaw('COUNT(*) as total')
|
||||
->groupBy('material')->orderByDesc('total')->get()
|
||||
->map(fn (Filament $filament): array => [
|
||||
'label' => $filament->material ?: 'Unspecified',
|
||||
'count' => (int) $filament->total,
|
||||
])->all();
|
||||
|
||||
$etsyStatus = $isAdmin ? 'Not connected' : null;
|
||||
$etsyConnected = false;
|
||||
$receipts = [];
|
||||
$listingCount = null;
|
||||
$shippingStatus = $isAdmin ? 'Not configured' : null;
|
||||
$shipments = [];
|
||||
|
||||
if ($isAdmin && ($connection = EtsyConnection::first()) && $etsy->configured()) {
|
||||
try {
|
||||
$receipts = $etsy->receipts($connection)['results'] ?? [];
|
||||
$etsyStatus = 'Live · '.$connection->shop_name;
|
||||
$etsyConnected = true;
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
$etsyStatus = 'Temporarily unavailable';
|
||||
}
|
||||
|
||||
try {
|
||||
$listings = $etsy->listings($connection);
|
||||
$listingCount = (int) ($listings['count'] ?? count($listings['results'] ?? []));
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
}
|
||||
}
|
||||
|
||||
if ($isAdmin && filled(config('services.parcel2go.client_id')) && filled(config('services.parcel2go.client_secret'))) {
|
||||
try {
|
||||
$shipments = array_slice($parcel2Go->fetch('me/orders/detail')['Orders'] ?? [], 0, 4);
|
||||
$shippingStatus = 'Live account';
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
$shippingStatus = 'Temporarily unavailable';
|
||||
}
|
||||
}
|
||||
|
||||
$printers = [];
|
||||
$printerStatus = 'Not configured';
|
||||
if ($bambuddy->configured()) {
|
||||
try {
|
||||
$printers = $bambuddy->printers();
|
||||
$printerStatus = 'Live from BamBuddy';
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
$printerStatus = 'Temporarily unavailable';
|
||||
}
|
||||
}
|
||||
|
||||
$printingCount = collect($printers)->filter(fn (array $printer): bool => in_array(
|
||||
strtoupper((string) ($printer['live']['state'] ?? $printer['status'] ?? '')),
|
||||
['RUNNING', 'PRINTING'],
|
||||
true,
|
||||
))->count();
|
||||
|
||||
$dryerState = 'Not configured';
|
||||
if (filled(config('services.dryer.api_key'))) {
|
||||
try {
|
||||
$dryerState = ucfirst((string) ($dryer->status()['state'] ?? 'Unknown'));
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
$dryerState = 'Unavailable';
|
||||
}
|
||||
}
|
||||
|
||||
$days = collect(range(13, 0))->map(fn (int $offset): Carbon => now()->startOfDay()->subDays($offset));
|
||||
$orderTrend = $days->map(fn (Carbon $day): int => collect($receipts)->filter(
|
||||
fn (array $receipt): bool => isset($receipt['create_timestamp'])
|
||||
&& Carbon::createFromTimestamp((int) $receipt['create_timestamp'])->isSameDay($day),
|
||||
)->count())->all();
|
||||
|
||||
return view('dashboard', [
|
||||
'filamentCount' => Filament::count(),
|
||||
'userCount' => $request->user()->role === 'admin' ? User::count() : null,
|
||||
'recentFilaments' => Filament::with(['prices' => fn ($query) => $query->orderByDesc('recordedAt')->limit(1)])->orderByDesc('id')->limit(5)->get(),
|
||||
'isAdmin' => $isAdmin,
|
||||
'filamentCount' => $filamentCount,
|
||||
'recentFilaments' => $recentFilaments,
|
||||
'materials' => $materials,
|
||||
'receipts' => array_slice($receipts, 0, 5),
|
||||
'awaitingDispatch' => count($receipts),
|
||||
'listingCount' => $listingCount,
|
||||
'etsyStatus' => $etsyStatus,
|
||||
'etsyConnected' => $etsyConnected,
|
||||
'shipments' => $shipments,
|
||||
'shippingStatus' => $shippingStatus,
|
||||
'printers' => $printers,
|
||||
'printerStatus' => $printerStatus,
|
||||
'printingCount' => $printingCount,
|
||||
'dryerState' => $dryerState,
|
||||
'orderChart' => [
|
||||
'labels' => $days->map(fn (Carbon $day): string => $day->format('d M'))->all(),
|
||||
'series' => $orderTrend,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user