| 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\Resources\JobPoster as JobPosterResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Mail\JobPosterReviewRequested; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use App\Models\JobPoster; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use App\Models\Lookup\JobTerm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Support\Facades\Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Illuminate\Support\Facades\Log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Illuminate\Support\Facades\Mail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Jenssegers\Date\Date; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use App\Http\Requests\UpdateJobPoster; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use App\Http\Requests\StoreJobPoster; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Illuminate\Support\Facades\Gate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use App\Models\Criteria; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | class JobController extends Controller | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * Class constructor | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         // This applies the appropriate policy to each resource route. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |         $this->authorizeResource(JobPoster::class, 'job'); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * Return the list of all jobs the user is authorized to view, | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * using all query parameters as search filters. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @param Request $request | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     public function index(Request $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $jobs = JobPoster::where($request->query())->get(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $viewableJobs = $jobs->filter(function ($job) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             return Gate::allows('view', $job); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         })->values(); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         return response()->json($viewableJobs->map(function ($job) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             return new JobPosterResource($job); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         })); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * Store a newly created resource in storage. | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @param  \App\Http\Requests\StoreJobPoster $request Incoming request. | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     public function store(StoreJobPoster $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $data = $request->validated(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $job = new JobPoster(); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $job->manager_id = $request->user()->manager->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         // Defaulting JPB created Jobs to monthly terms for now. | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $job->job_term_id = JobTerm::where('name', 'month')->value('id'); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $job->fill($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $job->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         return new JobPosterResource($job); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * Display the specified resource. | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @param  \App\Models\JobPoster $job Incoming Job Poster. | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     public function show(JobPoster $job) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         return new JobPosterResource($job); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * Update the specified resource in storage. | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param  \App\Http\Requests\UpdateJobPoster $request Validates input. | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @param  \App\Models\JobPoster              $job     Incoming Job Poster. | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function update(UpdateJobPoster $request, JobPoster $job) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $data = $request->validated(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // Only values both in the JobPoster->fillable array, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // and returned by UpdateJobPoster->validatedData(), will be set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $job->fill($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // Defaulting JPB updated jobs to monthly for now. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $job->job_term_id = JobTerm::where('name', 'month')->value('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $job->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $job->fresh(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |         return new JobPosterResource($job); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * Remove the specified resource from storage. | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @param  integer $id Job Poster ID. | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     public function destroy(int $id) | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         // TODO: complete. | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * Submit the Job Poster for review. | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      * @param  \App\Models\JobPoster $job Job Poster object. | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public function submitForReview(JobPoster $job) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         // Check to avoid submitting for review multiple times. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         if ($job->review_requested_at === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             // Update review request timestamp. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $job->review_requested_at = new Date(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $job->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             // Send email. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $reviewer_email = config('mail.reviewer_email'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             if (isset($reviewer_email)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 Mail::to($reviewer_email)->send(new JobPosterReviewRequested($job, Auth::user())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 Log::error('The reviewer email environment variable is not set.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  |         return new JobPosterResource($job); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 134 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 135 |  |  |  |