Files
Verde-Web/app/Http/Controllers/Api/V1/Admin/AdminHouseholdController.php
admin 15d56d0e98 feat(backend): finish Module 13 sub-modules + flow fixes
Notifications: notification_preferences + Laravel notifications inbox.
SmsChannel adapter for our SmsService. RoutesByPreferences trait reads
per-user toggles. HouseholdApproved/Rejected, QrBalanceLow, and
CodesPurchased notifications wired in via auto-discovered listeners
or direct dispatch from controllers/StoreOperations.

Payments: payments table + PaymentDriver interface. ManualPaymentDriver
works out of the box; PayMongoDriver activates when
PAYMONGO_SECRET_KEY is set, falls back to manual otherwise. Resident
initiates code-purchase, admin can mark paid manually, webhook applies
real provider events. Fulfillment runs StoreOperations::sellToHousehold.

Live tracking (HTTP polling): truck_location_history (with SPATIAL
INDEX + 7-day retention plan). Driver POST /driver/trucks/{uuid}/location
writes history, updates trucks.last_known_coordinates, caches in Redis,
flags geofence-trigger when entering active trip dumpsite. Admin
GET /admin/live/trucks returns active truck positions. Reverb broadcast
deferred.

Flow corrections:
- QrAllocator now idempotent — re-approving a household no longer
  re-dispenses free codes.
- arrive-dumpsite enforces dumpsite geofence via ST_Contains; can be
  bypassed with override_geofence: true.

171 feature tests passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 14:01:38 +08:00

110 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1\Admin;
use App\Events\HouseholdVerified;
use App\Http\Controllers\Api\V1\ApiController;
use App\Http\Requests\Admin\RejectProfileRequest;
use App\Http\Resources\HouseholdResource;
use App\Models\Household;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class AdminHouseholdController extends ApiController
{
public function index(Request $request): JsonResponse
{
$request->validate([
'verification_status' => ['nullable', 'in:pending,approved,rejected'],
'barangay_id' => ['nullable', 'integer'],
'q' => ['nullable', 'string', 'max:100'],
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
]);
$perPage = (int) $request->input('per_page', 25);
$households = Household::query()
->with(['head', 'barangay'])
->withCount('members')
->when(
$request->filled('verification_status'),
fn ($q) => $q->where('verification_status', $request->string('verification_status')),
)
->when(
$request->filled('barangay_id'),
fn ($q) => $q->where('barangay_id', $request->integer('barangay_id')),
)
->when($request->filled('q'), function ($q) use ($request) {
$term = '%'.$request->string('q').'%';
$q->where(function ($qq) use ($term) {
$qq->where('address_line', 'like', $term)
->orWhereHas('head', function ($h) use ($term) {
$h->where('first_name', 'like', $term)
->orWhere('last_name', 'like', $term)
->orWhere('email', 'like', $term);
});
});
})
->orderByDesc('id')
->paginate($perPage);
return $this->ok(
HouseholdResource::collection($households),
null,
[
'page' => $households->currentPage(),
'per_page' => $households->perPage(),
'total' => $households->total(),
'last_page' => $households->lastPage(),
],
);
}
public function show(Household $household): JsonResponse
{
$household->load(['head', 'barangay.cityMunicipality.province', 'members.user'])
->loadCount('members');
return $this->ok(new HouseholdResource($household));
}
public function approve(Request $request, Household $household): JsonResponse
{
if ($household->verification_status === Household::VERIFICATION_APPROVED) {
return $this->fail('Household already approved', null, 422);
}
if (! $household->proof_of_residency_path) {
return $this->fail(
'Cannot approve without proof of residency on file',
['proof' => ['missing']],
422,
);
}
$household->markVerified($request->user());
HouseholdVerified::dispatch($household->fresh(), $request->user());
$household->load(['head', 'barangay'])->loadCount('members');
return $this->ok(new HouseholdResource($household), 'Household approved');
}
public function reject(RejectProfileRequest $request, Household $household): JsonResponse
{
$reason = $request->validated('reason');
$household->markRejected($request->user(), $reason);
if ($household->head) {
\Illuminate\Support\Facades\Notification::send(
$household->head,
new \App\Notifications\HouseholdRejected($household, $reason),
);
}
$household->load(['head', 'barangay'])->loadCount('members');
return $this->ok(new HouseholdResource($household), 'Household rejected');
}
}