From 6854c6c1f9ff3f6e3a4d92ce6d33f4ffe294c305 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Feb 2021 14:42:02 +1100 Subject: [PATCH 1/4] Fixes for multi db migration --- app/Console/Commands/DesignUpdate.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/Console/Commands/DesignUpdate.php b/app/Console/Commands/DesignUpdate.php index c0d1ac8c5a4a..a5de9a59eaa4 100644 --- a/app/Console/Commands/DesignUpdate.php +++ b/app/Console/Commands/DesignUpdate.php @@ -49,19 +49,26 @@ class DesignUpdate extends Command */ public function handle() { + //always return state to first DB + $current_db = config('database.default'); if (! config('ninja.db.multi_db_enabled')) { $this->handleOnDb(); } else { + //multiDB environment, need to foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); $this->handleOnDb($db); } + + MultiDB::setDB($current_db); + } + } private function handleOnDb() From 0a1b4b26b975cd6dd7bfb0f55e5abb71e2c50c15 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Feb 2021 15:10:05 +1100 Subject: [PATCH 2/4] Fixes for tests --- app/Libraries/MultiDB.php | 5 ++++- phpunit.xml | 2 +- tests/Unit/InvitationTest.php | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 2fed0652a6b9..aa84ef5993fb 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -160,7 +160,8 @@ class MultiDB public static function userFindAndSetDb($email) : bool { - //multi-db active + //multi-db active + foreach (self::$dbs as $db) { if (User::on($db)->where(['email' => $email])->get()->count() >= 1) { // if user already exists, validation will fail return true; @@ -172,6 +173,8 @@ class MultiDB public static function findAndSetDb($token) :bool { + nlog($token); + foreach (self::$dbs as $db) { if ($ct = CompanyToken::on($db)->whereRaw('BINARY `token`= ?', [$token])->first()) { self::setDb($ct->company->db); diff --git a/phpunit.xml b/phpunit.xml index e1b0575994e1..e1ef466e8b20 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -22,7 +22,7 @@ - + diff --git a/tests/Unit/InvitationTest.php b/tests/Unit/InvitationTest.php index 76db0cd24020..0bee7f83e1d1 100644 --- a/tests/Unit/InvitationTest.php +++ b/tests/Unit/InvitationTest.php @@ -34,6 +34,7 @@ class InvitationTest extends TestCase public function testInvitationSanity() { + $this->assertEquals($this->invoice->invitations->count(), 2); $invitations = $this->invoice->invitations()->get(); @@ -46,11 +47,20 @@ class InvitationTest extends TestCase $this->invoice->line_items = []; + $response = null; + + try { + $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $this->invoice->toArray()) - ->assertStatus(200); + ])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $this->invoice->toArray()); + } catch (\Exception $e) { + + nlog($e->getMessage()); + } + + $response->assertStatus(200); $arr = $response->json(); From 7981093d9742248e7a9cf3e071ba7ee6c437ee43 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Feb 2021 17:08:16 +1100 Subject: [PATCH 3/4] Working on migration email notifications --- app/Jobs/Ninja/CheckCompanyData.php | 57 +++++++++++-------- app/Jobs/Util/Import.php | 7 +-- app/Libraries/MultiDB.php | 5 +- app/Mail/MigrationCompleted.php | 2 +- .../views/email/import/completed.blade.php | 6 +- 5 files changed, 42 insertions(+), 35 deletions(-) diff --git a/app/Jobs/Ninja/CheckCompanyData.php b/app/Jobs/Ninja/CheckCompanyData.php index 35bf20201016..27912c84eb21 100644 --- a/app/Jobs/Ninja/CheckCompanyData.php +++ b/app/Jobs/Ninja/CheckCompanyData.php @@ -25,6 +25,7 @@ use Illuminate\Queue\Middleware\RateLimited; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\DB; use Turbo124\Beacon\Jobs\Database\MySQL\DbStatus; +use Illuminate\Support\Facades\Cache; class CheckCompanyData implements ShouldQueue { @@ -36,6 +37,7 @@ class CheckCompanyData implements ShouldQueue public $company_data = []; + public $is_valid; /** * Create a new job instance. * @@ -54,6 +56,7 @@ class CheckCompanyData implements ShouldQueue */ public function handle() { + $this->is_valid = true; $this->checkInvoiceBalances(); $this->checkInvoicePayments(); @@ -68,15 +71,14 @@ class CheckCompanyData implements ShouldQueue else $cache_instance = Cache::add($this->hash, ''); - $cache_instance[$this->company->company_hash] = $this->company_data; + $this->company_data['company_hash'] = $this->company->company_hash; Cache::put($this->hash, $cache_instance, now()->addMinutes(30)); nlog(Cache::get($this->hash)); - nlog($this->company_data); - if(!$this->isValid) + if(!$this->is_valid) $this->company_data['status'] = 'errors'; else $this->company_data['status'] = 'success'; @@ -109,13 +111,13 @@ class CheckCompanyData implements ShouldQueue $wrong_balances++; - $this->company_data[] = "# {$client->id} " . $client->present()->name.' - '.$client->number." - Balance Failure - Invoice Balances = {$invoice_balance} Client Balance = {$client->balance} Ledger Balance = {$ledger->balance} \n"; + $this->company_data[] = "# {$client->id} " . $client->present()->name.' - '.$client->number." - Balance Failure - Invoice Balances = {$invoice_balance} Client Balance = {$client->balance} Ledger Balance = {$ledger->balance} "; - $this->isValid = false; + $this->is_valid = false; } } - $this->company_data[] = "{$wrong_balances} clients with incorrect balances\n"; + $this->company_data[] = "{$wrong_balances} clients with incorrect balances"; } private function checkInvoicePayments() @@ -135,14 +137,14 @@ class CheckCompanyData implements ShouldQueue if ((string)$total_paid != (string)($invoice->amount - $invoice->balance - $total_credit)) { $wrong_balances++; - $this->company_data[] = $client->present()->name.' - '.$client->id." - Total Amount = {$total_amount} != Calculated Total = {$calculated_paid_amount} - Total Refund = {$total_refund} Total credit = {$total_credit}\n"; + $this->company_data[] = $client->present()->name.' - '.$client->id." - Total Amount = {$total_amount} != Calculated Total = {$calculated_paid_amount} - Total Refund = {$total_refund} Total credit = {$total_credit}"; - $this->isValid = false; + $this->is_valid = false; } }); }); - $this->company_data[] = "{$wrong_balances} clients with incorrect invoice balances\n"; + $this->company_data[] = "{$wrong_balances} clients with incorrect invoice balances"; } private function checkPaidToDates() @@ -176,9 +178,9 @@ class CheckCompanyData implements ShouldQueue if (round($total_invoice_payments, 2) != round($client->paid_to_date, 2)) { $wrong_paid_to_dates++; - $this->company_data[] = $client->present()->name.'id = # '.$client->id." - Paid to date does not match Client Paid To Date = {$client->paid_to_date} - Invoice Payments = {$total_invoice_payments}\n"; + $this->company_data[] = $client->present()->name.'id = # '.$client->id." - Paid to date does not match Client Paid To Date = {$client->paid_to_date} - Invoice Payments = {$total_invoice_payments}"; - $this->isValid = false; + $this->is_valid = false; } }); @@ -206,13 +208,13 @@ class CheckCompanyData implements ShouldQueue if ($ledger && (string) $invoice_balance != (string) $client->balance) { $wrong_paid_to_dates++; - $this->company_data[] = $client->present()->name.' - '.$client->id." - calculated client balances do not match {$invoice_balance} - ".rtrim($client->balance, '0')."\n"; + $this->company_data[] = $client->present()->name.' - '.$client->id." - calculated client balances do not match {$invoice_balance} - ".rtrim($client->balance, '0').""; - $this->isValid = false; + $this->is_valid = false; } } - $this->company_data[] = "{$wrong_paid_to_dates} clients with incorrect client balances\n"; + $this->company_data[] = "{$wrong_paid_to_dates} clients with incorrect client balances"; } private function checkContacts() @@ -224,10 +226,10 @@ class CheckCompanyData implements ShouldQueue ->orderBy('id') ->get(['id']); - $this->company_data[] = $contacts->count().' contacts without a contact_key\n'; + $this->company_data[] = $contacts->count().' contacts without a contact_key'; if ($contacts->count() > 0) { - $this->isValid = false; + $this->is_valid = false; } // if ($this->option('fix') == 'true') { @@ -244,7 +246,7 @@ class CheckCompanyData implements ShouldQueue // check for missing contacts $clients = DB::table('clients') - ->where('company_id', $this->company->id) + ->where('clients.company_id', $this->company->id) ->leftJoin('client_contacts', function ($join) { $join->on('client_contacts.client_id', '=', 'clients.id') ->whereNull('client_contacts.deleted_at'); @@ -258,10 +260,10 @@ class CheckCompanyData implements ShouldQueue $clients = $clients->get(['clients.id', 'clients.user_id', 'clients.company_id']); - $this->company_data[] = $clients->count().' clients without any contacts\n'; + $this->company_data[] = $clients->count().' clients without any contacts'; if ($clients->count() > 0) { - $this->isValid = false; + $this->is_valid = false; } // if ($this->option('fix') == 'true') { @@ -293,10 +295,10 @@ class CheckCompanyData implements ShouldQueue // } $clients = $clients->get(['clients.id', DB::raw('count(client_contacts.id)')]); - $this->company_data[] = $clients->count().' clients without a single primary contact\n'; + $this->company_data[] = $clients->count().' clients without a single primary contact'; if ($clients->count() > 0) { - $this->isValid = false; + $this->is_valid = false; } } @@ -335,11 +337,20 @@ class CheckCompanyData implements ShouldQueue ->get(["{$table}.id"]); if ($records->count()) { - $this->isValid = false; - $this->company_data[] = $records->count()." {$table} records with incorrect {$entityType} company id\n"; + $this->is_valid = false; + $this->company_data[] = $records->count()." {$table} records with incorrect {$entityType} company id"; } } } } + + public function pluralizeEntityType($type) + { + if ($type === 'company') { + return 'companies'; + } + + return $type.'s'; + } } diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 427e10510a16..a5b6d619546b 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -210,12 +210,11 @@ class Import implements ShouldQueue // $this->fixClientBalances(); $check_data = CheckCompanyData::dispatchNow($this->company, md5(time())); - - if($check_data['status'] == 'errors') - throw new ProcessingMigrationArchiveFailed($check_data); + // if($check_data['status'] == 'errors') + // throw new ProcessingMigrationArchiveFailed(implode("\n", $check_data)); Mail::to($this->user) - ->send(new MigrationCompleted($this->company, $check_data)); + ->send(new MigrationCompleted($this->company, implode("
",$check_data))); /*After a migration first some basic jobs to ensure the system is up to date*/ VersionCheck::dispatch(); diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index aa84ef5993fb..2fed0652a6b9 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -160,8 +160,7 @@ class MultiDB public static function userFindAndSetDb($email) : bool { - //multi-db active - + //multi-db active foreach (self::$dbs as $db) { if (User::on($db)->where(['email' => $email])->get()->count() >= 1) { // if user already exists, validation will fail return true; @@ -173,8 +172,6 @@ class MultiDB public static function findAndSetDb($token) :bool { - nlog($token); - foreach (self::$dbs as $db) { if ($ct = CompanyToken::on($db)->whereRaw('BINARY `token`= ?', [$token])->first()) { self::setDb($ct->company->db); diff --git a/app/Mail/MigrationCompleted.php b/app/Mail/MigrationCompleted.php index 5e7baf470f87..e81f72356496 100644 --- a/app/Mail/MigrationCompleted.php +++ b/app/Mail/MigrationCompleted.php @@ -34,7 +34,7 @@ class MigrationCompleted extends Mailable public function build() { $data['settings'] = $this->company->settings; - $data['company'] = $this->company; + $data['company'] = $this->company->fresh(); $data['whitelabel'] = $this->company->account->isPaid() ? true : false; $data['check_data'] = $this->check_data; diff --git a/resources/views/email/import/completed.blade.php b/resources/views/email/import/completed.blade.php index fcaef0824bbc..4813e4c0077d 100644 --- a/resources/views/email/import/completed.blade.php +++ b/resources/views/email/import/completed.blade.php @@ -10,8 +10,8 @@

- @if(isset($company) && count($company->clients) >=1) -

Clients Imported: {{ count($company->clients) }}

+ @if(isset($company) && $company->clients->count() >=1) +

Clients Imported: {{ $company->clients->count() }}

@endif @if(isset($company) && count($company->products) >=1) @@ -75,7 +75,7 @@

Data Quality:

-

{{ $check_data }}

+

{!! $check_data !!}

{{ ctrans('texts.account_login')}} From 85df91a6e8fd437f0d63b9127a262efbce2bc0ce Mon Sep 17 00:00:00 2001 From: = Date: Wed, 10 Feb 2021 21:43:32 +1100 Subject: [PATCH 4/4] Add redirect URI to google auth requests --- app/Http/Controllers/Auth/LoginController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 9e08392db093..fe3b7144a1f3 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -289,7 +289,7 @@ class LoginController extends BaseController $client = new Google_Client(); $client->setClientId(config('ninja.auth.google.client_id')); $client->setClientSecret(config('ninja.auth.google.client_secret')); - + $client->setRedirectUri(config('ninja.app_url')); $token = $client->authenticate(request()->input('server_auth_code')); $refresh_token = '';