More refactoring

Refactored some old code and added missing translation calls.
This commit is contained in:
2021-07-20 10:32:43 +01:00
parent 620453c1e4
commit 6cda1fe183
7 changed files with 61 additions and 134 deletions

View File

@@ -24,7 +24,6 @@ namespace App\Http\Controllers;
use App\Application;
use App\Events\ApplicationDeniedEvent;
use App\Facades\JSON;
use App\Http\Resources\ApplicationResource;
use App\Notifications\ApplicationMoved;
use App\Notifications\NewApplicant;
use App\Response;
@@ -49,7 +48,7 @@ class ApplicationController extends Controller
}
}
return ($allvotes->count() == 1) ? false : true;
return !(($allvotes->count() == 1));
}
public function showUserApps()
@@ -60,51 +59,34 @@ class ApplicationController extends Controller
public function showUserApp(Request $request, Application $application)
{
if (!$request->wantsJson()) {
$this->authorize('view', $application);
$this->authorize('view', $application);
if (!is_null($application)) {
return view('dashboard.user.viewapp')
->with(
[
'application' => $application,
'comments' => $application->comments,
'structuredResponses' => json_decode($application->response->responseData, true),
'formStructure' => $application->response->form,
'vacancy' => $application->response->vacancy,
'canVote' => $this->canVote($application->votes),
]
);
} else {
$request->session()->flash('error', 'The application you requested could not be found.');
}
return redirect()->back();
if (!is_null($application)) {
return view('dashboard.user.viewapp')
->with(
[
'application' => $application,
'comments' => $application->comments,
'structuredResponses' => json_decode($application->response->responseData, true),
'formStructure' => $application->response->form,
'vacancy' => $application->response->vacancy,
'canVote' => $this->canVote($application->votes),
]
);
} else {
$request->session()->flash('error', __('The application you requested could not be found.'));
}
return (new ApplicationResource($application))->additional([
'meta' => [
'code' => 200,
'status' => 'success'
]
]);
return redirect()->back();
}
public function showAllApps(Request $request)
{
if (!$request->wantsJson()) {
$this->authorize('viewAny', Application::class);
$this->authorize('viewAny', Application::class);
return view('dashboard.appmanagement.all')
->with('applications', Application::paginate(6));
}
return view('dashboard.appmanagement.all');
// 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),
]);
} 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();
if ($vacancy->isEmpty()) {
if (!$request->wantsJson()) {
return redirect()
->back()
->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();
return redirect()
->back()
->with('error', __('This vacancy doesn\'t exist; Please use the proper buttons to apply to one.'));
}
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()
->back()
->with('error', 'This application is unavailable');
->with('error', __('This application is unavailable'));
}
Log::info('Processing new application!');
$formStructure = json_decode($vacancy->first()->forms->formStructure, true);
$responseValidation = ($request->wantsJson()) ? ContextAwareValidator::getResponseValidator($request->json('payload'), $formStructure) : ContextAwareValidator::getResponseValidator($request->all(), $formStructure);
$applicant = ($request->wantsJson()) ? User::findOrFail($request->json('metadata.submittingUserID')) : Auth::user();
$responseValidation = ContextAwareValidator::getResponseValidator($request->all(), $formStructure);
$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.
// User ID 1 is exempt from application rate limits
@@ -197,55 +165,35 @@ class ApplicationController extends Controller
}
}
if ($request->wantsJson()) {
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.');
$request->session()->flash('success', __('Thank you for your application! It will be reviewed as soon as possible.'));
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!');
return redirect()
->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)
{
$messageIsError = false;
if (!$request->wantsJson())
$this->authorize('update', Application::class);
$this->authorize('update', Application::class);
switch ($newStatus) {
case 'deny':
event(new ApplicationDeniedEvent($application));
$message = "Application denied successfully.";
$message = __("Application denied successfully.");
break;
case 'interview':
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->user->notify(new ApplicationMoved());
@@ -253,40 +201,21 @@ class ApplicationController extends Controller
break;
default:
$message = "There are no suitable statuses to update to.";
$message = __("There are no suitable statuses to update to.");
$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();
}
public function delete(Request $request, Application $application)
{
if (!$request->wantsJson()) {
$this->authorize('delete', $application);
$application->delete(); // observers will run, cleaning it up
$this->authorize('delete', $application);
$application->delete(); // observers will run, cleaning it up
return redirect()
->back()
->with('success', 'Application deleted. Comments, appointments and responses have also been deleted.');
}
return redirect()
->back()
->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();
}
}