1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Utils; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\ChangeStageRequest; |
6
|
|
|
use App\Mail\CandidateMailable; |
7
|
|
|
use App\Mail\NewCandidateNotification; |
8
|
|
|
use App\Models\Candidate; |
9
|
|
|
use App\Models\Message; |
10
|
|
|
use App\Models\PredefinedMessage; |
11
|
|
|
use Carbon\Carbon; |
12
|
|
|
use Illuminate\Support\Facades\Auth; |
13
|
|
|
use Illuminate\Support\Facades\Mail; |
14
|
|
|
|
15
|
|
|
class MessageService |
16
|
|
|
{ |
17
|
|
|
public static function sendConfirmationToCandidate($candidate) |
18
|
|
|
{ |
19
|
|
|
$predefinedMessage = PredefinedMessage::where('recruitment_id', $candidate->recruitment->id) |
20
|
|
|
->where('trigger', 'onApply')->first(); |
21
|
|
|
|
22
|
|
|
if (!$predefinedMessage) { |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$message = new Message(); |
27
|
|
|
$message->candidate_id = $candidate->id; |
|
|
|
|
28
|
|
|
$message->to = $candidate->email; |
29
|
|
|
//$message->from = ''; |
30
|
|
|
$message->subject = ContentParser::parse($predefinedMessage->subject, $candidate).' '.UtilsService::hashSuffix($candidate->id); |
31
|
|
|
$message->body = ContentParser::parse($predefinedMessage->body, $candidate); |
32
|
|
|
$message->save(); |
33
|
|
|
|
34
|
|
|
return self::send($message); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function sendMessage(Candidate $candidate, ChangeStageRequest $request) |
38
|
|
|
{ |
39
|
|
|
//request->user() |
40
|
|
|
$message = new Message(); |
41
|
|
|
$message->candidate_id = $candidate->id; |
|
|
|
|
42
|
|
|
$message->to = $candidate->email; |
43
|
|
|
// $message->from = ''; |
44
|
|
|
$message->subject = $request->get('message_subject'); |
|
|
|
|
45
|
|
|
$message->body = $request->get('message_body'); |
46
|
|
|
|
47
|
|
|
if ($request->get('delay_message_send')) { |
48
|
|
|
$delayUntil = Carbon::parse($request->get('delayed_message_date')); |
49
|
|
|
$message->scheduled_for = $delayUntil; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$message->save(); |
53
|
|
|
|
54
|
|
|
return self::send($message); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function notifyObservers(Candidate $candidate) |
58
|
|
|
{ |
59
|
|
|
$notificationEmail = $candidate->recruitment->notification_email; |
60
|
|
|
|
61
|
|
|
if (empty($notificationEmail)) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
Mail::to($notificationEmail)->queue(new NewCandidateNotification($candidate)); |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function notifyDeletedCandidate(Candidate $candidate, $emailAddress) |
71
|
|
|
{ |
72
|
|
|
$predefinedMessage = PredefinedMessage::where('trigger', 'onDelete')->first(); |
73
|
|
|
|
74
|
|
|
if (!$predefinedMessage) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$message = new Message(); |
79
|
|
|
$message->candidate_id = $candidate->id; |
|
|
|
|
80
|
|
|
$message->to = $emailAddress; |
81
|
|
|
//$message->from = ''; |
82
|
|
|
$message->subject = ContentParser::parse($predefinedMessage->subject, $candidate, null, Auth::user()); // . ' ' . UtilsService::hashSuffix($candidate->id); |
83
|
|
|
$message->body = ContentParser::parse($predefinedMessage->body, $candidate, null, Auth::user()); |
84
|
|
|
$message->save(); |
85
|
|
|
|
86
|
|
|
return self::send($message); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected static function send(Message $message) |
90
|
|
|
{ |
91
|
|
|
if ($message->scheduled_for) { |
92
|
|
|
Mail::to($message->to)->later($message->scheduled_for, new CandidateMailable($message)); |
|
|
|
|
93
|
|
|
} else { |
94
|
|
|
Mail::to($message->to)->queue(new CandidateMailable($message)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|