Files
Verde-Web/app/Http/Controllers/Api/V1/Auth/RegisterController.php
Azure_0_ ee72cd7211 feat: segregate super admin and LGU admin workflows
- Created AdminBarangayController to strictly scope barangay management to LGU boundaries
- Created AdminTenantController for LGU admins to fetch their own tenant configurations
- Updated sidebar to expose Barangays to LGU admins under the Areas menu
- Updated frontend UI to hide the City selection dropdown from LGU admins and dynamically inject their city_municipality_id
- Handled form validation issues caused by hidden required fields
- Implemented LGU dashboard routes and views
- Enforced 2 free QR code allocations on resident signup
2026-07-07 00:36:04 +08:00

112 lines
4.2 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\Household;
use App\Models\HouseholdMember;
use App\Models\NotificationPreference;
use App\Models\OtpCode;
use App\Models\User;
use App\Services\DropOff\DropOffPointFinder;
use App\Services\Otp\OtpService;
use App\Tenancy\Tenancy;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use MatanYadaev\EloquentSpatial\Objects\Point;
class RegisterController extends ApiController
{
public function __invoke(RegisterRequest $request, OtpService $otp, DropOffPointFinder $dopFinder): JsonResponse
{
$data = $request->validated();
// Tenant gate. New residents always belong to one LGU; the
// ResolveTenant middleware sets it from the X-Tenant-Code header.
$tenant = Tenancy::current();
if (! $tenant) {
return $this->fail('Pick your LGU first.', ['tenant' => ['LGU code required']], 400);
}
$user = DB::transaction(function () use ($data, $tenant, $dopFinder) {
$user = User::create([
'tenant_id' => $tenant->id,
'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',
]);
if ($data['role'] === User::ROLE_RESIDENT) {
$point = new Point((float) $data['lat'], (float) $data['lng'], 4326);
$nearestDop = $dopFinder->nearest((float) $data['lat'], (float) $data['lng']);
$household = Household::create([
'tenant_id' => $tenant->id,
'head_user_id' => $user->id,
'barangay_id' => $data['barangay_id'] ?? null,
'address_line' => $data['address_line'],
'coordinates' => $point,
'household_size' => $data['household_size'] ?? 1,
'assigned_drop_off_point_id' => $nearestDop?->id,
'verification_status' => Household::VERIFICATION_PENDING,
]);
HouseholdMember::create([
'household_id' => $household->id,
'user_id' => $user->id,
'relationship' => HouseholdMember::RELATIONSHIP_HEAD,
'full_name' => $user->full_name,
]);
app(\App\Services\Qr\QrAllocator::class)->allocateFreeToHousehold($household);
}
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.');
}
}