This commit is contained in:
114
api/app/Services/Mobile/ConectaCidadao/Termos/TermoService.php
Normal file
114
api/app/Services/Mobile/ConectaCidadao/Termos/TermoService.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user