DONE: disable auto trigger tac

This commit is contained in:
ISMAIL MASSERAN
2026-04-19 10:22:14 +08:00
parent a5aedb8e56
commit f31dcb0320
11 changed files with 454 additions and 75583 deletions
@@ -9,6 +9,7 @@ use App\UserOTP;
use App\Voter;
use Carbon\Carbon;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Auth;
use GuzzleHttp\Client;
@@ -16,15 +17,39 @@ class LoginController extends Controller
{
public function __invoke(Request $request)
{
/* Request OTP */
/* Request OTP (or lookup phone only when send_otp is false) */
try {
// $voter = Voter::where('no_kp', $request->no_kp)->firstOrFail();
$voter = $this->findVoterForCurrentElection($request->no_kp);
if (! $this->wantsSendOtp($request)) {
activity()
->performedOn($voter)
->withProperties([
'election_id' => Util::getCurrentElection(),
'voter_id' => $voter->id,
'voter_name' => $voter->name,
'no_kp' => $voter->no_kp,
'telefon' => $voter->telefon,
'ip' => $request->ip(),
'user_agent' => $request->userAgent(),
])
->log('voter lookup phone (no tac sent)');
return response()->json([
'status' => 'success',
'message' => 'Nombor telefon dijumpai.',
'notel' => $voter->telefon,
'nokp' => $voter->no_kp,
]);
}
$currentDateTime = Carbon::now();
$expired_at = Carbon::now()->addMinutes(config('onewaysms.minutes'));
$created_at = $currentDateTime->toDateTimeString();
$voter = Voter::where('no_kp', $request->no_kp)->firstOrFail();
if ($voter) {
$expiration = $this->checkExpirationTAC($request->no_kp);
@@ -78,6 +103,21 @@ class LoginController extends Controller
'status' => 'failed',
'message' => 'Does not found NO KP.',
]);
} catch (ModelNotFoundException $e) {
activity()
->withProperties([
'no_kp' => (string) $request->input('no_kp'),
'election_id' => Util::getCurrentElection(),
'ip' => $request->ip(),
'user_agent' => $request->userAgent(),
'reason' => 'voter not registered for current election',
])
->log('voter login rejected: not in current election');
return response()->json([
'status' => 'failed',
'message' => 'No. KP ini tidak didaftarkan untuk pilihan raya semasa. Sila pergi ke kaunter IT untuk mendaftar.',
]);
} catch (Exception $e) {
activity()
->withProperties([
@@ -95,6 +135,47 @@ class LoginController extends Controller
}
}
/**
* One row per member per election; telefon must be the one for the current (latest) election.
*/
private function findVoterForCurrentElection($noKp)
{
$electionId = Util::getCurrentElection();
return Voter::where('no_kp', $noKp)
->where('election_id', $electionId)
->orderBy('id', 'desc')
->firstOrFail();
}
/* Check if the user wants to send OTP */
private function wantsSendOtp(Request $request, $default = true)
{
// Use array_key_exists: Request::has() treats false as "missing"
$input = $request->all();
if (! array_key_exists('send_otp', $input)) {
return $default;
}
$value = $input['send_otp'];
if (is_bool($value)) {
return $value;
}
if (is_int($value) || is_float($value)) {
return (int) $value !== 0;
}
$lower = strtolower(trim((string) $value));
if (in_array($lower, ['false', '0', 'no', 'off', ''], true)) {
return false;
}
return true;
}
public function checkExpirationTAC($nokp): array
{
$currentDateTime = Carbon::now();