Finished applications API endpoint group

This commit is contained in:
Miguel Nogueira 2021-03-31 19:39:42 +01:00
parent f267da3760
commit f62ea9669b
Signed by: miguel456
GPG Key ID: 2CF61B825316C6A0
3 changed files with 146 additions and 35 deletions

View File

@ -10,7 +10,7 @@ namespace App\Helpers;
class JSON class JSON
{ {
protected $type, $status, $message, $code, $data; protected $type, $status, $message, $code, $data, $additional;
/** /**
* @param mixed $type * @param mixed $type
@ -21,6 +21,23 @@ class JSON
return $this; return $this;
} }
/**
* @param mixed $additional
*/
public function setAdditional($additional)
{
$this->additional = $additional;
return $this;
}
/**
* @return mixed
*/
public function getAdditional()
{
return $this->additional;
}
/** /**
* @return mixed * @return mixed
*/ */
@ -109,9 +126,16 @@ class JSON
'meta' => [ 'meta' => [
'status' => $this->getStatus(), 'status' => $this->getStatus(),
'message' => $this->getMessage(), 'message' => $this->getMessage(),
], ]
]; ];
if (!empty($this->additional))
{
foreach($this->additional as $additionalKeyName => $key)
{
$response[$additionalKeyName] = $key;
}
}
return response($response, $this->getCode(), $headers); return response($response, $this->getCode(), $headers);
} }

View File

