-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #934 from portabilis/portabilis-patch-2024-01-19
Portabilis patch 19/01/2024
- Loading branch information
Showing
37 changed files
with
1,776 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\FileExportRequest; | ||
use App\Jobs\FileExporterJob; | ||
use App\Models\FileExport; | ||
use App\Models\LegacyRegistration; | ||
use App\Models\LegacySchool; | ||
use App\Models\LegacySchoolClass; | ||
use App\Process; | ||
use Illuminate\Http\Request; | ||
|
||
class FileExportController extends Controller | ||
{ | ||
public function index(Request $request) | ||
{ | ||
$this->menu(Process::DOCUMENT_EXPORT); | ||
$this->breadcrumb('Exportações', [ | ||
url('/intranet/educar_configuracoes_index.php') => 'Configurações', | ||
]); | ||
$exports = FileExport::query() | ||
->where('user_id', $request->user()->getKey()) | ||
->orderByDesc('created_at') | ||
->paginate(); | ||
|
||
return view('file_export.index', [ | ||
'exports' => $exports, | ||
]); | ||
} | ||
|
||
public function store(FileExportRequest $request) | ||
{ | ||
$fileExport = FileExport::create([ | ||
'user_id' => $request->user()->getKey(), | ||
'filename' => $this->buildFileName( | ||
schoolId: $request->get('ref_cod_escola'), | ||
schoolClassId: $request->get('ref_cod_turma'), | ||
registrationId: $request->get('matricula') | ||
) | ||
]); | ||
|
||
FileExporterJob::dispatch( | ||
fileExport: $fileExport, | ||
args: [ | ||
'year' => $request->get('ano'), | ||
'school' => $request->get('ref_cod_escola'), | ||
'course' => $request->get('ref_cod_curso'), | ||
'grade' => $request->get('ref_cod_serie'), | ||
'schoolClass' => $request->get('ref_cod_turma'), | ||
'registration' => $request->get('matricula'), | ||
] | ||
); | ||
|
||
return redirect()->route('file.export.index'); | ||
} | ||
|
||
public function create() | ||
{ | ||
$this->menu(Process::DOCUMENT_EXPORT); | ||
$this->breadcrumb('Nova Exportação', [ | ||
url('/intranet/educar_configuracoes_index.php') => 'Configurações', | ||
route('file.export.index') => 'Exportações', | ||
]); | ||
|
||
return view('file_export.create'); | ||
} | ||
|
||
private function removeSpecialCharacters($string): string | ||
{ | ||
return preg_replace('/[^\w\s]/u', '', $string); | ||
} | ||
|
||
private function buildFileName(int $schoolId, int $schoolClassId, int|null $registrationId): string | ||
{ | ||
if ($registrationId) { | ||
$registration = LegacyRegistration::query()->find($registrationId, [ | ||
'cod_matricula', | ||
'ref_cod_aluno' | ||
]); | ||
|
||
return mb_strtoupper($this->removeSpecialCharacters($registration->name)) . " ({$registration->ref_cod_aluno})"; | ||
} | ||
|
||
$school = LegacySchool::query()->with([ | ||
'person:idpes,nome', | ||
'organization:idpes,fantasia' | ||
])->find($schoolId, [ | ||
'cod_escola', | ||
'ref_idpes' | ||
]); | ||
|
||
$schoolClass = LegacySchoolClass::query() | ||
->whereKey($schoolClassId) | ||
->first([ | ||
'nm_turma', | ||
'ano' | ||
]); | ||
|
||
$schoolName = mb_strtoupper($this->removeSpecialCharacters($school->name)); | ||
$schoolClassName = mb_strtoupper($this->removeSpecialCharacters($schoolClass->nm_turma)) . ' (' . $schoolClass->ano . ')'; | ||
|
||
return $schoolName . ' - ' . $schoolClassName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class FileExportRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'ano' => [ | ||
'required', | ||
'date_format:Y', | ||
], | ||
'ref_cod_instituicao' => 'required', | ||
'ref_cod_escola' => 'required', | ||
'ref_cod_curso' => 'required', | ||
'ref_cod_serie' => 'required', | ||
'ref_cod_turma' => 'required', | ||
]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function messages() | ||
{ | ||
return [ | ||
'ano.required' => 'Você precisa informar o ano', | ||
'ano.date_format' => 'Você precisa informar um ano válido.', | ||
'ref_cod_instituicao.required' => 'Você precisa selecionar a instituição.', | ||
'ref_cod_curso.required' => 'Você precisa selecionar um curso.', | ||
'ref_cod_escola.required' => 'Você precisa selecionar uma escola.', | ||
'ref_cod_serie.required' => 'Você precisa selecionar uma série.', | ||
'ref_cod_turma.required' => 'Você precisa selecionar uma turma.', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\FileExport; | ||
use App\Services\FileExportService; | ||
use App\Setting; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\DB; | ||
use Throwable; | ||
|
||
class FileExporterJob implements ShouldQueue | ||
{ | ||
use Dispatchable; | ||
use InteractsWithQueue; | ||
use Queueable; | ||
use SerializesModels; | ||
|
||
public $tries = 5; | ||
|
||
public $timeout = 3600; | ||
|
||
public $retryAfter = 5; | ||
|
||
public function __construct(private FileExport $fileExport, private array $args) | ||
{ | ||
} | ||
|
||
public function handle() | ||
{ | ||
$this->setConnection(); | ||
$this->setConfigs(); | ||
(new FileExportService($this->fileExport, $this->args))->execute(); | ||
} | ||
|
||
public function failed(Throwable $exception) | ||
{ | ||
$this->setConnection(); | ||
(new FileExportService($this->fileExport, $this->args))->failed(); | ||
} | ||
|
||
private function setConnection(): void | ||
{ | ||
DB::setDefaultConnection($this->fileExport->getConnectionName()); | ||
} | ||
|
||
private function setConfigs(): void | ||
{ | ||
Config::set(Setting::all()->pluck('value', 'key')->toArray()); | ||
} | ||
|
||
public function tags(): array | ||
{ | ||
return [ | ||
$this->fileExport->getConnectionName(), | ||
'file-export', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Models\Enums; | ||
enum FileExportStatus: int | ||
{ | ||
case WAITING = 1; | ||
case ERROR = 2; | ||
case SUCCESS = 3; | ||
|
||
public function name(): string | ||
{ | ||
return match ($this) { | ||
self::WAITING => 'Aguardando a exportação do arquivo ser finalizada', | ||
self::ERROR => 'O arquivo não pode ser exportado', | ||
self::SUCCESS => 'Fazer download', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Models\Enums\FileExportStatus; | ||
use App\Services\UrlPresigner; | ||
use Illuminate\Database\Eloquent\Casts\Attribute; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class FileExport extends Model | ||
{ | ||
protected $fillable = [ | ||
'user_id', | ||
'url', | ||
'hash', | ||
'filename', | ||
'status_id', | ||
'size' | ||
]; | ||
|
||
protected static function boot() | ||
{ | ||
parent::boot(); | ||
static::creating(function (self $studentFileExport) { | ||
$studentFileExport->hash = md5(time()); | ||
$studentFileExport->status_id = FileExportStatus::WAITING; | ||
}); | ||
} | ||
|
||
protected function presignedUrl(): Attribute | ||
{ | ||
return Attribute::make( | ||
get: fn () => (new UrlPresigner)->getPresignedUrl($this->url) | ||
); | ||
} | ||
|
||
protected function sizeFormat(): Attribute | ||
{ | ||
return Attribute::make( | ||
get: function () { | ||
$units = [ | ||
'B', | ||
'KB', | ||
'MB', | ||
'GB', | ||
'TB' | ||
]; | ||
$bytes = max($this->size, 0); | ||
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); | ||
$pow = min($pow, count($units) - 1); | ||
|
||
return number_format($bytes / (1024 ** $pow), 2) . ' ' . $units[$pow]; | ||
} | ||
); | ||
} | ||
|
||
public function statusIsError(): bool | ||
{ | ||
return $this->status_id === FileExportStatus::ERROR->value || ($this->statusIsWaiting() && $this->created_at < now()->subMinutes(5)); | ||
} | ||
|
||
public function statusIsWaiting(): bool | ||
{ | ||
return $this->status_id === FileExportStatus::WAITING->value; | ||
} | ||
|
||
public function statusIsSuccess(): bool | ||
{ | ||
return $this->status_id === FileExportStatus::SUCCESS->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
class LegacyActiveLooking extends LegacyModel | ||
{ | ||
use SoftDeletes; | ||
use HasFiles; | ||
|
||
/** | ||
* @var string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.