File: /var/www/html/app/Livewire/Auth/Decision.php
<?php
namespace App\Livewire\Auth;
use Livewire\Component;
use App\Models\SellerNin;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
class Decision extends Component
{
public $role;
public $user;
public $countries;
public $nationality_id;
public $name;
public $email;
public $phone;
public $last_name;
public $middle_name;
public $dob;
public $gender;
public $userId;
public $nin;
public function mount()
{
// $this->user = Session::get("user") ;
$this->countries = Cache::remember('countries', 60 * 60 * 24 * 30, function () {
return Http::get(ApiEndpoints::getCountry())->json()['data'];
});
}
public function updateRegister()
{
if (!empty($this->nin)) { // Check if $this->nin is empty
$this->verifyNin();
} else {
try {
$this->validate([
'role' => 'required|string|max:255',
'nationality_id' => 'required|string|max:255',
], [
'role.required' => 'The user type field is required.',
'nationality_id.required' => 'The country field is required.',
'nationality_id.exists' => 'The selected country is invalid.',
]);
$other = Session::get('body');
$body = [
'role' => $this->role,
'nationality_id' => $this->nationality_id,
'email' => $other['email'],
'name' => $other['name'],
'ip_address' => $other['ip_address'],
'password' => $other['password'],
'device_name' => $other['device_name'],
];
$response = Http::post(ApiEndpoints::register(), $body);
// dd($response->json());
if ($response->successful()) {
$info = $response->json()['user'];
Session::put('user', $info);
Session::put('user_email', $info['email']);
$this->redirect('/verify-email');
} else {
$info = $response->json()['message'] ?? 'An unexpected error occurred.';
$this->addError('role', $info);
}
} catch (\Throwable $th) {
$this->addError('role', $th->getMessage());
}
}
}
public function verifyNin()
{
try {
$this->validate([
'nin' => 'required|digits:11',
'role' => 'required|string|max:255',
'nationality_id' => 'required|string|max:255',
], [
'nin.required' => 'The nin field is required.',
'role.required' => 'The role field is required.',
'nationality_id.required' => 'The country field is required.',
'nationality_id.exists' => 'The selected country is invalid.',
]);
$response = Http::post(ApiEndpoints::validateNIN(), [
'nin' => $this->nin,
]);
if ($response->successful()) {
$data = $response->json()['body'];
$this->name = ucwords(strtolower($data['firstName']));
$this->last_name = ucwords(strtolower($data['lastName']));
$this->middle_name = isset($data['middleName']) ? ucwords(strtolower($data['middleName'])) : 'NA';
$this->dob = $data['dateOfBirth'];
$this->gender = $data['gender'];
$this->phone = $data['mobileNumber'];
Session::put('user_phone', $data['mobileNumber']);
$this->sendPhoneOtp();
$this->updateSellerInfo();
Session::flash('success', 'OTP sent successfully');
return redirect()->to('/verify-phone');
} else {
$this->addError('nin', $response->json()['message']);
}
} catch (\Throwable $th) {
Log::error($th->getMessage());
Session::flash('error', 'Please retry your registration. Something went wrong');
$this->redirect('/register');
// $this->addError('nin', 'Check your internet connection and try again');
}
}
private function registerUser()
{
$other = Session::get('body');
$body = [
'role' => $this->role,
'nationality_id' => $this->nationality_id,
'email' => $other['email'],
'phone' => $this->phone,
'name' => $this->name,
'lastname' => $this->last_name,
'ip_address' => $other['ip_address'] ?? '',
'password' => $other['password'],
'device_name' => $other['device_name'],
];
$response = Http::post(ApiEndpoints::register(), $body);
if ($response->successful()) {
$info = $response->json(['message']);
$user = $response->json(['user']);
session(['user' => $user]);
$this->user = Session::get("user");
session(['user_email' => $this->email]);
} else {
$info = $response->json(['message']);
$this->addError('name', $info);
Session::flash('error', $info);
}
}
private function sendPhoneOtp()
{
$this->registerUser();
$body = [
'phone' => $this->phone,
];
$response = Http::post(ApiEndpoints::sendPhoneOtp(), $body);
if ($response->successful()) {
return $response->json()['message'];
}
}
private function updateSellerInfo()
{
$body = [
'user_id' => $this->user['id'],
'first_name' => $this->name,
'last_name' => $this->last_name,
'middle_name' => $this->middle_name,
'dob' => $this->dob,
'gender' => $this->gender,
'nin' => $this->nin,
'created_at' => now(),
];
SellerNin::updateOrInsert(
['user_id' => $this->user['id']],
$body
);
}
public function render()
{
return view('livewire.auth.decision');
}
}