More refactoring

Refactored some old code and added missing translation calls.
This commit is contained in:
Miguel Nogueira 2021-07-20 10:32:43 +01:00
parent 620453c1e4
commit 6cda1fe183
Signed by: miguel456
GPG Key ID: 2CF61B825316C6A0
7 changed files with 61 additions and 134 deletions

View File

@ -42,7 +42,7 @@ class ApiKeyController extends Controller
if ($key) if ($key)
{ {
$request->session()->flash('success', 'Key successfully registered!'); $request->session()->flash('success', __('Key successfully registered!'));
$request->session()->flash('finalKey', $discriminator . '.' . $secret); $request->session()->flash('finalKey', $discriminator . '.' . $secret);
return redirect() return redirect()
@ -51,7 +51,7 @@ class ApiKeyController extends Controller
return redirect() return redirect()
->back() ->back()
->with('error', 'An error occurred whilst trying to create an API key.'); ->with('error', __('An error occurred whilst trying to create an API key.'));
} }
@ -68,12 +68,12 @@ class ApiKeyController extends Controller
{ {
return redirect() return redirect()
->back() ->back()
->with('error', 'Key already revoked.'); ->with('error', __('Key already revoked.'));
} }
return redirect() return redirect()
->back() ->back()
->with('success', 'Key revoked. Apps using this key will stop working.'); ->with('success', __('Key revoked. Apps using this key will stop working.'));
} }
@ -89,7 +89,7 @@ class ApiKeyController extends Controller
return redirect() return redirect()
->back() ->back()
->with('success', 'Key deleted successfully. Apps using this key will stop working.'); ->with('success', __('Key deleted successfully. Apps using this key will stop working.'));
} }
} }

View File

