| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Tickets; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Attachment; |
| 6 |
use FluentSupport\App\Models\Conversation; |
| 7 |
use FluentSupport\Framework\Response\Response; |
| 8 |
use FluentSupport\Framework\Support\Arr; |
| 9 |
|
| 10 |
class ResponseService |
| 11 |
{ |
| 12 |
public function createResponse($data, $person, $ticket) |
| 13 |
{ |
| 14 |
if (empty($data['content'])) { |
| 15 |
return false; |
| 16 |
} |
| 17 |
|
| 18 |
$convoType = Arr::get($data, 'conversation_type', 'response'); |
| 19 |
|
| 20 |
$content = wp_unslash(wp_kses_post($data['content'])); |
| 21 |
|
| 22 |
$response = [ |
| 23 |
'person_id' => $person->id, |
| 24 |
'ticket_id' => $ticket->id, |
| 25 |
'conversation_type' => $convoType, |
| 26 |
'content' => $content, |
| 27 |
'source' => Arr::get($data, 'source', 'web'), |
| 28 |
'content_hash' => md5($content) |
| 29 |
]; |
| 30 |
|
| 31 |
if(!empty($data['message_id'])) { |
| 32 |
$response['message_id'] = sanitize_text_field($data['message_id']); |
| 33 |
} |
| 34 |
|
| 35 |
$response = apply_filters('fluent_support/new_' . $person->person_type . '_' . $convoType, $response, $ticket, $person); |
| 36 |
|
| 37 |
$createdResponse = \FluentSupport\App\Models\Conversation::create($response); |
| 38 |
$createdResponse->load('person'); |
| 39 |
|
| 40 |
if ($attachments = Arr::get($data, 'attachments', [])) { |
| 41 |
Attachment::where('ticket_id', $ticket->id) |
| 42 |
->whereIn('file_hash', $attachments) |
| 43 |
->update([ |
| 44 |
'conversation_id' => $createdResponse->id, |
| 45 |
'status' => 'active' |
| 46 |
]); |
| 47 |
$createdResponse->load('attachments'); |
| 48 |
} |
| 49 |
|
| 50 |
if ($person->person_type == 'agent' && $ticket->status == 'new' && $convoType == 'response') { |
| 51 |
$ticket->status = 'active'; |
| 52 |
if ($ticket->created_at) { |
| 53 |
$ticket->first_response_time = strtotime(current_time('mysql')) - strtotime($ticket->created_at); |
| 54 |
} else { |
| 55 |
$ticket->first_response_time = 300; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
$agentAdded = false; |
| 60 |
$updateData = []; |
| 61 |
|
| 62 |
if ($person->person_type == 'agent') { |
| 63 |
if (!$ticket->agent_id && $convoType == 'response') { |
| 64 |
$ticket->agent_id = $person->id; |
| 65 |
$agentAdded = true; |
| 66 |
$updateData = [ |
| 67 |
'agent_id' => $person->id |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
if ($convoType == 'response') { |
| 72 |
$ticket->last_agent_response = current_time('mysql'); |
| 73 |
$ticket->waiting_since = current_time('mysql'); |
| 74 |
} |
| 75 |
} else { |
| 76 |
|
| 77 |
if ($ticket->last_agent_response && strtotime($ticket->last_agent_response) > strtotime($ticket->last_customer_response)) { |
| 78 |
$ticket->waiting_since = current_time('mysql'); |
| 79 |
} |
| 80 |
|
| 81 |
$ticket->last_customer_response = current_time('mysql'); |
| 82 |
} |
| 83 |
|
| 84 |
$ticket->response_count += 1; |
| 85 |
|
| 86 |
$closed = false; |
| 87 |
if (Arr::get($data, 'close_ticket') == 'yes' && $ticket->status != 'closed') { |
| 88 |
$ticket->status = 'closed'; |
| 89 |
$ticket->resolved_at = current_time('mysql'); |
| 90 |
$ticket->closed_by = $person->id; |
| 91 |
$ticket->total_close_time = current_time('timestamp') - strtotime($ticket->created_at); |
| 92 |
$closed = true; |
| 93 |
Conversation::create([ |
| 94 |
'ticket_id' => $ticket->id, |
| 95 |
'person_id' => $person->id, |
| 96 |
'conversation_type' => 'internal_info', |
| 97 |
'content' => __('Ticket has been closed', 'fluent-support') |
| 98 |
]); |
| 99 |
} |
| 100 |
|
| 101 |
$ticket->save(); |
| 102 |
|
| 103 |
if ($agentAdded) { |
| 104 |
$ticket->load('agent'); |
| 105 |
do_action('fluent_support/agent_assigned_to_ticket', $person, $ticket); |
| 106 |
$updateData['agent'] = $ticket->agent; |
| 107 |
} |
| 108 |
|
| 109 |
do_action('fluent_support/' . $convoType . '_added_by_' . $person->person_type, $createdResponse, $ticket, $person); |
| 110 |
|
| 111 |
|
| 112 |
if ($closed) { |
| 113 |
do_action('fluent_support/ticket_closed', $ticket, $person); |
| 114 |
do_action('fluent_support/ticket_closed_by_' . $person->person_type, $ticket, $person); |
| 115 |
} |
| 116 |
|
| 117 |
return [ |
| 118 |
'response' => $createdResponse, |
| 119 |
'ticket' => $ticket, |
| 120 |
'update_data' => $updateData |
| 121 |
]; |
| 122 |
} |
| 123 |
} |
| 124 |
|