---
task: Fix GET /api/public/cooperative/:hash returning 500 instead of 404 for invalid hash
cycles: 3
context: true
private: false
started: 2026-05-16T16:00:46Z
finished: 2026-05-16T16:01:15Z
---

## files
- app/Http/Controllers/Market/CooperativeController.php [lines 390-403] — `publicGetCooperative` method; uses `response()->json()` instead of `Response::json()`
- routes/web.php [line 673] — `Route::get('/api/public/cooperative/{hkey}', ...)` — no auth middleware, public

## steps
1. In `CooperativeController::publicGetCooperative()` replace `response()->json()` calls with `Response::json()` using the Hypervel facade. Add `use Hypervel\Support\Facades\Response;` import if not present.
2. Wrap the `Organization::where(...)` query in a try/catch to return a clean 500 JSON error (not bare Server Error page) if the DB call itself fails.
3. Also audit `publicRegisterMember()` (lines 405-450) for the same `response()->json()` pattern and fix there too.

## context
```php
// BROKEN (lines 390-403):
public function publicGetCooperative(Request $request, string $hkey)
{
    $cooperative = Organization::where('hashkey', $hkey)
        ->where('type', 'COOPERATIVE')
        ->where('is_active', true)
        ->select(['id', 'hashkey', 'name', 'type', ...])
        ->first();

    if (!$cooperative) {
        return response()->json(['success' => false, 'message' => 'Cooperative not found'], 404);
        // ^ Should be Response::json() — response() helper may not exist in Hypervel
    }

    return response()->json(['success' => true, 'data' => $cooperative]);
}

// FIX:
use Hypervel\Support\Facades\Response;

public function publicGetCooperative(Request $request, string $hkey)
{
    try {
        $cooperative = Organization::where('hashkey', $hkey)
            ->where('type', 'COOPERATIVE')
            ->where('is_active', true)
            ->select(['id', 'hashkey', 'name', 'type', 'cooperative_type', 'cooperative_category', 'contact_person', 'contact_number', 'address'])
            ->first();
    } catch (\Throwable $e) {
        return Response::json(['success' => false, 'message' => 'Service temporarily unavailable'], 500);
    }

    if (!$cooperative) {
        return Response::json(['success' => false, 'message' => 'Cooperative not found'], 404);
    }

    return Response::json(['success' => true, 'data' => $cooperative]);
}
```

Impact: `RegisterCoop.vue` fetches `GET /api/public/cooperative/{hkey}` to render the public self-registration form. An invalid hash in the URL crashes the server instead of showing "Cooperative not found." This also affects the public cooperative registration QR code links shared by cooperatives — any typo or expired link causes a confusing server error.

## notes
- dictionary: ai-docs/dictionary.md (organizations table = `organizations`, cooperative members = `cooperative_members`)
- linters: none
- constraints: Use `Response::json()` facade (Hypervel), not `response()->json()` (Laravel global helper).
