Files
rkm-conectasus-ci-sandbox/api/app/Services/Ambulatorio/SmsService.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

145 lines
4.3 KiB
PHP

<?php
namespace App\Services\Ambulatorio;
use App\Enums\Ambulatorio\AgendamentoTipo;
use App\Models\Ambulatorio\Agendamento;
use App\Models\Ambulatorio\ParametrizacaoInfraestrutura;
use App\Models\Geral\DiaSemana;
use App\Services\Service;
use App\Services\Sms\SmsService as BaseSmsService;
use App\Utils\MaskUtil;
use Carbon\Carbon;
use Illuminate\Support\Str;
use RuntimeException;
class SmsService extends Service
{
private $baseSmsService;
private $agendamento;
private $parametrizacao;
public function __construct($agendamentoId)
{
$this->baseSmsService = new BaseSmsService();
$this->agendamento = Agendamento::findOrFail($agendamentoId);
$this->parametrizacao = ParametrizacaoInfraestrutura::where(
'infraestrutura_id',
$this->agendamento->jornada->lotacao->infraestrutura_id
)->first();
}
public function send()
{
if ($this->agendamento->jornada->data->isToday()) {
return;
}
$cellPhone = $this->getCellPhone();
if (is_null($cellPhone)) {
return;
}
if (!$this->parametrizacao->envia_sms || ($this->parametrizacao->envia_sms && is_null($this->parametrizacao->sms))) {
return;
}
$schedule = $this->getSchedule();
if (!$this->isAllowedToSend($schedule)) {
return;
}
try {
$smsEnvio = $this->baseSmsService->send($cellPhone, $this->getMessage(), $schedule);
$this->agendamento->sms_envio_id = $smsEnvio->id;
$this->agendamento->save();
}
catch (RuntimeException $e) {}
}
private function isAllowedToSend($schedule)
{
if (is_null($schedule)) {
$schedule = Carbon::now();
}
$envioDe = $this->parametrizacao->sms->envio_de->startOfDay();
$envioAte = $this->parametrizacao->sms->envio_ate->endOfDay();
if ($schedule >= $envioDe && $schedule <= $envioAte) {
return $this->parametrizacao->sms->especialidades->pluck('id')
->contains($this->agendamento->jornada->especialidade_id);
}
return false;
}
private function getCellPhone()
{
return $this->agendamento->paciente->cidadao->telefones()
->where('autoriza_sms', true)
->first()->numero ?? null;
}
private function getMessage()
{
$nomePaciente = $this->agendamento->paciente->cidadao->nome;
$agendamentoTipo = Str::lower($this->agendamento->tipo->nome);
$especialidade = $this->agendamento->jornada->especialidade->nome;
$data = $this->agendamento->jornada->data->format('d/m/Y');
$diaSemana = DiaSemana::findOrFail($this->agendamento->jornada->dia_semana)->descricao;
$horario = $this->agendamento->horario->format('H:i');
$unidade = $this->agendamento->jornada->lotacao->infraestrutura->unidade->nome_fantasia;
$infraestrutura = $this->agendamento->jornada->lotacao->infraestrutura->nome;
$usuario = $this->agendamento->jornada->lotacao->usuario->cidadao->nome;
$telefone = MaskUtil::getPhoneMasked(
$this->agendamento->jornada->lotacao->infraestrutura->telefone
);
$pronomeAgendamentoTipo = $this->agendamento->tipo_id === AgendamentoTipo::CONSULTA ? 'sua' : 'seu';
$unidade .= $infraestrutura === $unidade ? '' : " ({$infraestrutura})";
return "Sr(a) {$nomePaciente}, {$pronomeAgendamentoTipo} {$agendamentoTipo} para a especialidade"
. " {$especialidade} está agendada para {$data} ({$diaSemana}) às {$horario} na unidade {$unidade} com"
. " o profissional {$usuario}. Caso não possa comparecer, entrar em contato no telefone {$telefone}";
}
private function getSchedule()
{
$schedule = $this->agendamento->jornada->data->copy()->subDays($this->parametrizacao->sms->dia_antecedencia);
if ($schedule <= Carbon::today()) {
return null;
}
return $schedule;
}
public function cancel()
{
if (is_null($this->agendamento->sms_envio_id)) {
return;
}
try {
$this->baseSmsService->cancel($this->agendamento->sms_envio_id);
}
catch (RuntimeException $e) {}
}
}