1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\CandidateDeleteRequest; |
6
|
|
|
use App\Http\Requests\CandidatesCreateRequest; |
7
|
|
|
use App\Http\Requests\CandidatesListRequest; |
8
|
|
|
use App\Http\Requests\CandidateUpdateRequest; |
9
|
|
|
use App\Http\Requests\ChangeStageRequest; |
10
|
|
|
use App\Http\Resources\CandidateResource; |
11
|
|
|
use App\Http\Resources\TruncatedCandidateResource; |
12
|
|
|
use App\Models\Candidate; |
13
|
|
|
use App\Repositories\CandidatesRepository; |
14
|
|
|
use App\Services\TenantManager; |
15
|
|
|
use App\Utils\Candidates\CandidateCreator; |
16
|
|
|
use App\Utils\Candidates\CandidateDeleter; |
17
|
|
|
use App\Utils\Candidates\CandidateUpdater; |
18
|
|
|
use App\Utils\Candidates\StageChanger; |
19
|
|
|
use Illuminate\Http\Request; |
20
|
|
|
use Illuminate\Support\Facades\Auth; |
21
|
|
|
use Illuminate\Support\Facades\Storage; |
22
|
|
|
|
23
|
|
|
class CandidatesController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TenantManager |
27
|
|
|
*/ |
28
|
|
|
protected $tenantManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new controller instance. |
32
|
|
|
* |
33
|
|
|
* @param TenantManager $tenantManager |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function __construct(TenantManager $tenantManager) |
38
|
|
|
{ |
39
|
|
|
$this->tenantManager = $tenantManager; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function create(CandidatesCreateRequest $request) |
43
|
|
|
{ |
44
|
|
|
$candidate = CandidateCreator::createCandidate($request, $this->tenantManager); |
45
|
|
|
|
46
|
|
|
return response()->json($candidate, 201); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function list(CandidatesListRequest $request) |
50
|
|
|
{ |
51
|
|
|
$recruitmentId = $request->get('recruitmentId'); |
|
|
|
|
52
|
|
|
$user = Auth::user(); |
53
|
|
|
|
54
|
|
|
if ($request->get('search')) { |
55
|
|
|
$candidates = CandidatesRepository::search($request->validated()); |
56
|
|
|
} elseif ($recruitmentId) { |
57
|
|
|
$candidates = Candidate::where('recruitment_id', $recruitmentId)->with('recruitment')->orderBy('created_at', 'DESC')->get(); |
58
|
|
|
} else { |
59
|
|
|
$candidates = Candidate::with('recruitment')->orderBy('created_at', 'DESC')->get(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!$user->can('read all recruitments')) { |
63
|
|
|
$filtered = $candidates->filter( |
64
|
|
|
function ($candidate, $key) use ($user) { |
|
|
|
|
65
|
|
|
return $user->can('view', $candidate); |
66
|
|
|
} |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$candidates = $filtered; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return TruncatedCandidateResource::collection($candidates); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// DEPRECATED |
76
|
|
|
// public function names(Request $request) |
77
|
|
|
// { |
78
|
|
|
// $search = $request->get('search'); |
79
|
|
|
// $columns = ['id', 'name']; |
80
|
|
|
// |
81
|
|
|
// if ($search) { |
82
|
|
|
// $candidates = CandidatesRepository::search(['search' => $search], $columns); |
83
|
|
|
// } else { |
84
|
|
|
// $candidates = Candidate::select($columns)->get(); |
85
|
|
|
// } |
86
|
|
|
// return response()->json($candidates); |
87
|
|
|
// } |
88
|
|
|
|
89
|
|
|
public function get(Candidate $candidate) |
90
|
|
|
{ |
91
|
|
|
return new CandidateResource($candidate); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function update(CandidateUpdateRequest $request, Candidate $candidate) |
95
|
|
|
{ |
96
|
|
|
$candidate = CandidateUpdater::updateCandidate($candidate->id, $request); |
97
|
|
|
|
98
|
|
|
return response()->json($candidate, 200, ['Location' => '/candidates/'.$candidate->id]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function delete(CandidateDeleteRequest $request, Candidate $candidate) |
102
|
|
|
{ |
103
|
|
|
CandidateDeleter::deleteCandidate($request, $candidate->id); |
104
|
|
|
|
105
|
|
|
return response()->json(null, 200); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function hasBeenSeen(Candidate $candidate) |
109
|
|
|
{ |
110
|
|
|
if (!$candidate->seen_at) { |
111
|
|
|
$candidate->seen_at = new \DateTime(); |
112
|
|
|
$candidate->save(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return response()->json($candidate, 200, ['Location' => '/candidates/'.$candidate->id]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
//TODO: route nie chroniony - docelowo zrobić zabezpieczenie z użyciem jednorazowych tokenów, a także nie przekazywać do klienta pola path_to_cv |
119
|
|
|
public function cv(Request $request, $candidateId) |
120
|
|
|
{ |
121
|
|
|
$download = $request->get('download'); |
|
|
|
|
122
|
|
|
$candidate = Candidate::find($candidateId); |
123
|
|
|
|
124
|
|
|
if (!Storage::disk('s3')->exists($candidate->path_to_cv)) { |
|
|
|
|
125
|
|
|
return response('File not found', 404); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$fileName = str_replace(' ', '_', $candidate->name).'-CV.pdf'; //TODO sanitize |
129
|
|
|
|
130
|
|
|
if ($download) { |
131
|
|
|
return Storage::disk('s3')->download($candidate->path_to_cv, $fileName); |
|
|
|
|
132
|
|
|
} else { |
133
|
|
|
return Storage::disk('s3')->response($candidate->path_to_cv, $fileName); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function changeStage(ChangeStageRequest $request) |
138
|
|
|
{ |
139
|
|
|
$candidate = StageChanger::changeStage($request); |
140
|
|
|
|
141
|
|
|
return response()->json($candidate, 200, ['Location' => '/candidates/'.$candidate->id]); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.