1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use App\Exceptions\SendEmailException; |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use App\Mail\MicroReferenceMail; |
8
|
|
|
use App\Models\ApplicationReview; |
9
|
|
|
use App\Models\JobApplication; |
10
|
|
|
use Illuminate\Support\Facades\Lang; |
11
|
|
|
use Illuminate\Support\Facades\Mail; |
12
|
|
|
|
13
|
|
|
class MicroReferenceController extends Controller |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get all reference emails created for an application. |
18
|
|
|
* |
19
|
|
|
* @param JobApplication $application The application the references are for. |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\Http\Response |
22
|
|
|
*/ |
23
|
|
|
public function index(JobApplication $application) |
24
|
|
|
{ |
25
|
|
|
$directorMail = new MicroReferenceMail($application, true); |
26
|
|
|
$referenceMail = new MicroReferenceMail($application, false); |
27
|
|
|
$mails = [ |
28
|
|
|
'director' => $directorMail->toArray(), |
29
|
|
|
'secondary' => $referenceMail->toArray(), |
30
|
|
|
]; |
31
|
|
|
return response()->json($mails); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get email created to be sent to an application's Director reference. |
36
|
|
|
* |
37
|
|
|
* @param JobApplication $application The application the reference is for. |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Http\Response |
40
|
|
|
*/ |
41
|
|
|
public function showDirectorEmail(JobApplication $application) |
42
|
|
|
{ |
43
|
|
|
$mail = new MicroReferenceMail($application, true); |
44
|
|
|
return response()->json($mail->toArray()); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function sendDirectorEmail(JobApplication $application) |
48
|
|
|
{ |
49
|
|
|
$mail = new MicroReferenceMail($application, true); |
50
|
|
|
$mail->build(); |
51
|
|
|
if ($mail->to === []) { |
52
|
|
|
throw new SendEmailException(Lang::get('errors.email_missing_delivery_address')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
Mail::send($mail); |
56
|
|
|
|
57
|
|
|
$review = $application->application_review; |
58
|
|
|
if ($review === null) { |
59
|
|
|
$review = new ApplicationReview(); |
60
|
|
|
$review->job_application()->associate($application); |
61
|
|
|
} |
62
|
|
|
$review->director_email_sent = true; |
63
|
|
|
$review->save(); |
64
|
|
|
|
65
|
|
|
// Recreate the mail, because being calling ->toArray() after ->build() can cause some fields to double. |
66
|
|
|
$mail = new MicroReferenceMail($application, true); |
67
|
|
|
return response()->json($mail->toArray()); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get email created to be sent to an application's Secondary reference. |
72
|
|
|
* |
73
|
|
|
* @param JobApplication $application The application the reference is for. |
74
|
|
|
* |
75
|
|
|
* @return \Illuminate\Http\Response |
76
|
|
|
*/ |
77
|
|
|
public function showSecondaryReferenceEmail(JobApplication $application) |
78
|
|
|
{ |
79
|
|
|
$mail = new MicroReferenceMail($application, false); |
80
|
|
|
return response()->json($mail->toArray()); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function sendSecondaryReferenceEmail(JobApplication $application) |
84
|
|
|
{ |
85
|
|
|
$mail = new MicroReferenceMail($application, false); |
86
|
|
|
$mail->build(); |
87
|
|
|
if ($mail->to === []) { |
88
|
|
|
throw new SendEmailException(Lang::get('errors.email_missing_delivery_address')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
Mail::send($mail); |
92
|
|
|
|
93
|
|
|
$review = $application->application_review; |
94
|
|
|
if ($review === null) { |
95
|
|
|
$review = new ApplicationReview(); |
96
|
|
|
$review->job_application()->associate($application); |
97
|
|
|
} |
98
|
|
|
$review->reference_email_sent = true; |
99
|
|
|
$review->save(); |
100
|
|
|
|
101
|
|
|
// Recreate the mail, because being calling ->toArray() after ->build() can cause some fields to double. |
102
|
|
|
$mail = new MicroReferenceMail($application, false); |
103
|
|
|
return response()->json($mail->toArray()); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|