| Server IP : 54.94.228.101 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/app/Console/Commands/ |
Upload File : |
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
use App\Modules\AlertaFila\Models\AlertaFila;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Helper\ProgressBar;
use Notification;
use App\Notifications\NotificacaoPedidoEntrega;
use App\Notifications\NotificacaoAcessoAutenticado;
use App\Modules\PedidoEntregaHasStatus\Models\PedidoEntregaHasStatus;
use App\Modules\CanalAcessoAutenticado\Models\CanalAcessoAutenticado;
use App\Services\PedidoEntregaService;
use DB;
class EnvioAlertaFila extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'envio_alerta_fila {timeout?} {limit?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Envio E-mail de Alertas da Fila';
protected $options;
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
date_default_timezone_set('America/Sao_Paulo');
$this->options = $this->arguments();
$timeout = !empty($this->options['timeout']) ? $this->options['timeout'] : 5;
$limit = !empty($this->options['limit']) ? $this->options['limit'] : 25;
$listaAlertas = AlertaFila::where('alerta_fila.email_enviado', 'N')->where('timeout', $timeout)->orderBy('alerta_fila.created_at', 'ASC')->limit($limit)->get();
$this->info(date('d/m/Y H:i:s') . " - ENVIO INICIADO");
if(count($listaAlertas) > 0){
$IDsAlertas = array_map(function($el){
return (int) $el['id'];
}, $listaAlertas->toArray());
$data_atualizacao = date('Y-m-d H:i:s');
$IDsAlertas = implode(",", $IDsAlertas);
$sql = "UPDATE alerta_fila SET email_enviado = 'Q', data_atualizacao = '{$data_atualizacao}' WHERE id IN ({$IDsAlertas})";
DB::update($sql);
$progressBar = new ProgressBar($this->output, count($listaAlertas));
foreach ($listaAlertas as $alerta) {
$this->sendEmail($alerta);
$progressBar->advance();
}
$progressBar->finish();
$this->info("\n");
}
$this->info(date('d/m/Y H:i:s') . " - ENVIO FINALIZADO");
}
public function sendEmail($alerta){
$data_atualizacao = date('Y-m-d H:i:s');
try {
$user = $alerta->cadastro->user;
if(env('APP_ENV') == 'local'){
$user->email = 'marcosarobed@gmail.com';
}
switch ($alerta->tipo) {
case 'pedido-entrega-has-status':
$pedidoEntregaHasStatus = PedidoEntregaHasStatus::where('id', $alerta->tipo_id)->first();
if(!$pedidoEntregaHasStatus){
$update = DB::update("UPDATE alerta_fila SET data_atualizacao = '{$data_atualizacao}', email_enviado = 'E', observacao = 'Status já foi excluído!' WHERE id = {$alerta->id}");
return false;
}
$pedidoEntrega = $pedidoEntregaHasStatus->pedidoEntrega;
$pedidoEntregaStatus = $pedidoEntregaHasStatus->pedidoEntregaStatus;
Notification::send($user, new NotificacaoPedidoEntrega($pedidoEntrega, $pedidoEntregaStatus, $pedidoEntregaHasStatus, url('admin/pedido-entrega/edit/'.$pedidoEntregaHasStatus->pedidoEntrega->slug)));
$update = DB::update("UPDATE alerta_fila SET data_atualizacao = '{$data_atualizacao}', email_enviado = 'Y' WHERE id = {$alerta->id}");
break;
case 'canal-acesso-autenticado':
$canalAcessoAutenticado = CanalAcessoAutenticado::where('id', $alerta->tipo_id)->first();
if(!$canalAcessoAutenticado){
$update = DB::update("UPDATE alerta_fila SET data_atualizacao = '{$data_atualizacao}', email_enviado = 'E', observacao = 'Acesso já foi excluído!' WHERE id = {$alerta->id}");
return false;
}
$cadastro = $canalAcessoAutenticado->cadastro;
$canal = $canalAcessoAutenticado->canalAcessoAutenticado;
$produto = $canalAcessoAutenticado->produto;
Notification::send($user, new NotificacaoAcessoAutenticado($cadastro, $canal, $produto, 'https://www.vinumday.com.br/' . rota_canal($canal->tipo) . '/' . $canal->slug));
$update = DB::update("UPDATE alerta_fila SET data_atualizacao = '{$data_atualizacao}', email_enviado = 'Y' WHERE id = {$alerta->id}");
break;
default:
return false;
break;
}
} catch (Exception $e) {
$observacao = json_encode($e->getMessage());
$update = DB::update("UPDATE alerta_fila SET data_atualizacao = '{$data_atualizacao}', email_enviado = 'E', observacao = '{$observacao}' WHERE id = {$alerta->id}");
return false;
}
}
}