Allowance check
This commit is contained in:
@@ -3,9 +3,11 @@
|
||||
namespace App\Http\Controllers\API\v1\Voter;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
use Illuminate\Validation\Rule;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Util;
|
||||
use App\Services\PhysicalAttendanceGateCode;
|
||||
use App\Voter;
|
||||
|
||||
class AttendanceController extends Controller
|
||||
@@ -14,6 +16,7 @@ class AttendanceController extends Controller
|
||||
public function __invoke(Request $request, $id)
|
||||
{
|
||||
$this->validateRequest($request, $id);
|
||||
$this->assertPhysicalGateCode($request);
|
||||
$this->updateAttendance($request, $id);
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
@@ -23,7 +26,7 @@ class AttendanceController extends Controller
|
||||
|
||||
private function validateRequest($request, $id)
|
||||
{
|
||||
$this->validate($request, [
|
||||
$rules = [
|
||||
'name' => [
|
||||
'required',
|
||||
Rule::unique('voter')->ignore($id)->where(function($query){
|
||||
@@ -46,15 +49,72 @@ class AttendanceController extends Controller
|
||||
})
|
||||
],
|
||||
|
||||
'kehadiran' => 'required'
|
||||
'kehadiran' => 'required',
|
||||
];
|
||||
|
||||
]);
|
||||
if (config('physical_attendance_gate.enabled')) {
|
||||
$rules['physical_gate_code'] = 'required_if:kehadiran,1|string|max:64';
|
||||
}
|
||||
|
||||
$this->validate($request, $rules);
|
||||
}
|
||||
|
||||
/**
|
||||
* When gate is enabled, Fizikal (kehadiran = 1) must match the rotating counter code.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return void
|
||||
*/
|
||||
private function assertPhysicalGateCode(Request $request)
|
||||
{
|
||||
if (!config('physical_attendance_gate.enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((int) $request->input('kehadiran') !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$gate = app(PhysicalAttendanceGateCode::class);
|
||||
if (!$gate->isConfigured()) {
|
||||
throw new HttpResponseException(response()->json([
|
||||
'status' => 'failed',
|
||||
'message' => 'Pengesahan kod fizikal tidak dikonfigurasi dengan betul.',
|
||||
], 503));
|
||||
}
|
||||
|
||||
if (!$gate->validates(Util::getCurrentElection(), (string) $request->input('physical_gate_code'))) {
|
||||
throw new HttpResponseException(response()->json([
|
||||
'status' => 'failed',
|
||||
'message' => 'Kod kaunter tidak sah atau telah tamat tempoh. Sila semak skrin pendaftaran dan cuba lagi.',
|
||||
], 422));
|
||||
}
|
||||
}
|
||||
|
||||
private function updateAttendance($request, $id)
|
||||
{
|
||||
$voter = $request->all();
|
||||
Voter::find($id)->update($voter);
|
||||
$voter = Voter::findOrFail($id);
|
||||
$voter->fill($request->only(['name', 'no_kp', 'no_anggota', 'unit', 'kehadiran']));
|
||||
|
||||
if ((int) $voter->kehadiran === 1) {
|
||||
$voter->fizikal_registration_verified_at = null;
|
||||
}
|
||||
|
||||
$voter->save();
|
||||
|
||||
$mode = ((int) $voter->kehadiran === 1) ? 'physical' : 'online';
|
||||
|
||||
activity()
|
||||
->performedOn($voter)
|
||||
->withProperties([
|
||||
'voter_id' => $voter->id,
|
||||
'voter_name' => $voter->name,
|
||||
'election_id' => Util::getCurrentElection(),
|
||||
'attendance_mode' => $mode,
|
||||
'ip' => request()->ip(),
|
||||
'user_agent' => request()->userAgent(),
|
||||
])
|
||||
->log("confirm attendance ({$mode}): {$voter->name}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user