42 lines
3.3 KiB
PHP
42 lines
3.3 KiB
PHP
<?php
|
|
namespace App\Shared\Geography;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class Catalogue
|
|
{
|
|
public static function guard():void
|
|
{
|
|
abort_unless(config('geography.enabled')===true,404);
|
|
abort_unless(app()->environment('budget-testing'),403);
|
|
if(request()->is('api/geography/*'))abort_unless(request()->hasSession()&&hash_equals((string)request()->session()->get('company_context.token',''),(string)request()->header('X-SGF-Context')),409,'O contexto mudou. Reabra o formulário.');
|
|
\App\Modules\Purchasing\BudgetAdapter::guard();
|
|
$c=DB::connection();$p=$c->getConfig();$run=(string)getenv('SGF_ISOLATED_REGRESSION_RUN');
|
|
abort_unless(preg_match('/^[a-f0-9]{12}$/D',$run)&&preg_match('/^budget_core_sgf_([a-f0-9]{12})$/D',$c->getDatabaseName(),$m)&&$p['username']==='ir_composition_'.$m[1]&&$p['host']==='127.0.0.1'&&(int)$p['port']===55439&&empty($p['url'])&&empty($p['read'])&&empty($p['write'])&&($p['search_path']??'public')==='public',403);
|
|
$i=$c->selectOne("SELECT current_user role,current_setting('cluster_name') cluster,rolsuper OR rolcreatedb OR rolcreaterole OR rolreplication OR rolbypassrls elevated FROM pg_roles WHERE rolname=current_user");
|
|
abort_unless(!$i->elevated&&$i->role===$p['username']&&$i->cluster==='sgf_modular_regression_'.$run,403);
|
|
}
|
|
public static function active(bool $lock=false):object
|
|
{
|
|
$q=DB::table('geo_catalogue_state')->where('id',1);$row=($lock?$q->lockForUpdate():$q)->first();
|
|
abort_unless($row&&hash_equals((string)config('geography.source_sha256'),$row->source_version),409,'A versão do catálogo mudou. Reabra a conferência.');return $row;
|
|
}
|
|
public static function describe(object $row):array
|
|
{
|
|
return ['key'=>$row->territory_key,'source_id'=>$row->source_id,'level'=>(int)$row->level,'designation'=>$row->designation,'codes'=>array_combine(Cartography::FIELDS,array_map(fn($f)=>$row->$f,Cartography::FIELDS)),'issues'=>json_decode($row->issues,true),'selectable'=>(bool)$row->selectable];
|
|
}
|
|
public static function selection(string $version,string $key):array
|
|
{
|
|
$active=self::active(true);abort_unless(hash_equals($active->source_version,$version),409,'O catálogo mudou.');
|
|
$row=DB::table('geo_territories')->where('source_version',$version)->where('territory_key',$key)->first();abort_unless($row,422,'Território desconhecido nesta versão.');abort_unless($row->selectable,422,'Este território tem pendências de conferência na fonte.');return ['country_code'=>'MZ','version'=>$version]+self::describe($row);
|
|
}
|
|
public static function page(array $d):array
|
|
{
|
|
$version=self::active()->source_version;$q=DB::table('geo_territories')->where('source_version',$version);
|
|
if(isset($d['parent'])){$parent=DB::table('geo_territories')->where('source_version',$version)->where('territory_key',$d['parent'])->first();abort_unless($parent&&$parent->selectable,422,'Ascendente por conferir.');$q->where('parent_key',$parent->territory_key);}
|
|
elseif(empty($d['search']))$q->where('level',1);
|
|
if(!empty($d['search']))$q->where('designation','ilike','%'.$d['search'].'%');
|
|
return ['version'=>$version,'records'=>$q->orderBy('designation')->orderBy('source_id')->paginate(25,['*'],'page',(int)($d['page']??1))->through(fn($r)=>self::describe($r))];
|
|
}
|
|
}
|