Skip to content

Commit

Permalink
Merge pull request #934 from portabilis/portabilis-patch-2024-01-19
Browse files Browse the repository at this point in the history
Portabilis patch 19/01/2024
  • Loading branch information
edersoares authored Jan 21, 2024
2 parents da3f2a9 + cc23873 commit 620a065
Show file tree
Hide file tree
Showing 37 changed files with 1,776 additions and 581 deletions.
105 changes: 105 additions & 0 deletions app/Http/Controllers/FileExportController.php
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;
}
}
44 changes: 44 additions & 0 deletions app/Http/Requests/FileExportRequest.php
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.',
];
}
}
64 changes: 64 additions & 0 deletions app/Jobs/FileExporterJob.php
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',
];
}
}
18 changes: 18 additions & 0 deletions app/Models/Enums/FileExportStatus.php
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',
};
}
}
1 change: 1 addition & 0 deletions app/Models/Exporter/Enrollment.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getExportedColumnsByGroup()
'school_class_id' => 'ID Turma',
'grade_id' => 'ID Série',
'course_id' => 'ID Curso',
'inep_id' => 'Código INEP (Aluno)',
],
'Aluno' => [
'name' => 'Nome',
Expand Down
71 changes: 71 additions & 0 deletions app/Models/FileExport.php
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;
}
}
1 change: 1 addition & 0 deletions app/Models/LegacyActiveLooking.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class LegacyActiveLooking extends LegacyModel
{
use SoftDeletes;
use HasFiles;

/**
* @var string
Expand Down
5 changes: 5 additions & 0 deletions app/Models/LegacyStudent.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ protected function realName(): Attribute
);
}

public function picture(): HasOne
{
return $this->hasOne(LegacyIndividualPicture::class, 'idpes', 'ref_idpes');
}

public function person(): BelongsTo
{
return $this->belongsTo(LegacyPerson::class, 'ref_idpes');
Expand Down
2 changes: 2 additions & 0 deletions app/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Process

public const BATCH_EXEMPTION = 1025;

public const DOCUMENT_EXPORT = 1026;

public const UNDO_STUDENT_UNIFICATION = 2001;

public const EXEMPTION_LIST = 2002;
Expand Down
1 change: 1 addition & 0 deletions app/Services/Exemption/ActiveLookingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function store(LegacyActiveLooking $activeLooking, LegacyRegistration $re
)->validate();

$activeLooking->save();
return $activeLooking;
}

public function delete(LegacyActiveLooking $activeLooking)
Expand Down
Loading

0 comments on commit 620a065

Please sign in to comment.