Files
Verde-Web/app/Http/Controllers/Api/V1/Auth/RegisterController.php
admin 58a7d3680a feat(backend): hardening sprint — docs, schedulers, validation, email + pickup
1. API docs via dedoc/scramble at /docs/api (scoped to api/v1).
   Linked from the admin sidebar Settings group.

2. Scheduled commands registered in routes/console.php:
   - reports:aggregate (02:00) — daily/weekly/monthly aggregations
   - qr:expire (02:30) — flips past-due allocated/active codes to expired
   - trucks:prune-locations (03:00) — drops history older than retention
     window (default 7 days, config('verde.location_retention_days'))
   All idempotent + withoutOverlapping. --dry flags on qr:expire and
   trucks:prune-locations for safe inspection.

3. Trip double-booking validation: AdminTripController::store rejects
   new trips when the team or truck already has a non-cancelled trip on
   the same date. override_conflicts: true bypasses for emergencies.
   Cancelled trips don't block rebooking.

4a. Email verification: User implements MustVerifyEmail.
    VerifyEmailNotification overrides verificationUrl() for our
    namespaced route. Register sends the link automatically (best
    effort, won't block signup). POST /auth/email/resend (auth) +
    GET /auth/email/verify/{id}/{hash} (signed URL).

4b. Password change while logged in: POST /me/password validates
    current_password, requires the new password to differ, revokes
    every other active token on success — current session stays.

5a. PickupImminent notification: when TripStop -> arrived,
    TripExecutor::notifyAssignedHouseholds() finds households whose
    assigned_drop_off_point_id matches and sends DB + SMS.

5b. Auto-geofence on truck location: TruckTracker::record() now
    auto-fires TripExecutor::arriveAtDumpsite() when an in-progress
    trip's truck pings inside its dumpsite boundary. The executor's
    status guard prevents duplicate timeline events if the driver also
    presses arrive-dumpsite manually.

190 feature tests passing.

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

74 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1\Auth;
use App\Http\Controllers\Api\V1\ApiController;
use App\Http\Requests\Auth\RegisterRequest;
use App\Http\Resources\UserResource;
use App\Models\NotificationPreference;
use App\Models\OtpCode;
use App\Models\User;
use App\Services\Otp\OtpService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class RegisterController extends ApiController
{
public function __invoke(RegisterRequest $request, OtpService $otp): JsonResponse
{
$data = $request->validated();
$user = DB::transaction(function () use ($data) {
$user = User::create([
'first_name' => $data['first_name'],
'middle_name' => $data['middle_name'] ?? null,
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => Hash::make($data['password']),
'role' => $data['role'],
'status' => User::STATUS_PENDING,
'preferred_language' => $data['preferred_language'] ?? 'en',
]);
$user->assignRole($data['role']);
$profileClass = User::profileModelForRole($data['role']);
if ($profileClass) {
$profileClass::create(['user_id' => $user->id]);
}
NotificationPreference::create([
'user_id' => $user->id,
'language' => $data['preferred_language'] ?? 'en',
]);
return $user;
});
$issue = $otp->issue(
destination: $user->phone,
purpose: OtpCode::PURPOSE_REGISTER,
user: $user,
);
// Fire-and-forget email verification link. If the mail backend is
// misconfigured we don't block registration.
try {
$user->sendEmailVerificationNotification();
} catch (\Throwable $e) {
\Log::warning('Failed to send verification email', [
'user_id' => $user->id, 'error' => $e->getMessage(),
]);
}
return $this->created([
'user' => new UserResource($user),
'otp_sent' => $issue->issued,
'otp_destination' => $user->phone,
'debug_code' => app()->environment('local', 'testing') ? $issue->plainCode : null,
], 'Registration successful. Verify the OTP sent to your phone.');
}
}