50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Me;
|
|
|
|
use App\Http\Controllers\Api\V1\ApiController;
|
|
use App\Http\Resources\HouseholdResource;
|
|
use App\Models\Household;
|
|
use App\Tenancy\Tenancy;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class MyHouseholdController extends ApiController
|
|
{
|
|
/**
|
|
* Resident fetches their own household without knowing the uuid.
|
|
* Returns `null` cleanly when none exists yet (used by the onboarding
|
|
* wizard to decide whether to start a new flow or show edit/status).
|
|
*/
|
|
public function show(Request $request): JsonResponse
|
|
{
|
|
$userId = $request->user()->id;
|
|
$tenantId = Tenancy::current()?->id;
|
|
Log::info("DEBUG HOUSEHOLD show for User $userId, Tenant: $tenantId");
|
|
$household = Household::with([
|
|
'head', 'barangay.cityMunicipality.province.region', 'barangay.serviceAreas',
|
|
'members.user',
|
|
'assignedDropOffPoint',
|
|
])
|
|
->withCount('members')
|
|
->where(function ($query) use ($request) {
|
|
$query->where('head_user_id', $request->user()->id)
|
|
->orWhereHas('members', function ($q) use ($request) {
|
|
$q->where('user_id', $request->user()->id);
|
|
});
|
|
})
|
|
->first();
|
|
|
|
if (! $household) {
|
|
return $this->ok([
|
|
'household' => null,
|
|
]);
|
|
}
|
|
|
|
return $this->ok([
|
|
'household' => new HouseholdResource($household),
|
|
]);
|
|
}
|
|
}
|