115 lines
3.2 KiB
PHP
115 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Mobile\ConectaCidadao\Termos;
|
|
|
|
use App\Enums\Mobile\ConectaCidadao\TermoTipo;
|
|
use App\Models\Mobile\ConectaCidadao\ConectaCidadaoAceitacaoCidadao;
|
|
use App\Models\Mobile\ConectaCidadao\ConectaCidadaoTermo;
|
|
use App\Models\Mobile\ConectaCidadao\ConectaCidadaoTermoTipo;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Response;
|
|
|
|
class TermoService extends Service
|
|
{
|
|
|
|
public static function getAtivoByTipo(string $tipo): ?ConectaCidadaoTermo
|
|
{
|
|
$tipoId = ConectaCidadaoTermoTipo::where('codigo', $tipo)->value('id');
|
|
|
|
return ConectaCidadaoTermo::where('tipo_id', $tipoId)
|
|
->where('ativo', true)
|
|
->latest('data_publicacao')
|
|
->first();
|
|
}
|
|
|
|
public static function getAtivo(): ConectaCidadaoTermo
|
|
{
|
|
$tipo = request()->validate(
|
|
[
|
|
'tipo' => 'required|string|in:' . TermoTipo::TERMO_USO,
|
|
]
|
|
)['tipo'];
|
|
|
|
$tipoId = ConectaCidadaoTermoTipo::where('codigo', $tipo)->value('id');
|
|
|
|
$termo = ConectaCidadaoTermo::where('tipo_id', $tipoId)
|
|
->where('ativo', true)
|
|
->latest('data_publicacao')
|
|
->first();
|
|
|
|
if (!$termo) {
|
|
|
|
abort(Response::HTTP_NOT_FOUND, "Nenhum termo ativo encontrado para o tipo '{$tipo}'.");
|
|
}
|
|
|
|
return $termo;
|
|
}
|
|
|
|
public static function getById(int $id): ConectaCidadaoTermo
|
|
{
|
|
return ConectaCidadaoTermo::findOrFail($id);
|
|
}
|
|
|
|
public static function getPendentes(): array
|
|
{
|
|
$cidadaoId = self::getCidadaoId();
|
|
|
|
return ConectaCidadaoTermo::where('ativo', true)
|
|
->where('obrigatorio', true)
|
|
->whereNotIn('id', function ($query) use ($cidadaoId) {
|
|
$query->select('termo_id')
|
|
->from('conecta_cidadao_aceitacao_cidadao')
|
|
->where('cidadao_id', $cidadaoId);
|
|
})
|
|
->get(
|
|
[
|
|
'id',
|
|
'tipo_id',
|
|
'versao',
|
|
'data_publicacao',
|
|
'obrigatorio'
|
|
]
|
|
)
|
|
->toArray();
|
|
}
|
|
|
|
public static function aceitar(int $id): ConectaCidadaoAceitacaoCidadao
|
|
{
|
|
ConectaCidadaoTermo::findOrFail($id);
|
|
|
|
$cidadaoId = self::getCidadaoId();
|
|
|
|
$jaAceito = ConectaCidadaoAceitacaoCidadao::where('cidadao_id', $cidadaoId)
|
|
->where('termo_id', $id)
|
|
->exists();
|
|
|
|
if ($jaAceito) {
|
|
|
|
abort(Response::HTTP_UNPROCESSABLE_ENTITY, 'Este termo já foi aceito anteriormente.');
|
|
}
|
|
|
|
return ConectaCidadaoAceitacaoCidadao::create([
|
|
'cidadao_id' => $cidadaoId,
|
|
'termo_id' => $id,
|
|
'aceitado_em' => Carbon::now(),
|
|
]);
|
|
}
|
|
|
|
public static function getAceitacoes(): array
|
|
{
|
|
$cidadaoId = self::getCidadaoId();
|
|
|
|
return ConectaCidadaoAceitacaoCidadao::where('cidadao_id', $cidadaoId)
|
|
->with('termo:id,tipo_id,versao,data_publicacao')
|
|
->orderByDesc('aceitado_em')
|
|
->get()
|
|
->toArray();
|
|
}
|
|
|
|
private static function getCidadaoId(): int
|
|
{
|
|
return auth('mobile')->user()->cidadao_id;
|
|
}
|
|
}
|