@ -24,7 +24,6 @@ namespace App\Http\Controllers;
use App\Application; use App\Application;
use App\Events\ApplicationDeniedEvent; use App\Events\ApplicationDeniedEvent;
use App\Facades\JSON; use App\Facades\JSON;
use App\Http\Resources\ApplicationResource;
use App\Notifications\ApplicationMoved; use App\Notifications\ApplicationMoved;
use App\Notifications\NewApplicant; use App\Notifications\NewApplicant;
use App\Response; use App\Response;
@ -49,7 +48,7 @@ class ApplicationController extends Controller
} }
} }
return ($allvotes->count() == 1) ? false : true; return !(($allvotes->count() == 1));
} }
public function showUserApps() public function showUserApps()
@ -60,51 +59,34 @@ class ApplicationController extends Controller
public function showUserApp(Request $request, Application $application) public function showUserApp(Request $request, Application $application)
{ {
if (!$request->wantsJson()) { $this->authorize('view', $application);
$this->authorize('view', $application);
if (!is_null($application)) { if (!is_null($application)) {
return view('dashboard.user.viewapp') return view('dashboard.user.viewapp')
->with( ->with(
[ [
'application' => $application, 'application' => $application,
'comments' => $application->comments, 'comments' => $application->comments,
'structuredResponses' => json_decode($application->response->responseData, true), 'structuredResponses' => json_decode($application->response->responseData, true),
'formStructure' => $application->response->form, 'formStructure' => $application->response->form,
'vacancy' => $application->response->vacancy, 'vacancy' => $application->response->vacancy,
'canVote' => $this->canVote($application->votes), 'canVote' => $this->canVote($application->votes),
] ]
); );
} else { } else {
$request->session()->flash('error', 'The application you requested could not be found.'); $request->session()->flash('error', __('The application you requested could not be found.'));
}
return redirect()->back();
} }
return (new ApplicationResource($application))->additional([ return redirect()->back();
'meta' => [
'code' => 200,
'status' => 'success'
]
]);
} }
public function showAllApps(Request $request) public function showAllApps(Request $request)
{ {
if (!$request->wantsJson()) { $this->authorize('viewAny', Application::class);
$this->authorize('viewAny', Application::class);
return view('dashboard.appmanagement.all') return view('dashboard.appmanagement.all');
->with('applications', Application::paginate(6));
}
// todo: eager load all relationships used
return ApplicationResource::collection(Application::paginate(6))->additional([
'code' => '200',
'status' => 'success',
]);
} }
@ -121,7 +103,7 @@ class ApplicationController extends Controller
'preprocessedForm' => json_decode($vacancyWithForm->first()->forms->formStructure, true), 'preprocessedForm' => json_decode($vacancyWithForm->first()->forms->formStructure, true),
]); ]);
} else { } else {
abort(404, 'The application you\'re looking for could not be found or it is currently unavailable.'); abort(404, __('The application you\'re looking for could not be found or it is currently unavailable.'));
} }
} }
@ -130,38 +112,24 @@ class ApplicationController extends Controller
$vacancy = Vacancy::with('forms')->where('vacancySlug', $vacancySlug)->get(); $vacancy = Vacancy::with('forms')->where('vacancySlug', $vacancySlug)->get();
if ($vacancy->isEmpty()) { if ($vacancy->isEmpty()) {
if (!$request->wantsJson()) {
return redirect() return redirect()
->back() ->back()
->with('error', 'This vacancy doesn\'t exist; Please use the proper buttons to apply to one.'); ->with('error', __('This vacancy doesn\'t exist; Please use the proper buttons to apply to one.'));
}
return JSON::setResponseType('error')
->setStatus('error')
->setMessage('Can\'t apply to non-existent application!')
->setCode(404)
->build();
} }
if ($vacancy->first()->vacancyCount == 0 || $vacancy->first()->vacancyStatus !== 'OPEN') { if ($vacancy->first()->vacancyCount == 0 || $vacancy->first()->vacancyStatus !== 'OPEN') {
if ($request->wantsJson()) {
return JSON::setResponseType('error')
->setStatus('error')
->setMessage('This application is unavailable.')
->setCode(404)
->build();
}
return redirect() return redirect()
->back() ->back()
->with('error', 'This application is unavailable'); ->with('error', __('This application is unavailable'));
} }
Log::info('Processing new application!'); Log::info('Processing new application!');
$formStructure = json_decode($vacancy->first()->forms->formStructure, true); $formStructure = json_decode($vacancy->first()->forms->formStructure, true);
$responseValidation = ($request->wantsJson()) ? ContextAwareValidator::getResponseValidator($request->json('payload'), $formStructure) : ContextAwareValidator::getResponseValidator($request->all(), $formStructure); $responseValidation = ContextAwareValidator::getResponseValidator($request->all(), $formStructure);
$applicant = ($request->wantsJson()) ? User::findOrFail($request->json('metadata.submittingUserID')) : Auth::user(); $applicant = Auth::user();
// API users may specify ID 1 for an anonymous application, but they'll have to submit contact details for it to become active. // API users may specify ID 1 for an anonymous application, but they'll have to submit contact details for it to become active.
// User ID 1 is exempt from application rate limits // User ID 1 is exempt from application rate limits
@ -197,55 +165,35 @@ class ApplicationController extends Controller
} }
} }
if ($request->wantsJson()) { $request->session()->flash('success', __('Thank you for your application! It will be reviewed as soon as possible.'));
return JSON::setResponseType('success')
->setStatus('accepted')
->setMessage('Application submitted successfully. Please refer to the docs to add contact info if your submission was anonymous.')
->setCode(201)
->setAdditional([
'links' => [
'application-web' => route('showUserApp', ['application' => $application->id])
]
])->build();
}
$request->session()->flash('success', 'Thank you for your application! It will be reviewed as soon as possible.');
return redirect(route('showUserApps')); return redirect(route('showUserApps'));
} elseif ($request->wantsJson()) {
return JSON::setResponseType('error')
->setStatus('validation_error')
->setMessage('There are one or more errors in this application. Please make sure all fields are present in "payload" according to this application\'s respective form structure; They\'re always required.')
->setCode(400)
->build();
} }
Log::warning('Application form for ' . $applicant->name . ' contained errors, resetting!'); Log::warning('Application form for ' . $applicant->name . ' contained errors, resetting!');
return redirect() return redirect()
->back() ->back()
->with('error', 'There are one or more errors in your application. Please make sure none of your fields are empty, since they are all required.'); ->with('error', __('There are one or more errors in your application. Please make sure none of your fields are empty, since they are all required.'));
} }
public function updateApplicationStatus(Request $request, Application $application, $newStatus) public function updateApplicationStatus(Request $request, Application $application, $newStatus)
{ {
$messageIsError = false; $messageIsError = false;
$this->authorize('update', Application::class);
if (!$request->wantsJson())
$this->authorize('update', Application::class);
switch ($newStatus) { switch ($newStatus) {
case 'deny': case 'deny':
event(new ApplicationDeniedEvent($application)); event(new ApplicationDeniedEvent($application));
$message = "Application denied successfully."; $message = __("Application denied successfully.");
break; break;
case 'interview': case 'interview':
Log::info(' Moved application ID ' . $application->id . 'to interview stage!'); Log::info(' Moved application ID ' . $application->id . 'to interview stage!');
$message = 'Application moved to interview stage! (:'; $message = __('Application moved to interview stage!');
$application->setStatus('STAGE_INTERVIEW'); $application->setStatus('STAGE_INTERVIEW');
$application->user->notify(new ApplicationMoved()); $application->user->notify(new ApplicationMoved());
@ -253,40 +201,21 @@ class ApplicationController extends Controller
break; break;
default: default:
$message = "There are no suitable statuses to update to."; $message = __("There are no suitable statuses to update to.");
$messageIsError = true; $messageIsError = true;
} }
if ($request->wantsJson())
{
return JSON::setResponseType(($messageIsError) ? 'error' : 'success')
->setStatus(($messageIsError) ? 'error' : 'success')
->setMessage($message)
->setCode(($messageIsError) ? 400 : 200)
->build();
}
return redirect()->back(); return redirect()->back();
} }
public function delete(Request $request, Application $application) public function delete(Request $request, Application $application)
{ {
if (!$request->wantsJson()) { $this->authorize('delete', $application);
$this->authorize('delete', $application); $application->delete(); // observers will run, cleaning it up
$application->delete(); // observers will run, cleaning it up
return redirect() return redirect()
->back() ->back()
->with('success', 'Application deleted. Comments, appointments and responses have also been deleted.'); ->with('success', __('Application deleted. Comments, appointments and responses have also been deleted.'));
}
$application->delete();
return JSON::setResponseType('success')
->setStatus('deleted')
->setMessage('Application deleted. Relationships were also nuked.')
->setCode(200)
->build();
} }
} }

