74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\APIS\CustomerApi;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
use App\Models\UserAssetLink;
|
|
use Illuminate\Http\Request;
|
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
|
use Illuminate\Container\Attributes\Auth;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Facades\Log;
|
|
class UserAssetLinkController extends Controller
|
|
{
|
|
// public function index(Request $request)
|
|
// {
|
|
|
|
// // $token = $request->bearerToken(); // Or $request->header('Authorization')
|
|
// $token = readHeaderToken(); // Or $request->header('Authorization')
|
|
// dd($token['sub']);
|
|
// // $user = Auth::user();
|
|
// // dd($user);
|
|
|
|
|
|
// $userAssetLinks = UserAssetLink::with(['user', 'asset.devices'])
|
|
// ->withCount([
|
|
// 'asset as active_devices_count' => function ($query) {
|
|
// $query->whereHas('devices', function ($q) {
|
|
// $q->where('active', 1);
|
|
// });
|
|
// },
|
|
// 'asset as inactive_devices_count' => function ($query) {
|
|
// $query->whereHas('devices', function ($q) {
|
|
// $q->where('active', 0);
|
|
// });
|
|
// }
|
|
// ])
|
|
// ->get();
|
|
|
|
|
|
// return response()->json($userAssetLinks);
|
|
// }
|
|
public function index()
|
|
{
|
|
|
|
try {
|
|
$token = readHeaderToken();
|
|
// dd($token['sub']);
|
|
$user = User::where('id',$token['sub'])->first();
|
|
|
|
$userAssetLinks = UserAssetLink::with(['user', 'asset.devices'])
|
|
->where('user_id', $user->id)
|
|
->withCount([
|
|
'asset as active_devices_count' => function ($query) {
|
|
$query->whereHas('devices', function ($q) {
|
|
$q->where('active', 1);
|
|
});
|
|
},
|
|
'asset as inactive_devices_count' => function ($query) {
|
|
$query->whereHas('devices', function ($q) {
|
|
$q->where('active', 0);
|
|
});
|
|
}
|
|
])
|
|
->get();
|
|
|
|
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $userAssetLinks, 200);
|
|
} catch (QueryException $e) {
|
|
Log::error('Something went wrong: ' . $e->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('Something went wrong'), 500);
|
|
}
|
|
}
|
|
}
|