Passed
Push — feature/micro-ref-email ( e2ffc1...2e0a82 )
by Tristan
04:28 queued 12s
created

ApplicationController::setAvailability()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 17
rs 9.9
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Resources\JobApplication as JobApplicationResource;
7
use App\Models\ApplicationReview;
8
use App\Models\JobApplication;
9
use App\Models\JobPoster;
10
use App\Models\Lookup\ApplicationStatus;
11
use App\Models\Lookup\Department;
12
use App\Models\Lookup\ReviewStatus;
13
use Illuminate\Http\Request;
14
use Illuminate\Http\Resources\Json\JsonResource;
15
use Illuminate\Validation\Rule;
16
17
class ApplicationController extends Controller
18
{
19
    /**
20
     * Return a single Job Application.
21
     *
22
     * @param JobApplication $application Incoming Job Application object.
23
     *
24
     * @return mixed
25
     */
26
    public function show(JobApplication $application)
27
    {
28
        $application->loadMissing('applicant', 'application_review', 'citizenship_declaration', 'veteran_status');
29
        return new JobApplicationResource($application);
30
    }
31
32
    /**
33
     * Return all Job Applications for a given Job Poster.
34
     *
35
     * @param JobPoster $jobPoster Incoming Job Poster object.
36
     *
37
     * @return mixed
38
     */
39
    public function index(JobPoster $jobPoster)
40
    {
41
        $applications = $jobPoster->submitted_applications()
42
            ->with([
43
                'applicant.user',
44
                'application_review.department',
45
                'citizenship_declaration',
46
                'veteran_status'
47
            ])
48
            ->get();
49
50
        return JobApplicationResource::collection($applications);
51
    }
52
53
    /**
54
     * Update a single Job Application Review.
55
     *
56
     * @param Request        $request     Incoming request object.
57
     * @param JobApplication $application Incoming Job Application object.
58
     *
59
     * @return mixed
60
     */
61
    public function updateReview(Request $request, JobApplication $application)
62
    {
63
        $strategicResponseDepartmentId = config('app.strategic_response_department_id');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        $strategicResponseDepartmentId = /** @scrutinizer ignore-call */ config('app.strategic_response_department_id');
Loading history...
64
        $availabilityStatuses = ReviewStatus::whereIn('name', ['allocated', 'not_available'])->get()->pluck('id');
65
66
        $request->validate([
67
            'review_status_id' => [
68
                'nullable',
69
                Rule::in(ReviewStatus::all()->pluck('id')->toArray())
70
            ],
71
            'department_id' => [
72
                'nullable',
73
                Rule::in(Department::all()->pluck('id')->toArray())
74
            ],
75
            'notes' => 'nullable|string'
76
        ]);
77
78
        $review = $application->application_review;
79
        if ($review === null) {
80
            $review = new ApplicationReview();
81
            $review->job_application()->associate($application);
82
        }
83
        $review->fill([
84
            'review_status_id' => $request->input('review_status_id'),
85
            'department_id' => $request->input('department_id'),
86
            'notes' => $request->input('notes'),
87
        ]);
88
        $review->save();
89
90
        if (
91
            $application->job_poster->department_id === $strategicResponseDepartmentId
92
            && in_array($review->review_status_id, $availabilityStatuses->toArray())
93
        ) {
94
            $this->setAvailability($application);
95
        }
96
97
        $review->fresh();
98
        $review->loadMissing('department');
99
100
        return new JsonResource($review);
101
    }
102
103
    /**
104
     * Sets the review status for any other application reviews
105
     * belonging to a given Applicant to `not_available`.
106
     * Designed to be used for the Strategic Talent Response screening,
107
     * but could be repurposed.
108
     *
109
     * @param JobApplication $application Incoming Job Application object.
110
     *
111
     * @return void
112
     */
113
    protected function setAvailability(JobApplication $application)
114
    {
115
        $departmentId = $application->job_poster->department_id;
116
        $submittedStatusId = ApplicationStatus::where('name', 'submitted')->value('id');
117
        $unavailableStatusId = ReviewStatus::where('name', 'not_available')->value('id');
118
119
        // This is kinda gross, but it seems to filter out the correct subset, and allows a single database call
120
        // to perform the update. Seems better than returning a result set, looping, and pushing individual changes.
121
        ApplicationReview::whereHas('job_application.job_poster', function ($query) use ($departmentId) {
122
            $query->where('department_id', $departmentId);
123
        })->whereHas('job_application', function ($query) use ($application, $submittedStatusId) {
124
            $query->where([
125
                ['id', '<>', $application->id],
126
                ['application_status_id', $submittedStatusId],
127
                ['applicant_id', $application->applicant->id]
128
            ]);
129
        })->update(['review_status_id' => $unavailableStatusId]);
130
    }
131
}
132