View File

@ -62,7 +62,7 @@ class AppointmentController extends Controller
]); ]);
$application->user->notify(new AppointmentScheduled($appointment)); $application->user->notify(new AppointmentScheduled($appointment));
$request->session()->flash('success', 'Appointment successfully scheduled @ '.$appointmentDate->toDateTimeString()); $request->session()->flash('success', __('Appointment successfully scheduled @ :appointmentTime', ['appointmentTime', $appointmentDate->toDateTimeString()]));
return redirect()->back(); return redirect()->back();
} }
@ -83,7 +83,7 @@ class AppointmentController extends Controller
$application->setStatus('STAGE_PEERAPPROVAL'); $application->setStatus('STAGE_PEERAPPROVAL');
$application->user->notify(new ApplicationMoved()); $application->user->notify(new ApplicationMoved());
$request->session()->flash('success', 'Interview finished! Staff members can now vote on it.'); $request->session()->flash('success', __('Interview finished! Staff members can now vote on it.'));
return redirect()->back(); return redirect()->back();
} }
@ -97,9 +97,9 @@ class AppointmentController extends Controller
$application->appointment->meetingNotes = $request->noteText; $application->appointment->meetingNotes = $request->noteText;
$application->appointment->save(); $application->appointment->save();
$request->session()->flash('success', 'Meeting notes have been saved.'); $request->session()->flash('success', __('Meeting notes have been saved.'));
} else { } else {
$request->session()->flash('error', 'There\'s no appointment to save notes to!'); $request->session()->flash('error', __('There\'s no appointment to save notes to!'));
} }
return redirect()->back(); return redirect()->back();

View File

