feat: added eligibility check to application save method

This commit addresses an issue where users could submit as many applications as they wanted by simply navigating to the previous page and resubmitting the form, therefore bypassing validation that was only existent in the  front end.
Fixes #20.
This commit is contained in:
Miguel Nogueira 2022-09-02 00:00:28 +01:00
parent b96a20a0a9
commit 667425e4e3
No known key found for this signature in database
GPG Key ID: 3C6A7E29AF26D370
2 changed files with 15 additions and 12 deletions

View File

@ -96,8 +96,8 @@ class ApplicationController extends Controller
public function saveApplicationAnswers(Request $request, $vacancySlug)
{
if (Auth::user()->isEligible()) {
try {
$this->applicationService->fillForm(Auth::user(), $request->all(), $vacancySlug);
} catch (VacancyNotFoundException | IncompleteApplicationException | UnavailableApplicationException $e) {
@ -112,6 +112,11 @@ class ApplicationController extends Controller
->with('success', __('Thank you! Your application has been processed and our team will get to it shortly.'));
}
return redirect()
->to(route('showUserApps'))
->with('error', __('Your account is not eligible to submit a new application.'));
}
public function updateApplicationStatus(Request $request, Application $application, $newStatus)
{
$messageIsError = false;

View File

@ -121,19 +121,17 @@ class User extends Authenticatable implements MustVerifyEmail
public function isEligible(): bool {
$eligible = false;
$lastApplication = Application::where('applicantUserID', $this->id)->latest()->first();
$lastApplication = Application::where('applicantUserID', $this->getAttribute('id'))->latest()->first();
if (is_null($lastApplication)) {
$eligible = true;
return true;
}
if ($lastApplication->created_at->diffInMonths(now()) > 1 && in_array($lastApplication->applicationStatus, ['DENIED', 'APPROVED'])) {
$eligible = true;
return true;
}
return $eligible;
return false;
}