1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use App\Http\Requests\UpdateApplicantProfile; |
8
|
|
|
use App\Http\Resources\ApplicantProfile as ApplicantProfileResource; |
9
|
|
|
use App\Http\Resources\Applicant as ApplicantResource; |
10
|
|
|
use App\Models\Applicant; |
11
|
|
|
use App\Models\ApplicantClassification; |
12
|
|
|
use Illuminate\Database\Eloquent\Builder; |
13
|
|
|
use App\Services\Validation\Rules\ValidClassificationRule; |
14
|
|
|
|
15
|
|
|
class ApplicantController extends Controller |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
public function index(Request $request) |
19
|
|
|
{ |
20
|
|
|
$maxLimit = config('app.api_max_limit'); |
|
|
|
|
21
|
|
|
$request->validate([ |
22
|
|
|
'limit' => "sometimes|integer|max:$maxLimit", |
23
|
|
|
'offset' => 'sometimes|integer|nullable', |
24
|
|
|
'skillIds' => 'sometimes|regex:/^[0-9]+(,[0-9]+)*$/', |
25
|
|
|
'classifications' => ['sometimes', 'string', new ValidClassificationRule()], |
26
|
|
|
]); |
27
|
|
|
|
28
|
|
|
$limit = $request->query('limit', $maxLimit); |
29
|
|
|
if ($limit > $maxLimit) { |
30
|
|
|
$limit = $maxLimit; |
31
|
|
|
} |
32
|
|
|
$offset = $request->query('offset', 0); |
33
|
|
|
|
34
|
|
|
$skillIds = preg_split('/,/', $request->query('skill_ids', ''), null, PREG_SPLIT_NO_EMPTY); |
35
|
|
|
$classifications = preg_split('/,/', $request->query('classifications', ''), null, PREG_SPLIT_NO_EMPTY); |
36
|
|
|
|
37
|
|
|
$query = Applicant::limit($limit)->offset($offset); |
38
|
|
|
foreach ($skillIds as $skillId) { |
39
|
|
|
$query->whereHas('skills', function (Builder $query) use ($skillId) { |
40
|
|
|
$query->where('skills.id', $skillId); |
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
if (count($classifications) > 0) { |
44
|
|
|
$query->where(function ($query) use ($classifications) { |
45
|
|
|
foreach ($classifications as $classification) { |
46
|
|
|
$values = explode('-', $classification); |
47
|
|
|
$classificationCode = strtoupper($values[0]); |
48
|
|
|
$classificationLevel = $values[1]; |
49
|
|
|
$query->orWhereHas('applicant_classifications', function (Builder $query) use ($classificationCode, $classificationLevel) { |
50
|
|
|
$query->where('applicant_classifications.level', (int)$classificationLevel) |
51
|
|
|
->whereHas('classification', function (Builder $query) use ($classificationCode) { |
52
|
|
|
$query->where('key', $classificationCode); |
53
|
|
|
}); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return ApplicantResource::collection($query->get()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieve Applicant profile. |
64
|
|
|
* |
65
|
|
|
* @param Applicant $applicant Incoming Applicant object. |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function getProfile(Applicant $applicant) |
70
|
|
|
{ |
71
|
|
|
$applicant->loadMissing('applicant_classifications'); |
72
|
|
|
return new ApplicantProfileResource($applicant); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Update Applicant profile. |
77
|
|
|
* |
78
|
|
|
* @param UpdateApplicantProfile $request Form Validation casted request object. |
79
|
|
|
* @param Applicant $applicant Incoming Applicant object. |
80
|
|
|
*/ |
81
|
|
|
public function updateProfile(UpdateApplicantProfile $request, Applicant $applicant) |
82
|
|
|
{ |
83
|
|
|
$validatedRequest = $request->validated(); |
84
|
|
|
// If there are no applicant classifications in the request, |
85
|
|
|
// then delete all applicant classifications attached to applicant. |
86
|
|
|
if (!array_key_exists('applicant_classifications', $validatedRequest)) { |
87
|
|
|
$applicant->applicant_classifications()->delete(); |
88
|
|
|
} else { |
89
|
|
|
$newApplicantClassifications = collect($validatedRequest['applicant_classifications'])->unique( |
|
|
|
|
90
|
|
|
// Remove all duplicate classification-level combinations from the collection. |
91
|
|
|
function ($newApplicantClassification) { |
92
|
|
|
return $newApplicantClassification['classification_id'].$newApplicantClassification['level']; |
93
|
|
|
} |
94
|
|
|
); |
95
|
|
|
$oldApplicantClassifications = $applicant->applicant_classifications; |
96
|
|
|
|
97
|
|
|
// Delete old applicant classifications that were not resubmitted. |
98
|
|
|
foreach ($oldApplicantClassifications as $oldApplicantClassification) { |
99
|
|
|
$newApplicantClassification = $newApplicantClassifications->firstWhere( |
100
|
|
|
'id', |
101
|
|
|
$oldApplicantClassification['id'] |
102
|
|
|
); |
103
|
|
|
if ($newApplicantClassification === null) { |
104
|
|
|
$oldApplicantClassification->delete(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
// Update old applicant classifications and/or create them if it doesn't exist. |
110
|
|
|
$newApplicantClassifications->map(function ($newApplicantClassification) use ($oldApplicantClassifications) { |
111
|
|
|
$applicantClassification = $oldApplicantClassifications->firstWhere( |
112
|
|
|
'id', |
113
|
|
|
$newApplicantClassification['id'] |
114
|
|
|
); |
115
|
|
|
if (!$applicantClassification) { |
116
|
|
|
$applicantClassification = new ApplicantClassification(); |
117
|
|
|
} |
118
|
|
|
$applicantClassification->applicant_id = $newApplicantClassification['applicant_id']; |
119
|
|
|
$applicantClassification->classification_id = $newApplicantClassification['classification_id']; |
120
|
|
|
$applicantClassification->fill($newApplicantClassification); |
121
|
|
|
$applicantClassification->save(); |
122
|
|
|
}); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$applicant->citizenship_declaration_id = $validatedRequest['citizenship_declaration_id']; |
126
|
|
|
$applicant->veteran_status_id = $validatedRequest['veteran_status_id']; |
127
|
|
|
$applicant->save(); |
128
|
|
|
|
129
|
|
|
$applicant->refresh(); |
130
|
|
|
$applicant->loadMissing('applicant_classifications'); |
131
|
|
|
|
132
|
|
|
return new ApplicantProfileResource($applicant); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|