Type checking

This commit is contained in:
David Bomba 2022-04-21 12:07:08 +10:00
parent bdf95fcf70
commit ebc498a815
4 changed files with 199 additions and 2 deletions

View File

@ -0,0 +1,162 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Console\Commands;
use App\Http\ValidationRules\ValidClientGroupSettingsRule;
use App\Libraries\MultiDB;
use App\Models\Backup;
use App\Models\Client;
use App\Models\Company;
use App\Models\Design;
use App\Utils\Traits\ClientGroupSettingsSaver;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use stdClass;
class TypeCheck extends Command
{
use ClientGroupSettingsSaver;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ninja:type-check {--all=} {--client_id=} {--company_id=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check Settings Types';
protected $log = '';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//always return state to first DB
$current_db = config('database.default');
if($this->option('all'))
{
if (! config('ninja.db.multi_db_enabled')) {
$this->checkAll();
} else {
foreach (MultiDB::$dbs as $db)
{
MultiDB::setDB($db);
$this->checkAll();
}
MultiDB::setDB($current_db);
}
}
if($this->option('client_id'))
{
$client = MultiDB::findAndSetDbByClientId($this->option('client_id'));
if($client)
$this->checkClient($client);
else
$this->logMessage(date('Y-m-d h:i:s').' Could not find this client');
}
if($this->option('company_id'))
{
$company = MultiDB::findAndSetDbByCompanyId($this->option('company_id'));
if($company)
$this->checkCompany($company);
else
$this->logMessage(date('Y-m-d h:i:s').' Could not find this company');
}
}
private function checkClient($client)
{
$this->logMessage(date('Y-m-d h:i:s').' Checking Client => ' . $client->present()->name(). " " . $client->id);
$entity_settings = $this->checkSettingType($client->settings);
$entity_settings->md5 = md5(time());
$client->settings = $entity_settings;
$client->save();
}
private function checkCompany($company)
{
$this->logMessage(date('Y-m-d h:i:s').' Checking Company => ' . $company->present()->name(). " " . $company->id);
$company->saveSettings($company->settings, $company);
}
private function checkAll()
{
$this->logMessage(date('Y-m-d h:i:s').' Checking all clients and companies.');
Client::cursor()->each( function ($client) {
$entity_settings = $this->checkSettingType($client->settings);
$entity_settings->md5 = md5(time());
$client->settings = $entity_settings;
$client->save();
});
Company::cursor()->each( function ($company) {
$company->saveSettings($company->settings, $company);
});
}
private function logMessage($str)
{
$str = date('Y-m-d h:i:s').' '.$str;
$this->info($str);
$this->log .= $str."\n";
}
}

View File

@ -36,7 +36,8 @@ class StoreClientRequest extends Request
}
public function rules()
{nlog($this->input);
{
if ($this->input('documents') && is_array($this->input('documents'))) {
$documents = count($this->input('documents'));

View File

@ -284,6 +284,22 @@ class MultiDB
return false;
}
public static function findAndSetDbByCompanyId($company_id) :?Company
{
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if ($company = Company::on($db)->where('id', $company_id)->first()) {
self::setDb($db);
return $company;
}
}
self::setDB($current_db);
return false;
}
public static function findAndSetDbByAccountKey($account_key) :bool
{
$current_db = config('database.default');
@ -332,6 +348,22 @@ class MultiDB
return false;
}
public static function findAndSetDbByClientId($client_id) :?Client
{
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if ($client = Client::on($db)->where('id', $client_id)->first()) {
self::setDb($db);
return $client;
}
}
self::setDB($current_db);
return false;
}
public static function findAndSetDbByDomain($query_array)
{

View File

@ -176,7 +176,9 @@ trait ClientGroupSettingsSaver
if (! property_exists($settings, $key)) {
continue;
} elseif ($this->checkAttribute($value, $settings->{$key})) {
if (substr($key, -3) == '_id') {
if (substr($key, -3) == '_id'||
($key == 'payment_terms' && property_exists($settings, 'payment_terms') && strlen($settings->{$key}) >= 1) ||
($key == 'valid_until' && property_exists($settings, 'valid_until') && strlen($settings->{$key}) >= 1)) {
settype($settings->{$key}, 'string');
} else {
settype($settings->{$key}, $value);