@ -34,6 +34,8 @@ class BanController extends Controller
{ {
$this->authorize('create', [Ban::class, $user]); $this->authorize('create', [Ban::class, $user]);
// FIXME: Needs refactoring to a simpler format, e.g. parse the user's given date directly.
if (is_null($user->bans)) { if (is_null($user->bans)) {
$reason = $request->reason; $reason = $request->reason;
$duration = strtolower($request->durationOperator); $duration = strtolower($request->durationOperator);
@ -61,7 +63,7 @@ class BanController extends Controller
} }
} else { } else {
// Essentially permanent // Essentially permanent
$expiryDate->addYears(5); $expiryDate->addYears(40);
} }
$ban = Ban::create([ $ban = Ban::create([
@ -73,9 +75,9 @@ class BanController extends Controller
]); ]);
event(new UserBannedEvent($user, $ban)); event(new UserBannedEvent($user, $ban));
$request->session()->flash('success', 'User suspended successfully! Ban ID: #'.$ban->id); $request->session()->flash('success', __('Account suspended. Suspension ID #:susId', ['susId', $ban->id]));
} else { } else {
$request->session()->flash('error', 'User already suspended!'); $request->session()->flash('error', __('Account already suspended!'));
} }
return redirect()->back(); return redirect()->back();

View File

@ -29,10 +29,6 @@ use Illuminate\Support\Facades\Auth;
class CommentController extends Controller class CommentController extends Controller
{ {
public function index()
{
//
}
public function insert(NewCommentRequest $request, Application $application) public function insert(NewCommentRequest $request, Application $application)
{ {
@ -45,7 +41,7 @@ class CommentController extends Controller
]); ]);
if ($comment) { if ($comment) {
$request->session()->flash('success', __('Comment posted! (:')); $request->session()->flash('success', __('Comment posted!'));
} else { } else {
$request->session()->flash('error', __('Something went wrong while posting your comment!')); $request->session()->flash('error', __('Something went wrong while posting your comment!'));
} }

View File

@ -110,7 +110,7 @@ class UserController extends Controller
->get(); ->get();
if (! $matchingUsers->isEmpty()) { if (! $matchingUsers->isEmpty()) {
$request->session()->flash('success', 'There were '.$matchingUsers->count().' user(s) matching your search.'); $request->session()->flash('success', __('There were :usersCount user(s) matching your search.', ['usersCount' => $matchingUsers->count()]));
return view('dashboard.administration.players') return view('dashboard.administration.players')
->with([ ->with([
@ -118,7 +118,7 @@ class UserController extends Controller
'bannedUserCount' => Ban::all()->count(), 'bannedUserCount' => Ban::all()->count(),
]); ]);
} else { } else {
$request->session()->flash('error', 'Your search term did not return any results.'); $request->session()->flash('error', __('Your search term did not return any results.'));
return redirect(route('registeredPlayerList')); return redirect(route('registeredPlayerList'));
} }
@ -161,7 +161,7 @@ class UserController extends Controller
'timestamp' => now(), 'timestamp' => now(),
]); ]);
$request->session()->flash('success', 'Successfully logged out other devices. Remember to change your password if you think you\'ve been compromised.'); $request->session()->flash('success', __('Successfully logged out other devices. Remember to change your password if you think you\'ve been compromised.'));
return redirect()->back(); return redirect()->back();
} }
@ -236,8 +236,8 @@ class UserController extends Controller
$user->uuid = $request->uuid; $user->uuid = $request->uuid;
$existingRoles = Role::all() $existingRoles = Role::all()
->pluck('name') ->pluck('name')
->all(); ->all();
$roleDiff = array_diff($existingRoles, $request->roles); $roleDiff = array_diff($existingRoles, $request->roles);
@ -255,7 +255,7 @@ class UserController extends Controller
} }
$user->save(); $user->save();
$request->session()->flash('success', 'User updated successfully!'); $request->session()->flash('success', __('User updated successfully!'));
return redirect()->back(); return redirect()->back();
} }

View File

@ -42,7 +42,7 @@ class VoteController extends Controller
Log::info('User '.Auth::user()->name.' has voted in applicant '.$application->user->name.'\'s application', [ Log::info('User '.Auth::user()->name.' has voted in applicant '.$application->user->name.'\'s application', [
'voteType' => $voteRequest->voteType, 'voteType' => $voteRequest->voteType,
]); ]);
$voteRequest->session()->flash('success', 'Your vote has been registered!'); $voteRequest->session()->flash('success', __('Your vote has been counted!'));
// Cron job will run command that processes votes // Cron job will run command that processes votes
return redirect()->back(); return redirect()->back();