Skip to content

Commit

Permalink
Merge pull request #57 from bildvitta/develop
Browse files Browse the repository at this point in the history
Merge Develop into Master
  • Loading branch information
zerossB authored Oct 10, 2024
2 parents 2f8949c + b12be5f commit dea0a5e
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/Console/Commands/DataImport/Hub/HubImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class HubImportCommand extends Command
*
* @var string
*/
protected $signature = 'dataimport:hub {--select=500} {--offset=0} {--tables=companies,users,positions,permissions,roles,user_companies,user_company_parent_positions,user_company_real_estate_developments}';
protected $signature = 'dataimport:hub {--select=500} {--offset=0} {--tables=brands,companies,users,positions,permissions,roles,user_companies,user_company_parent_positions,user_company_real_estate_developments}';

/**
* The console command description.
Expand Down Expand Up @@ -49,7 +49,7 @@ public function handle()
$tableIndex = 0;
$tables = explode(',', $this->option('tables'));

$worker = new Worker();
$worker = new Worker;
$worker->type = self::WORKER_TYPE;
$worker->status = Worker::STATUS_CREATED;
$worker->schedule = now();
Expand Down
24 changes: 24 additions & 0 deletions src/Console/Commands/DataImport/Hub/Jobs/HubImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace BildVitta\SpHub\Console\Commands\DataImport\Hub\Jobs;

use BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources\BrandImport;
use BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources\CompanyImport;
use BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources\ConfigConnection;
use BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources\DbHubBrand;
use BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources\DbHubCompany;
use BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources\DbHubPermission;
use BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources\DbHubPositions;
Expand Down Expand Up @@ -85,6 +87,9 @@ public function handle()
$this->init();

switch ($this->currentTable) {
case 'brands':
$this->importBrands();
break;
case 'companies':
$this->importCompanies();
break;
Expand Down Expand Up @@ -121,6 +126,25 @@ private function init(): void
$this->currentTable = $this->worker->payload->tables[$this->worker->payload->table_index];
}

private function importBrands(): void
{
$dbHubBrand = app(DbHubBrand::class);
$brandImport = app(BrandImport::class);
$payload = $this->worker->payload;

if (is_null($payload->total)) {
$payload->total = $dbHubBrand->totalRecords();
$this->updateWorker(['payload' => $payload]);
}

$brands = collect($dbHubBrand->getBrands($payload->limit, $payload->offset));
foreach ($brands as $brand) {
$brandImport->import($brand);
}

$this->dispatchNextJob();
}

private function importPermissions(): void
{
$dbHubPermission = app(DbHubPermission::class);
Expand Down
26 changes: 26 additions & 0 deletions src/Console/Commands/DataImport/Hub/Resources/BrandImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources;

class BrandImport
{
public function import(\stdClass $brand): void
{
$brandModelFromConfig = app(config('hub.model_brand'));

$permissionModel = $brandModelFromConfig::where('uuid', $brand->uuid)
->first();

if (! $permissionModel) {
$permissionModel = new $brandModelFromConfig;
}

$permissionModel->uuid = $brand->uuid;
$permissionModel->name = $brand->name;
$permissionModel->created_at = $brand->created_at;
$permissionModel->updated_at = $brand->updated_at;
$permissionModel->deleted_at = $brand->deleted_at;

$permissionModel->save();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources;

use BildVitta\Hub\Entities\HubBrand;
use BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers\UserExtraFields;
use BildVitta\SpHub\Models\HubCompany;
use stdClass;
Expand All @@ -13,14 +14,17 @@ class CompanyImport
public function import(stdClass $company): void
{
if (! $companyModel = HubCompany::withTrashed()->where('uuid', $company->uuid)->first()) {
$companyModel = new HubCompany();
$companyModel = new HubCompany;
$companyModel->uuid = $company->uuid;
}
$companyModel->name = $company->name;
$companyModel->main_company_id = null;
if ($company->main_company_uuid) {
$companyModel->main_company_id = HubCompany::withTrashed()->where('uuid', $company->main_company_uuid)->value('id');
}
if ($company->brand_uuid) {
$companyModel->brand_id = HubBrand::withTrashed()->where('uuid', $company->brand_uuid)->value('id');
}
$companyModel->deleted_at = $company->deleted_at;

$userModel = app(config('hub.model_user'));
Expand Down
26 changes: 26 additions & 0 deletions src/Console/Commands/DataImport/Hub/Resources/DbHubBrand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace BildVitta\SpHub\Console\Commands\DataImport\Hub\Resources;

use Illuminate\Support\Facades\DB;

class DbHubBrand
{
public function totalRecords(): int
{
$query = 'SELECT count(1) as total FROM brands';
$result = DB::connection('sp_hub')->select($query);

return (int) $result[0]->total;
}

public function getBrands(int $limit, int $offset): array
{
$query = 'SELECT b.* FROM brands b LIMIT :limit OFFSET :offset';

return DB::connection('sp_hub')->select($query, [
'limit' => $limit,
'offset' => $offset,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function totalRecords(): int

public function getCompanies(int $limit, int $offset): array
{
$query = 'SELECT a.*, b.uuid AS main_company_uuid FROM companies a LEFT JOIN companies b ON a.main_company_id = b.id LIMIT :limit OFFSET :offset';
$query = 'SELECT c.*, main_company.uuid AS main_company_uuid, b.uuid AS brand_uuid FROM companies c LEFT JOIN companies main_company ON c.main_company_id = main_company.id LEFT JOIN brands b ON c.brand_id = b.id LIMIT :limit OFFSET :offset';

return DB::connection('sp_hub')->select($query, [
'limit' => $limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function import(stdClass $permission): void
->first();

if (! $permissionModel) {
$permissionModel = new $permissionModelFromConfig();
$permissionModel = new $permissionModelFromConfig;
}

$permissionModel->name = $permission->name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function import(stdClass $position): void
->where('uuid', $position->uuid)
->first();
if (! $positionModel) {
$positionModel = new $positionClass();
$positionModel = new $positionClass;
}
$positionModel->uuid = $position->uuid;
$positionModel->name = $position->name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function import(stdClass $role): void
->first();

if (! $roleModel) {
$roleModel = new $roleModelFromConfig();
$roleModel = new $roleModelFromConfig;
}

$roleModel->uuid = $role->uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function import(stdClass $userCompany): void
->where('uuid', $userCompany->uuid)
->first();
if (! $userCompanyModel) {
$userCompanyModel = new $userCompanyClass();
$userCompanyModel = new $userCompanyClass;
}

$userCompanyModel->uuid = $userCompany->uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function import(stdClass $userCompany): void
->where('user_company_parent_id', $this->getUserCompanyId($userCompany->user_company_parent_uuid))
->first();
if (! $userCompanyModel) {
$userCompanyModel = new $userCompanyClass();
$userCompanyModel = new $userCompanyClass;
}

$userCompanyModel->user_company_id = $this->getUserCompanyId($userCompany->user_company_uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function import(stdClass $userCompany): void
$userCompanyModel = $userCompanyClass::where('user_company_id', $this->getUserCompanyId($userCompany->user_company_uuid))
->first();
if (! $userCompanyModel) {
$userCompanyModel = new $userCompanyClass();
$userCompanyModel = new $userCompanyClass;
}

$userCompanyModel->user_company_id = $this->getUserCompanyId($userCompany->user_company_uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function import(stdClass $user): void
->where('hub_uuid', $user->uuid)
->first();
if (! $userModel) {
$userModel = new $userClass();
$userModel = new $userClass;
$userModel->password = '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi';
}
$userModel->hub_uuid = $user->uuid;
Expand Down
28 changes: 28 additions & 0 deletions src/Console/Commands/Messages/Resources/Helpers/BrandsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers;

use BildVitta\Hub\Entities\HubBrand;

trait BrandsHelper
{
protected function brandCreateOrUpdate(\stdClass $message)
{
if (! $brand = HubBrand::withTrashed()->where('uuid', $message->uuid)->first()) {
$brand = new HubBrand;
$brand->uuid = $message->uuid;
}

$brand->name = $message->name;
$brand->created_at = $message->created_at;
$brand->updated_at = $message->updated_at;
$brand->deleted_at = $message->deleted_at;

$brand->save();
}

protected function brandDelete(\stdClass $message)
{
HubBrand::where('uuid', $message->uuid)->delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers;

use BildVitta\Hub\Entities\HubBrand;
use BildVitta\SpHub\Models\HubCompany;
use stdClass;

Expand All @@ -10,7 +11,7 @@ trait CompanyHelper
private function companyCreateOrUpdate(stdClass $message): void
{
if (! $company = HubCompany::withTrashed()->where('uuid', $message->uuid)->first()) {
$company = new HubCompany();
$company = new HubCompany;
$company->uuid = $message->uuid;
}
$company->name = $message->name;
Expand All @@ -27,6 +28,15 @@ private function companyCreateOrUpdate(stdClass $message): void
$company->main_company_id = $message->main_company_id ?? null;
}

if (property_exists($message, 'brand_uuid')) {
$company->brand_id = null;
if ($message->brand_uuid) {
$company->brand_id = HubBrand::withTrashed()->where('uuid', $message->brand_uuid)->value('id');
}
} else {
$company->brand_id = $message->brand_id ?? null;
}

$userModel = app(config('hub.model_user'));
if ($this->userHasExtraFields($userModel->getFillable())) {
$company->document = $message->document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers;

use BildVitta\SpHub\Events\Users\UserCompanyUpdated;
use Illuminate\Support\Facades\Log;
use Spatie\Permission\Models\Permission;
use stdClass;

Expand All @@ -16,7 +15,7 @@ private function userCompaniesCreateOrUpdate(stdClass $message): void
->where('uuid', $message->uuid)
->first();
if (! $userCompanyModel) {
$userCompanyModel = new $userCompanyClass();
$userCompanyModel = new $userCompanyClass;
}

$userCompanyModel->uuid = $message->uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait LogHelper
private function logError(Throwable $exception, $message): void
{
try {
$worker = new Worker();
$worker = new Worker;
$worker->type = 'rabbitmq.worker.error';
$worker->payload = [
'message' => $message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private function positionCreateOrUpdate(stdClass $message): void
->where('uuid', $message->uuid)
->first();
if (! $positionModel) {
$positionModel = new $positionClass();
$positionModel = new $positionClass;
}

$positionModel->uuid = $message->uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private function roleCreateOrUpdate(stdClass $message): void
->first();

if (! $roleModel) {
$roleModel = new $roleModelFromConfig();
$roleModel = new $roleModelFromConfig;
}

$roleModel->uuid = $message->uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private function userCreateOrUpdate(stdClass $message): void
{
$modelUser = config('sp-hub.model_user');
if (! $user = $modelUser::withTrashed()->where('hub_uuid', $message->uuid)->first()) {
$user = new $modelUser();
$user = new $modelUser;
$user->hub_uuid = $message->uuid;
$user->password = Hash::make('password');
}
Expand Down
20 changes: 20 additions & 0 deletions src/Console/Commands/Messages/Resources/MessageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BildVitta\SpHub\Console\Commands\Messages\Resources;

use BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers\BrandsHelper;
use BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers\CompanyHelper;
use BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers\CompanyLinksHelper;
use BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers\LogHelper;
Expand All @@ -15,6 +16,7 @@

class MessageProcessor
{
use BrandsHelper;
use CompanyHelper;
use CompanyLinksHelper;
use LogHelper;
Expand Down Expand Up @@ -53,6 +55,8 @@ class MessageProcessor
*/
public const USER_COMPANIES = 'user_companies';

public const BRANDS = 'brands';

/**
* @var string
*/
Expand Down Expand Up @@ -87,6 +91,9 @@ public function process(AMQPMessage $message): void
$operation = $properties[1];

switch ($type) {
case self::BRANDS:
$this->brands($messageData, $operation);
break;
case self::USERS:
$this->users($messageData, $operation);
break;
Expand Down Expand Up @@ -114,6 +121,19 @@ public function process(AMQPMessage $message): void
}
}

private function brands(stdClass $message, string $operation): void
{
switch ($operation) {
case self::CREATED:
case self::UPDATED:
$this->brandCreateOrUpdate($message);
break;
case self::DELETED:
$this->brandDelete($message);
break;
}
}

private function users(stdClass $message, string $operation): void
{
switch ($operation) {
Expand Down
4 changes: 1 addition & 3 deletions src/Events/Permissions/SupervisorBrokerUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ class SupervisorBrokerUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(public stdClass $message)
{
}
public function __construct(public stdClass $message) {}
}
Loading

0 comments on commit dea0a5e

Please sign in to comment.