|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Exceptions\StateMachineException; |
|
6
|
|
|
use App\Models\JobPoster; |
|
7
|
|
|
use App\Models\JobPosterStatusHistory; |
|
8
|
|
|
use App\Models\Lookup\JobPosterStatus; |
|
9
|
|
|
use App\Services\JobStatusTransitions; |
|
10
|
|
|
use Illuminate\Http\Request; |
|
11
|
|
|
|
|
12
|
|
|
class JobStatusController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
protected function transitionJobStatus(Request $request, JobPoster $job, string $to) |
|
15
|
|
|
{ |
|
16
|
|
|
$transitions = new JobStatusTransitions(); |
|
17
|
|
|
|
|
18
|
|
|
$user = $request->user(); |
|
19
|
|
|
$fromStatus = $job->job_poster_status; |
|
20
|
|
|
$from = $fromStatus->name; |
|
21
|
|
|
|
|
22
|
|
|
// Ensure state transition is legal. |
|
23
|
|
|
if (!$transitions->canTransition($user, $from, $to)) { |
|
24
|
|
|
throw new StateMachineException('Illegal state transition'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// Save new status on job. |
|
28
|
|
|
$toStatus = JobPosterStatus::where('name', $to)->first(); |
|
29
|
|
|
$job->job_poster_status_id = $toStatus->id; |
|
30
|
|
|
$job->save(); |
|
31
|
|
|
|
|
32
|
|
|
// Save transition history. |
|
33
|
|
|
$transition = new JobPosterStatusHistory(); |
|
34
|
|
|
$transition->job_poster_id = $job->id; |
|
35
|
|
|
$transition->user_id = $user->id; |
|
36
|
|
|
$transition->from_job_poster_status_id = $fromStatus->id; |
|
37
|
|
|
$transition->to_job_poster_status_id = $toStatus->id; |
|
38
|
|
|
$transition->save(); |
|
39
|
|
|
|
|
40
|
|
|
return $request->ajax() |
|
41
|
|
|
? response()->json(['status' => 'ok']) |
|
|
|
|
|
|
42
|
|
|
: back(); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function setJobStatus(Request $request, JobPoster $jobPoster, string $status) |
|
46
|
|
|
{ |
|
47
|
|
|
// $status = $request->input('status'); |
|
48
|
|
|
return $this->transitionJobStatus($request, $jobPoster, $status); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|