· 7 years ago · Sep 22, 2018, 10:14 PM
1class ActiveBreakController extends Controller
2{
3 public function create(Request $request)
4 {
5 /**
6 * Todo note
7 *
8 * Set up a middleware that will check if the Break list
9 * is in panic mode, if so find a way to display the
10 * error that it is closed elegantly with the sweetalerts
11 */
12 $this->validate(
13 $request,
14 [
15 // Ensure that the slack user exists in the agents table.
16 'slack_user' => 'required|exists:agents,slack_user',
17 ],
18 // Define the custom error messages to display if validation fails.
19 $messages = [
20 'slack_user.required' => 'Your slack username is required; failure to provide your slack username, results in Daddy not knowing who you are!',
21 'slack_user.exists' => 'BreakDaddy, doesn\'t know who you are. Reach out to your Supervisor or TeamLead to get added to my list of children.'
22 ]
23 );
24
25 // Validation has passed, so lets go ahead and create the Break Request.
26 $token = Helpers::generateBreakToken(); // Generate the random hash token.
27 $agent = Agent::select('id', 'role', 'slack_user')->where('slack_user', $request->input('slack_user'))->first(); // Grab the information needed to import it into the database
28 $clearing = ActiveBreak::getClearing()->with('agent')->get(); // Grab all breaks that are set to clearing with the agent
29 $breaking = ActiveBreak::getBreaking()->with('agent')->get(); // Grab all breaks that are set to breaking with the agent data
30 $getAllBreaks = ActiveBreak::with('agent')->get(); // Grab all the break data, to check if they already have a break. (May need to review at a later date to see if there is a way to cut down queries.)
31
32 // All is set, lets check if they already have a request in.
33 // If they do lets give them the Uh Oh!
34 if ($getAllBreaks) {
35 foreach ($getAllBreaks as $breaks) {
36 if ($breaks->agent->slack_user === $agent->slack_user) {
37 $data = [
38 'message' => "Whoops, looks like you already have a break request in.",
39 'title' => 'Uh Oh!',
40 'icon' => 'error'
41 ];
42
43 return ['data' => $data];
44 }
45 }
46 }
47
48 // Check the clearing queue, if anyone with the role of the person reqesting
49 // a break is clearing, then queue them up, for the next break.
50 if ($clearing) {
51 foreach ($clearing as $clearing) {
52 if ($clearing->agent->role === $agent->role) {
53 ActiveBreak::create(
54 [
55 'agent_id' => $agent->id,
56 'token' => $token,
57 ]
58 );
59
60 $data = [
61 'message' => "Break request has been submitted. At this time, you are queued for break. Once it is time you will be slacked and provided your hash code.",
62 'title' => 'Break Request Accepted.',
63 'icon' => 'success'
64 ];
65
66 return ['data' => $data];
67 // Else check if there is anyone breaking.
68 }
69 }
70 }
71 if ($breaking) {
72 foreach ($breaking as $breaking) {
73 if ($breaking->agent->role === $agent->role) {
74 ActiveBreak::create(
75 [
76 'agent_id' => $agent->id,
77 'token' => $token,
78 ]
79 );
80
81 $data = [
82 'message' => "Break request has been submitted. At this time, you are queued for break. Once it is time you will be slacked and provided your hash code.",
83 'title' => 'Break Request Accepted.',
84 'icon' => 'success'
85 ];
86
87 return ['data' => $data];
88 }
89 }
90 }
91 // If there is no one on that role clearing OR breaking, then go ahead and set them to clearing.
92 $break = ActiveBreak::create(
93 [
94 'agent_id' => $agent->id,
95 'status' => 'clearing',
96 'token' => $token,
97 ]
98 );
99 // Grab the data from the database, from the last inserted ID;
100 $breakData = ActiveBreak::select('agent_id', 'token')->with('agent')->where('id', $break->id)->first();
101
102 $breakDaddy = User::find(1);
103 $breakDaddy->notify(new AgentIsClearing($breakData)); // Send slack notice to the user, with their hash code.
104 $breakDaddy->notify(new AgentClearingLeader($breakData)); // Send notice to the leadership channel
105
106 $data = [
107 'message' => "Break Request Submitted, I will slack your hashcode!",
108 'title' => 'Break Request Accepted.',
109 'icon' => 'success'
110 ];
111
112 return ['data' => $data];
113 }