Skip to content

Commit

Permalink
Merge pull request #652 from portabilis/portabilis-patch-2019-09-06
Browse files Browse the repository at this point in the history
[2.2] Portabilis patch 06/09/2019
  • Loading branch information
edersoares authored Sep 9, 2019
2 parents 35b4028 + acf6b7f commit 95a4e9a
Show file tree
Hide file tree
Showing 134 changed files with 2,329 additions and 3,321 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ matrix:

script:
- vendor/bin/phpunit
- vendor/bin/phpunit --testsuite Diario
- php artisan dusk

env:
Expand Down Expand Up @@ -64,6 +65,7 @@ matrix:

script:
- vendor/bin/phpunit
- vendor/bin/phpunit --testsuite Diario
- php artisan dusk

env:
Expand Down
108 changes: 108 additions & 0 deletions app/Console/Commands/UpdateSchoolClassGrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace App\Console\Commands;

use App\Exceptions\Console\MissingSchoolCourseException;
use App\Exceptions\Console\MissingSchoolGradeException;
use App\Models\LegacyEnrollment;
use App\Models\LegacyLevel;
use App\Models\LegacySchoolClass;
use App\Models\LegacySchoolCourse;
use App\Models\LegacySchoolGrade;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdateSchoolClassGrade extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update:school-class-grade {schoolclass} {grade}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Atualiza a série da turma';

/**
* @var LegacyLevel
*/
private $grade;

/**
* @var LegacySchoolClass
*/
private $schoolClass;

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->grade = LegacyLevel::findOrFail($this->argument('grade'));
$this->schoolClass = LegacySchoolClass::findOrFail($this->argument('schoolclass'));

$this->validateSchoolGrade();
$this->validateSchoolCourse();

DB::beginTransaction();

$this->schoolClass->update([
'ref_ref_cod_serie' => $this->grade->getKey(),
'ref_cod_curso' => $this->grade->course->id,
]);

$this->schoolClass->enrollments->map(function ($enrollment) {
/** @var LegacyEnrollment $enrollment */
$enrollment->registration->update([
'ref_ref_cod_serie' => $this->grade->getKey(),
'ref_cod_curso' => $this->grade->course->id,
]);
});

DB::commit();
}

/**
* Valida se existe registro em escola_serie
*
* @throws MissingSchoolGradeException
*/
private function validateSchoolGrade()
{
$existsSchoolGrade = LegacySchoolGrade::where('ref_cod_escola', $this->schoolClass->school_id)
->where('ref_cod_serie', $this->grade->getKey())
->exists();

if ($existsSchoolGrade) {
return;
}

throw new MissingSchoolGradeException($this->schoolClass->school, $this->grade);
}

/**
* Valida se existe registro em escola_curso
*
* @throws MissingSchoolCourseException
*/
private function validateSchoolCourse()
{
$existsSchoolCourse = LegacySchoolCourse::where('ref_cod_escola', $this->schoolClass->school_id)
->where('ref_cod_curso', $this->grade->course->id)
->exists();

if ($existsSchoolCourse) {
return;
}

throw new MissingSchoolCourseException($this->schoolClass->school, $this->grade->course);
}

}
21 changes: 21 additions & 0 deletions app/Exceptions/Console/MissingSchoolCourseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Exceptions\Console;

use App\Models\LegacyCourse;
use App\Models\LegacySchool;
use DomainException;

class MissingSchoolCourseException extends DomainException
{
public function __construct(LegacySchool $school, LegacyCourse $course)
{
$message = 'Não existe registro em escola curso para escola %s e curso %s';

$message = sprintf(
$message, $school->getKey(), $course->getKey()
);

parent::__construct($message);
}
}
21 changes: 21 additions & 0 deletions app/Exceptions/Console/MissingSchoolGradeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Exceptions\Console;

use App\Models\LegacyLevel;
use App\Models\LegacySchool;
use DomainException;

class MissingSchoolGradeException extends DomainException
{
public function __construct(LegacySchool $school, LegacyLevel $grade)
{
$message = 'Não existe registro em escola série para escola %s e série %s';

$message = sprintf(
$message, $school->getKey(), $grade->getKey()
);

parent::__construct($message);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/LegacyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function loadLegacyFile($filename)
private function loadFileOrAbort($filename)
{
try {
require_once $filename;
require $filename;
return;
} catch (HttpResponseException $exception) {

Expand Down
10 changes: 10 additions & 0 deletions app/Listeners/LoginLegacySession.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use App\Models\LegacySchoolClass;
use App\Models\LegacyStudent;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Throwable;

class LoginLegacySession
{
Expand Down Expand Up @@ -48,10 +50,18 @@ private function getLoggedUserInfo($user)
{
$institution = app(LegacyInstitution::class);

try {
$createdAt = Carbon::create($user->created_at)->getTimestamp();
} catch (Throwable $throwable) {
$createdAt = Carbon::now()->subYear()->getTimestamp();
}

return (object) [
'personId' => $user->id,
'name' => $user->name,
'email' => $user->email,
'role' => $user->role,
'created_at' => $createdAt,
'institution' => $institution->name,
'city' => $institution->city,
'state' => $institution->state,
Expand Down
51 changes: 47 additions & 4 deletions app/Models/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,77 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* City
*
* @property int $id
* @property string $name
* @property string $formatted_name
* @property string $state_id
* @property int $ibge_code
* @property int $parent_id
* @property int $created_by
* @property int $updated_by
* @property int $created_at
* @property int $updated_at
* @property int $registry_origin
*/
class City extends Model
{
use SoftDeletes;
/**
* @var array
*/
protected $casts = [
'ibge_code' => 'integer',
'created_by' => 'integer',
'updated_by' => 'integer',
];

protected $dates = ['deleted_at'];
/**
* @return string
*/
public function getFormattedNameAttribute()
{
return "{$this->name}/{$this->state_id}";
}

/**
* @return BelongsTo
*/
public function state()
{
return $this->belongsTo(State::class);
}

/**
* @return BelongsTo
*/
public function parent()
{
return $this->belongsTo(self::class, 'parent_id', 'id');
return $this->belongsTo(City::class, 'parent_id', 'id');
}

/**
* @return BelongsTo
*/
public function updatedBy()
{
return $this->belongsTo(Individual::class, 'updated_by', 'id');
}

/**
* @return BelongsTo
*/
public function createdBy()
{
return $this->belongsTo(Individual::class, 'created_by', 'id');
}

/**
* @return string
*/
public function getRegistryOriginDescriptionAttribute()
{
return (new RegistryOrigin)->getDescriptiveValues()[(int) $this->registry_origin];
Expand Down
Loading

0 comments on commit 95a4e9a

Please sign in to comment.