Allowance check
This commit is contained in:
@@ -16,13 +16,38 @@ class VoteController extends Controller
|
||||
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
|
||||
if ($this->validateRequest($request)['status'] == 'failed') {
|
||||
return response()->json($this->validateRequest($request));
|
||||
$validation = $this->validateRequest($request);
|
||||
if (($validation['status'] ?? 'failed') === 'failed') {
|
||||
return response()->json($validation);
|
||||
}
|
||||
|
||||
$this->insertVote($request);
|
||||
|
||||
$user = Auth::guard('voter')->user() ?: $request->user();
|
||||
$vote = is_array($request->vote) ? $request->vote : [];
|
||||
$positionsVoted = 0;
|
||||
$totalSelections = 0;
|
||||
foreach ($vote as $value) {
|
||||
$nomineeIds = $value['nominee_id'] ?? null;
|
||||
if (is_array($nomineeIds) && count($nomineeIds)) {
|
||||
$positionsVoted++;
|
||||
$totalSelections += count($nomineeIds);
|
||||
}
|
||||
}
|
||||
|
||||
activity()
|
||||
->performedOn($user)
|
||||
->withProperties([
|
||||
'election_id' => Util::getCurrentElection(),
|
||||
'voter_id' => Auth::id(),
|
||||
'voter_name' => $user ? $user->name : null,
|
||||
'positions_voted' => $positionsVoted,
|
||||
'total_selections' => $totalSelections,
|
||||
'ip' => $request->ip(),
|
||||
'user_agent' => $request->userAgent(),
|
||||
])
|
||||
->log('submit vote');
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Voted successfully',
|
||||
@@ -32,34 +57,31 @@ class VoteController extends Controller
|
||||
|
||||
private function insertVote($request)
|
||||
{
|
||||
// dd($request->vote[0]['nominee_id']);
|
||||
$votes = array_map(function($nominee_id) use($request){
|
||||
return[
|
||||
'voter_id' => Auth::id(),
|
||||
'election_id' => Util::getCurrentElection(),
|
||||
'position_id' => $request->vote[0]['position_id'],
|
||||
'nominee_id' => $nominee_id
|
||||
];
|
||||
$vote = $request->vote;
|
||||
if (!is_array($vote)) return;
|
||||
|
||||
},$request->vote[0]['nominee_id']);
|
||||
foreach ($vote as $value) {
|
||||
$positionId = $value['position_id'] ?? null;
|
||||
$nomineeIds = $value['nominee_id'] ?? [];
|
||||
if (!$positionId || !is_array($nomineeIds) || !count($nomineeIds)) continue;
|
||||
|
||||
foreach($votes as $vote){
|
||||
Result::updateOrCreate([
|
||||
'voter_id' => $vote['voter_id'],
|
||||
'election_id' => $vote['election_id'],
|
||||
'position_id' => $vote['position_id'],
|
||||
'nominee_id' => $vote['nominee_id'],
|
||||
]);
|
||||
foreach ($nomineeIds as $nomineeId) {
|
||||
Result::updateOrCreate([
|
||||
'voter_id' => Auth::id(),
|
||||
'election_id' => Util::getCurrentElection(),
|
||||
'position_id' => $positionId,
|
||||
'nominee_id' => $nomineeId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function validNominee($id, $position_id)
|
||||
{
|
||||
try{
|
||||
$nominee = Nominee::where('position_id', '=', $position_id)->whereIn('id',$id)->count();
|
||||
}catch(Exception $e){
|
||||
dd($id);
|
||||
}catch(\Exception $e){
|
||||
return 0;
|
||||
}
|
||||
return $nominee;
|
||||
}
|
||||
@@ -68,8 +90,8 @@ class VoteController extends Controller
|
||||
{
|
||||
try{
|
||||
return Position::where('id', $id)->count();
|
||||
}catch(Exception $e){
|
||||
dd($id);
|
||||
}catch(\Exception $e){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,14 +109,8 @@ class VoteController extends Controller
|
||||
$result['status'] = 'failed';
|
||||
$vote = $request->vote;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Check if the user vote on all position
|
||||
*/
|
||||
if (!$this->voteAllPosition($request)) {
|
||||
dd('test');
|
||||
$result['message'] = 'You must vote on all position.';
|
||||
if (!is_array($vote) || !count($vote)) {
|
||||
$result['message'] = 'You must vote on at least one position.';
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -103,19 +119,25 @@ class VoteController extends Controller
|
||||
* Check if position_id and nominee_id has a value
|
||||
*/
|
||||
// dd($vote,$value);
|
||||
if (empty($value['position_id']) || empty($value['nominee_id'])) {
|
||||
|
||||
dd($value,empty($value['position_id']),empty($value['nominee_id']));
|
||||
$result['message'] = 'You must vote on all position.';
|
||||
$positionId = $value['position_id'] ?? null;
|
||||
$nomineeIds = $value['nominee_id'] ?? null;
|
||||
if (empty($positionId)) {
|
||||
$result['message'] = 'Invalid Position.';
|
||||
return $result;
|
||||
}
|
||||
if (!is_array($nomineeIds) || !count($nomineeIds)) {
|
||||
// Skip empty positions; allow partial voting setups (UI may send all positions with empty arrays)
|
||||
continue;
|
||||
}
|
||||
if (count($nomineeIds) > 1) {
|
||||
$result['message'] = 'You can only vote for one nominee.';
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if the Position you vote exists
|
||||
*/
|
||||
if (!$this->isPositionExist($value['position_id'])) {
|
||||
dd('test');
|
||||
if (!$this->isPositionExist($positionId)) {
|
||||
$result['message'] = 'Invalid Position.';
|
||||
return $result;
|
||||
}
|
||||
@@ -124,14 +146,28 @@ class VoteController extends Controller
|
||||
* Check if the Nominee you vote on certain position exists
|
||||
*/
|
||||
// elseif (!$this->validNominee($value['nominee_id'], $value['position_id'])) {
|
||||
elseif($this->validNominee($value['nominee_id'],$value['position_id']) <= 0){
|
||||
elseif($this->validNominee($nomineeIds, $positionId) <= 0){
|
||||
|
||||
$result['message'] = 'Invalid Nominee for '.Position::find($value['position_id'])->name.' position.';
|
||||
$result['message'] = 'Invalid Nominee for '.Position::find($positionId)->name.' position.';
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
return ['status'=>'success'];
|
||||
// Ensure at least one actual vote exists
|
||||
$hasAny = false;
|
||||
foreach ($vote as $value) {
|
||||
$nomineeIds = $value['nominee_id'] ?? null;
|
||||
if (is_array($nomineeIds) && count($nomineeIds)) {
|
||||
$hasAny = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$hasAny) {
|
||||
$result['message'] = 'You must vote on at least one position.';
|
||||
return $result;
|
||||
}
|
||||
|
||||
return ['status' => 'success'];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user