| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\CLI; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Person; |
| 6 |
use FluentSupport\App\Models\Conversation; |
| 7 |
use FluentSupport\App\Models\Ticket; |
| 8 |
use FluentSupport\App\Modules\StatModule; |
| 9 |
|
| 10 |
class FluentCli |
| 11 |
{ |
| 12 |
public function stats($args, $assoc_args) |
| 13 |
{ |
| 14 |
$overallStats = StatModule::getOverAllStats(); |
| 15 |
$format = \WP_CLI\Utils\get_flag_value($assoc_args, 'format', 'table'); |
| 16 |
|
| 17 |
\WP_CLI\Utils\format_items( |
| 18 |
$format, |
| 19 |
$overallStats, |
| 20 |
['title', 'count'] |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
public function fix_timestamps($args, $assoc_args) |
| 25 |
{ |
| 26 |
$targetTickets = Ticket::whereIn('status', ['active', 'new']) |
| 27 |
->get(); |
| 28 |
\WP_CLI::confirm(count($targetTickets) . " tickets will be affected. Continue?"); |
| 29 |
|
| 30 |
$counter = 0; |
| 31 |
|
| 32 |
foreach ($targetTickets as $ticket) { |
| 33 |
// get last customer response |
| 34 |
$lastCustomerResponse = Conversation::where('ticket_id', $ticket->id) |
| 35 |
->where('conversation_type', 'response') |
| 36 |
->where('person_id', $ticket->customer_id) |
| 37 |
->orderBy('id', 'DESC') |
| 38 |
->first(); |
| 39 |
$isDirty = false; |
| 40 |
if($lastCustomerResponse && $ticket->last_customer_response != $lastCustomerResponse->created_at) { |
| 41 |
$ticket->last_customer_response = $lastCustomerResponse->created_at; |
| 42 |
$isDirty = true; |
| 43 |
} |
| 44 |
|
| 45 |
if($ticket->status != 'new') { |
| 46 |
$lastAgentResponse = Conversation::where('ticket_id', $ticket->id) |
| 47 |
->where('conversation_type', 'response') |
| 48 |
->where('person_id', '!=', $ticket->customer_id) |
| 49 |
->orderBy('id', 'DESC') |
| 50 |
->first(); |
| 51 |
if($lastAgentResponse && $ticket->last_agent_response != $lastAgentResponse->created_at) { |
| 52 |
$ticket->last_agent_response = $lastAgentResponse->created_at; |
| 53 |
$isDirty = true; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if($isDirty) { |
| 58 |
$ticket->save(); |
| 59 |
\WP_CLI::line( 'Ticket Done at '. $ticket->id); |
| 60 |
$counter++; |
| 61 |
} else { |
| 62 |
\WP_CLI::line( 'Ticket Skipped at '. $ticket->id); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
\WP_CLI::line( 'All Done '. $counter); |
| 67 |
|
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
public function fix_resolve_null() |
| 72 |
{ |
| 73 |
$targetTickets = Ticket::where('resolved_at', '0000-00-00 00:00:00') |
| 74 |
->where('status', 'closed') |
| 75 |
->get(); |
| 76 |
|
| 77 |
if ($targetTickets->isEmpty()) { |
| 78 |
\WP_CLI::error('No tickets found!', true); |
| 79 |
} |
| 80 |
|
| 81 |
\WP_CLI::confirm(count($targetTickets) . " tickets will be affected."); |
| 82 |
|
| 83 |
|
| 84 |
$total = count($targetTickets); |
| 85 |
$progress = \WP_CLI\Utils\make_progress_bar('Replacing: ', $total , 1); |
| 86 |
|
| 87 |
foreach ($targetTickets as $index => $ticket) { |
| 88 |
$ticket->resolved_at = $ticket->updated_at; |
| 89 |
$ticket->save(); |
| 90 |
$completed = $index + 1; |
| 91 |
$progress->tick(1, "$completed / $total : Completed - ".$completed); |
| 92 |
} |
| 93 |
|
| 94 |
$progress->finish(); |
| 95 |
\WP_CLI::line( 'All Done! Cheers!!'); |
| 96 |
} |
| 97 |
|
| 98 |
public function replace_null_column_value($args, $assoc_args) |
| 99 |
{ |
| 100 |
$column = \WP_CLI\Utils\get_flag_value($assoc_args, 'column', ''); |
| 101 |
$replaceColumn = \WP_CLI\Utils\get_flag_value($assoc_args, 'replace_column', ''); |
| 102 |
|
| 103 |
if (empty($column) || empty($replaceColumn)) { |
| 104 |
\WP_CLI::error('--column and --replace_column parameter is required', true); |
| 105 |
} |
| 106 |
|
| 107 |
$targetTickets = Ticket::whereNull($column)->get(); |
| 108 |
|
| 109 |
if ($targetTickets->isEmpty()) { |
| 110 |
\WP_CLI::error('No tickets found!', true); |
| 111 |
} |
| 112 |
|
| 113 |
\WP_CLI::confirm(count($targetTickets) . " tickets will be affected. $column value will be replaced with $replaceColumn value of each row. Please confirm", $assoc_args); |
| 114 |
|
| 115 |
|
| 116 |
$firstTicket = $targetTickets[0]; |
| 117 |
|
| 118 |
$fillables = $firstTicket->getFillable(); |
| 119 |
|
| 120 |
if (!in_array($column, $fillables) || !in_array($column, $fillables)) { |
| 121 |
\WP_CLI::error('Properties could not be found', true); |
| 122 |
} |
| 123 |
|
| 124 |
$total = count($targetTickets); |
| 125 |
$progress = \WP_CLI\Utils\make_progress_bar('Replacing: ', $total , 1); |
| 126 |
|
| 127 |
foreach ($targetTickets as $index => $ticket) { |
| 128 |
$ticket->{$column} = $ticket->{$replaceColumn}; |
| 129 |
$ticket->save(); |
| 130 |
$completed = $index + 1; |
| 131 |
$progress->tick(1, "$completed / $total : Completed - ".$completed); |
| 132 |
} |
| 133 |
|
| 134 |
$progress->finish(); |
| 135 |
\WP_CLI::line( 'All Done! Cheers!!'); |
| 136 |
} |
| 137 |
|
| 138 |
public function match_old() |
| 139 |
{ |
| 140 |
$hasMore = true; |
| 141 |
$itteration = 0; |
| 142 |
|
| 143 |
while ($hasMore) { |
| 144 |
\WP_CLI::line( 'Itteration: '.$itteration); |
| 145 |
$oldTickets = \FluentSupport\App\App::db()->table('fs_tickets_old') |
| 146 |
->select(['id', 'customer_id']) |
| 147 |
->offset($itteration * 100) |
| 148 |
->limit(100) |
| 149 |
->get(); |
| 150 |
|
| 151 |
$itteration++; |
| 152 |
|
| 153 |
if(count($oldTickets) == 0) { |
| 154 |
$hasMore = false; |
| 155 |
} else { |
| 156 |
foreach ($oldTickets as $oldTicket) { |
| 157 |
\FluentSupport\App\App::db()->table('fs_tickets') |
| 158 |
->where('id', $oldTicket->id) |
| 159 |
->update([ |
| 160 |
'customer_id' => $oldTicket->customer_id |
| 161 |
]); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
\WP_CLI::line( 'All Done'); |
| 167 |
} |
| 168 |
|
| 169 |
public function adre_match_smtp() |
| 170 |
{ |
| 171 |
$nullTickets = Ticket::whereNull('customer_id') |
| 172 |
->get(); |
| 173 |
|
| 174 |
foreach ($nullTickets as $nullTicket) |
| 175 |
{ |
| 176 |
$emailLog = \FluentSupport\App\App::db()->table('fsmpt_email_logs') |
| 177 |
->where('subject', 'Request Received: '.$nullTicket->title) |
| 178 |
->first(); |
| 179 |
|
| 180 |
if($emailLog) { |
| 181 |
$to = maybe_unserialize($emailLog->to); |
| 182 |
$email = $to[0]['email']; |
| 183 |
$person = Person::where('email', $email)->first(); |
| 184 |
if($person) { |
| 185 |
$nullTicket->customer_id = $person->id; |
| 186 |
$nullTicket->save(); |
| 187 |
} else { |
| 188 |
\WP_CLI::error('Could not be found '.$nullTicket->id, true); |
| 189 |
} |
| 190 |
} else { |
| 191 |
\WP_CLI::line( $nullTicket->title); |
| 192 |
\WP_CLI::line('Could not be found', false); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
public function fix_waiting_since() |
| 198 |
{ |
| 199 |
$tickets = Ticket::where('status', '!=', 'closed') |
| 200 |
->whereNull('waiting_since') |
| 201 |
->get(); |
| 202 |
foreach ($tickets as $ticket) { |
| 203 |
$responses = Conversation::where('ticket_id', $ticket->id) |
| 204 |
->orderBy('id', 'ASC') |
| 205 |
->get(); |
| 206 |
$lastResponseFrom = 'customer'; |
| 207 |
$waitingSince = $ticket->created_at; |
| 208 |
|
| 209 |
foreach ($responses as $response) { |
| 210 |
if($response->person_id == $ticket->customer_id && $lastResponseFrom == 'customer') { |
| 211 |
// this is customer response and repeat response |
| 212 |
} else { |
| 213 |
// this is agent response |
| 214 |
$waitingSince = $response->created_at; |
| 215 |
$lastResponseFrom = ($response->person_id == $ticket->customer_id) ? 'customer' : 'agent'; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
$ticket->waiting_since = $waitingSince; |
| 220 |
$ticket->save(); |
| 221 |
\WP_CLI::line($ticket->waiting_since .' : '. $ticket->id.' : '. $ticket->title); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|