@ -23,6 +23,7 @@ namespace App\Http\Controllers;
use App\Application; use App\Application;
use App\Events\ApplicationDeniedEvent; use App\Events\ApplicationDeniedEvent;
use App\Facades\JSON;
use App\Http\Resources\ApplicationResource; use App\Http\Resources\ApplicationResource;
use App\Notifications\ApplicationMoved; use App\Notifications\ApplicationMoved;
use App\Notifications\NewApplicant; use App\Notifications\NewApplicant;
@ -59,11 +60,10 @@ class ApplicationController extends Controller
public function showUserApp(Request $request, Application $application) public function showUserApp(Request $request, Application $application)
{ {
if (!$request->wantsJson()) 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(
[ [
@ -92,8 +92,7 @@ class ApplicationController extends Controller
public function showAllApps(Request $request) public function showAllApps(Request $request)
{ {
if (!$request->wantsJson()) if (!$request->wantsJson()) {
{
$this->authorize('viewAny', Application::class); $this->authorize('viewAny', Application::class);
return view('dashboard.appmanagement.all') return view('dashboard.appmanagement.all')
@ -115,7 +114,7 @@ class ApplicationController extends Controller
$firstVacancy = $vacancyWithForm->first(); $firstVacancy = $vacancyWithForm->first();
if (! $vacancyWithForm->isEmpty() && $firstVacancy->vacancyCount !== 0 && $firstVacancy->vacancyStatus == 'OPEN') { if (!$vacancyWithForm->isEmpty() && $firstVacancy->vacancyCount !== 0 && $firstVacancy->vacancyStatus == 'OPEN') {
return view('dashboard.application-rendering.apply') return view('dashboard.application-rendering.apply')
->with([ ->with([
'vacancy' => $vacancyWithForm->first(), 'vacancy' => $vacancyWithForm->first(),
@ -130,35 +129,67 @@ class ApplicationController extends Controller
{ {
$vacancy = Vacancy::with('forms')->where('vacancySlug', $vacancySlug)->get(); $vacancy = Vacancy::with('forms')->where('vacancySlug', $vacancySlug)->get();
if ($vacancy->first()->vacancyCount == 0 || $vacancy->first()->vacancyStatus !== 'OPEN') { if ($vacancy->isEmpty()) {
$request->session()->flash('error', 'This application is unavailable.'); 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(); 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');
} }
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 = ContextAwareValidator::getResponseValidator($request->all(), $formStructure); $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();
// 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
Log::info('Built response & validator structure!'); Log::info('Built response & validator structure!');
if (! $responseValidation->get('validator')->fails()) { if (!$responseValidation->get('validator')->fails()) {
$response = Response::create([ $response = Response::create([
'responseFormID' => $vacancy->first()->forms->id, 'responseFormID' => $vacancy->first()->forms->id,
'associatedVacancyID' => $vacancy->first()->id, // Since a form can be used by multiple vacancies, we can only know which specific vacancy this response ties to by using a vacancy ID 'associatedVacancyID' => $vacancy->first()->id, // Since a form can be used by multiple vacancies, we can only know which specific vacancy this response ties to by using a vacancy ID
'responseData' => $responseValidation->get('responseStructure'), 'responseData' => $responseValidation->get('responseStructure'),
]); ]);
Log::info('Registered form response for user '.Auth::user()->name.' for vacancy '.$vacancy->first()->vacancyName); Log::info('Registered form response!', [
'applicant' => $applicant->name,
'vacancy' => $vacancy->first()->vacancyName
]);
$application = Application::create([ $application = Application::create([
'applicantUserID' => Auth::user()->id, 'applicantUserID' => $applicant->id,
'applicantFormResponseID' => $response->id, 'applicantFormResponseID' => $response->id,
'applicationStatus' => 'STAGE_SUBMITTED', 'applicationStatus' => 'STAGE_SUBMITTED',
]); ]);
Log::info('Submitted application for user '.Auth::user()->name.' with response ID'.$response->id); Log::info('Submitted an application!', [
'responseID' => $response->id,
'applicant' => $applicant->name
]);
foreach (User::all() as $user) { foreach (User::all() as $user) {
if ($user->hasRole('admin')) { if ($user->hasRole('admin')) {
@ -166,37 +197,73 @@ class ApplicationController extends Controller
} }
} }
$request->session()->flash('success', 'Thank you for your application! It will be reviewed as soon as possible.'); if ($request->wantsJson()) {
return JSON::setResponseType('success')
return redirect()->to(route('showUserApps')); ->setStatus('accepted')
} else { ->setMessage('Application submitted successfully. Please refer to the docs to add contact info if your submission was anonymous.')
Log::warning('Application form for '.Auth::user()->name.' contained errors, resetting!'); ->setCode(201)
$request->session()->flash('error', 'There are one or more errors in your application. Please make sure none of your fields are empty, since they are all required.'); ->setAdditional([
'links' => [
'application-web' => route('showUserApp', ['application' => $application->id])
]
])->build();
} }
return redirect()->back(); $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.');
} }
public function updateApplicationStatus(Request $request, Application $application, $newStatus) 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) { switch ($newStatus) {
case 'deny': case 'deny':
event(new ApplicationDeniedEvent($application)); event(new ApplicationDeniedEvent($application));
$message = "Application denied successfully.";
break; break;
case 'interview': case 'interview':
Log::info('User '.Auth::user()->name.' has moved application ID '.$application->id.'to interview stage'); Log::info(' Moved application ID ' . $application->id . 'to interview stage!');
$request->session()->flash('success', '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());
break; break;
default: default:
$request->session()->flash('error', 'There are no suitable statuses to update to. Do not mess with the URL.'); $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(); return redirect()->back();
@ -204,11 +271,22 @@ class ApplicationController extends Controller
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
$request->session()->flash('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.');
}
return redirect()->back();
$application->delete();
return JSON::setResponseType('success')
->setStatus('deleted')
->setMessage('Application deleted. Relationships were also nuked.')
->setCode(200)
->build();
} }
} }

View File

@ -19,6 +19,7 @@
* along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>. * along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
*/ */
use App\Http\Controllers\ApplicationController;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
@ -35,7 +36,15 @@ use Illuminate\Support\Facades\Route;
Route::middleware(['api'])->group(function (){ Route::middleware(['api'])->group(function (){
Route::get('applications', [\App\Http\Controllers\ApplicationController::class, 'showAllApps']);
Route::get('applications/view/{application}', [\App\Http\Controllers\ApplicationController::class, 'showUserApp']); Route::group(['prefix' => 'applications'], function () {
Route::get('/', [ApplicationController::class, 'showAllApps']);
Route::get('view/{application}', [ApplicationController::class, 'showUserApp']);
Route::post('apply/{vacancySlug}', [ApplicationController::class, 'saveApplicationAnswers']);
Route::patch('update/{application}/{newStatus}', [ApplicationController::class, 'updateApplicationStatus']);
Route::delete('delete/{application}', [ApplicationController::class, 'delete']);
});
}); });