Improve efficiency of queries relying on Country

This commit is contained in:
David Bomba 2022-07-28 10:58:13 +10:00
parent 9916288cab
commit d93bac1b09
18 changed files with 177 additions and 60 deletions

View File

@ -520,7 +520,7 @@ class CompanyController extends BaseController
$nmo->company = $other_company;
$nmo->settings = $other_company->settings;
$nmo->to_user = auth()->user();
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
$company->delete();

View File

@ -311,7 +311,7 @@ class MigrationController extends BaseController
$nmo->company = $user->account->companies()->first();
$nmo->settings = $user->account->companies()->first()->settings;
$nmo->to_user = $user;
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
return;
} elseif ($existing_company && $company_count > 10) {
@ -320,7 +320,7 @@ class MigrationController extends BaseController
$nmo->company = $user->account->companies()->first();
$nmo->settings = $user->account->companies()->first()->settings;
$nmo->to_user = $user;
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
return;
}
@ -340,7 +340,7 @@ class MigrationController extends BaseController
$nmo->settings = $user->account->companies()->first();
$nmo->to_user = $user;
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
return response()->json([
'_id' => Str::uuid(),

View File

@ -50,8 +50,9 @@ class QueryLogging
$timeEnd = microtime(true);
$time = $timeEnd - $timeStart;
// info("Query count = {$count}");
nlog("Query count = {$count}");
nlog($queries);
if ($count > 175) {
nlog("Query count = {$count}");
nlog($queries);

View File

@ -595,7 +595,7 @@ class BaseImport
$nmo->settings = $this->company->settings;
$nmo->to_user = $this->company->owner();
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
}
public function preTransform(array $data, $entity_type)

View File

@ -126,7 +126,7 @@ class CreateAccount
$nmo->settings = $sp035a66->settings;
$nmo->to_user = $sp035a66->owner();
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
\Modules\Admin\Jobs\Account\NinjaUser::dispatch([], $sp035a66);
}

View File

@ -108,9 +108,6 @@ class EmailEntity implements ShouldQueue
/* Set DB */
MultiDB::setDB($this->company->db);
if(Ninja::isHosted() && !$this->company->account->account_sms_verified)
return;
App::forgetInstance('translator');
$t = app('translator');
App::setLocale($this->invitation->contact->preferredLocale());

View File

@ -237,8 +237,6 @@ class NinjaMailerJob implements ShouldQueue
private function setGmailMailer()
{
if(LaravelGmail::check())
LaravelGmail::logout();
$sending_user = $this->nmo->settings->gmail_sending_user_id;
@ -265,7 +263,7 @@ class NinjaMailerJob implements ShouldQueue
$google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
sleep(rand(2,6));
sleep(rand(2,4));
}
catch(\Exception $e) {
$this->logMailError('Gmail Token Invalid', $this->company->clients()->first());
@ -347,6 +345,10 @@ class NinjaMailerJob implements ShouldQueue
if(class_exists(\Modules\Admin\Jobs\Account\EmailQuality::class))
return (new \Modules\Admin\Jobs\Account\EmailQuality($this->nmo, $this->company))->run();
/* On the hosted platform if the user has not verified their account we fail here */
if(Ninja::isHosted() && $this->company->account && !$this->company->account->account_sms_verified)
return true;
return false;
}

View File

@ -45,7 +45,7 @@ class CreateAccountActivity implements ShouldQueue
$nmo->settings = $event->company->settings;
$nmo->to_user = $event->user;
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
}
}
}

View File

@ -62,7 +62,7 @@ class SendVerificationNotification implements ShouldQueue
$nmo->company = $event->company;
$nmo->settings = $event->company->settings;
$nmo->to_user = $event->creating_user;
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
}
}
}

View File

@ -59,7 +59,7 @@ class UpdateUserLastLogin implements ShouldQueue
$nmo->company = $user->account->companies->first();
$nmo->settings = $user->account->companies->first()->settings;
$nmo->to_user = $user;
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
$user->ip = $ip;
$user->save();

View File

@ -427,7 +427,7 @@ class Account extends BaseModel
$nmo->company = $this->companies()->first();
$nmo->settings = $this->companies()->first()->settings;
$nmo->to_user = $this->companies()->first()->owner();
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
Cache::put("throttle_notified:{$this->key}", true, 60 * 24);
@ -467,7 +467,7 @@ class Account extends BaseModel
$nmo->company = $this->companies()->first();
$nmo->settings = $this->companies()->first()->settings;
$nmo->to_user = $this->companies()->first()->owner();
NinjaMailerJob::dispatch($nmo);
NinjaMailerJob::dispatch($nmo, true);
Cache::put("gmail_credentials_notified:{$this->key}", true, 60 * 24);

View File

@ -340,8 +340,21 @@ class Company extends BaseModel
*/
public function country()
{
$companies = Cache::get('countries');
if (! $companies) {
$this->buildCache(true);
$companies = Cache::get('countries');
}
return $companies->filter(function ($item) {
return $item->id == $this->getSetting('country_id');
})->first();
// return $this->belongsTo(Country::class);
return Country::find($this->settings->country_id);
// return Country::find($this->settings->country_id);
}
public function group_settings()

View File

@ -64,7 +64,7 @@ class Vendor extends BaseModel
protected $touches = [];
protected $with = [
// 'contacts',
'company',
];
protected $presenter = VendorPresenter::class;

View File

@ -22,15 +22,18 @@ use App\Models\RecurringInvoiceInvitation;
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
use App\Utils\Ninja;
use App\Utils\Number;
use App\Utils\Traits\AppSetup;
use App\Utils\Traits\MakesDates;
use App\Utils\transformTranslations;
use Exception;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
class HtmlEngine
{
use MakesDates;
use AppSetup;
public $entity;
public $invitation;
@ -644,7 +647,27 @@ class HtmlEngine
private function getCountryName() :string
{
$country = Country::find($this->settings->country_id);
$countries = Cache::get('countries');
if (! $countries) {
$this->buildCache(true);
$countries = Cache::get('countries');
}
if($countries){
$country = $countries->filter(function ($item) {
return $item->id == $this->settings->country_id;
})->first();
}
else
$country = Country::find($this->settings->country_id);
if ($country) {
return ctrans('texts.country_' . $country->name);

View File

@ -22,14 +22,17 @@ use App\Models\RecurringInvoiceInvitation;
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
use App\Utils\Ninja;
use App\Utils\Number;
use App\Utils\Traits\AppSetup;
use App\Utils\Traits\MakesDates;
use App\Utils\transformTranslations;
use Exception;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
class VendorHtmlEngine
{
use MakesDates;
use AppSetup;
public $entity;
@ -483,7 +486,27 @@ class VendorHtmlEngine
private function getCountryName() :string
{
$country = Country::find($this->settings->country_id);
$countries = Cache::get('countries');
if (! $countries) {
$this->buildCache(true);
$countries = Cache::get('countries');
}
if($countries){
$country = $countries->filter(function ($item) {
return $item->id == $this->settings->country_id;
})->first();
}
else
$country = Country::find($this->settings->country_id);
if ($country) {
return ctrans('texts.country_' . $country->name);

View File

@ -96,6 +96,7 @@
"require-dev": {
"php": "^7.4|^8.0",
"barryvdh/laravel-debugbar": "^3.6",
"beyondcode/laravel-query-detector": "^1.6",
"brianium/paratest": "^6.1",
"darkaonline/l5-swagger": "8.1.0",
"fakerphp/faker": "^1.14",

123
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "624b345504e2b9bb3bf1c7a9c7a913d3",
"content-hash": "bbd6f8107bd70628ea7527c968ca3c8d",
"packages": [
{
"name": "afosto/yaac",
@ -378,16 +378,16 @@
},
{
"name": "aws/aws-sdk-php",
"version": "3.231.14",
"version": "3.231.15",
"source": {
"type": "git",
"url": "https://github.com/aws/aws-sdk-php.git",
"reference": "6b79b9c8204813d9674ffa7badcd567d7f608165"
"reference": "ba379285d24b609a997bd8b40933d3e0a3826dfb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/6b79b9c8204813d9674ffa7badcd567d7f608165",
"reference": "6b79b9c8204813d9674ffa7badcd567d7f608165",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/ba379285d24b609a997bd8b40933d3e0a3826dfb",
"reference": "ba379285d24b609a997bd8b40933d3e0a3826dfb",
"shasum": ""
},
"require": {
@ -464,9 +464,9 @@
"support": {
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
"issues": "https://github.com/aws/aws-sdk-php/issues",
"source": "https://github.com/aws/aws-sdk-php/tree/3.231.14"
"source": "https://github.com/aws/aws-sdk-php/tree/3.231.15"
},
"time": "2022-07-26T18:20:14+00:00"
"time": "2022-07-27T18:59:36+00:00"
},
{
"name": "bacon/bacon-qr-code",
@ -685,16 +685,16 @@
},
{
"name": "checkout/checkout-sdk-php",
"version": "2.5.2",
"version": "2.5.3",
"source": {
"type": "git",
"url": "https://github.com/checkout/checkout-sdk-php.git",
"reference": "ab1e493ac9aa9c0350f58f069f6157b2a02a7e27"
"reference": "d1c012d905cbd1a04869e1d34f2438ac24bf02b6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/checkout/checkout-sdk-php/zipball/ab1e493ac9aa9c0350f58f069f6157b2a02a7e27",
"reference": "ab1e493ac9aa9c0350f58f069f6157b2a02a7e27",
"url": "https://api.github.com/repos/checkout/checkout-sdk-php/zipball/d1c012d905cbd1a04869e1d34f2438ac24bf02b6",
"reference": "d1c012d905cbd1a04869e1d34f2438ac24bf02b6",
"shasum": ""
},
"require": {
@ -747,9 +747,9 @@
],
"support": {
"issues": "https://github.com/checkout/checkout-sdk-php/issues",
"source": "https://github.com/checkout/checkout-sdk-php/tree/2.5.2"
"source": "https://github.com/checkout/checkout-sdk-php/tree/2.5.3"
},
"time": "2022-06-30T14:46:15+00:00"
"time": "2022-07-27T15:56:30+00:00"
},
{
"name": "cleverit/ubl_invoice",
@ -1323,34 +1323,31 @@
},
{
"name": "doctrine/event-manager",
"version": "1.1.1",
"version": "1.1.2",
"source": {
"type": "git",
"url": "https://github.com/doctrine/event-manager.git",
"reference": "41370af6a30faa9dc0368c4a6814d596e81aba7f"
"reference": "eb2ecf80e3093e8f3c2769ac838e27d8ede8e683"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/event-manager/zipball/41370af6a30faa9dc0368c4a6814d596e81aba7f",
"reference": "41370af6a30faa9dc0368c4a6814d596e81aba7f",
"url": "https://api.github.com/repos/doctrine/event-manager/zipball/eb2ecf80e3093e8f3c2769ac838e27d8ede8e683",
"reference": "eb2ecf80e3093e8f3c2769ac838e27d8ede8e683",
"shasum": ""
},
"require": {
"php": "^7.1 || ^8.0"
},
"conflict": {
"doctrine/common": "<2.9@dev"
"doctrine/common": "<2.9"
},
"require-dev": {
"doctrine/coding-standard": "^6.0",
"phpunit/phpunit": "^7.0"
"doctrine/coding-standard": "^9",
"phpstan/phpstan": "~1.4.10 || ^1.5.4",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
"vimeo/psalm": "^4.22"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Doctrine\\Common\\": "lib/Doctrine/Common"
@ -1397,7 +1394,7 @@
],
"support": {
"issues": "https://github.com/doctrine/event-manager/issues",
"source": "https://github.com/doctrine/event-manager/tree/1.1.x"
"source": "https://github.com/doctrine/event-manager/tree/1.1.2"
},
"funding": [
{
@ -1413,7 +1410,7 @@
"type": "tidelift"
}
],
"time": "2020-05-29T18:28:51+00:00"
"time": "2022-07-27T22:18:11+00:00"
},
{
"name": "doctrine/inflector",
@ -5406,16 +5403,16 @@
},
{
"name": "nesbot/carbon",
"version": "2.59.1",
"version": "2.60.0",
"source": {
"type": "git",
"url": "https://github.com/briannesbitt/Carbon.git",
"reference": "a9000603ea337c8df16cc41f8b6be95a65f4d0f5"
"reference": "00a259ae02b003c563158b54fb6743252b638ea6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/a9000603ea337c8df16cc41f8b6be95a65f4d0f5",
"reference": "a9000603ea337c8df16cc41f8b6be95a65f4d0f5",
"url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/00a259ae02b003c563158b54fb6743252b638ea6",
"reference": "00a259ae02b003c563158b54fb6743252b638ea6",
"shasum": ""
},
"require": {
@ -5504,7 +5501,7 @@
"type": "tidelift"
}
],
"time": "2022-06-29T21:43:55+00:00"
"time": "2022-07-27T15:57:48+00:00"
},
{
"name": "nette/schema",
@ -12753,6 +12750,66 @@
],
"time": "2022-07-11T09:26:42+00:00"
},
{
"name": "beyondcode/laravel-query-detector",
"version": "1.6.0",
"source": {
"type": "git",
"url": "https://github.com/beyondcode/laravel-query-detector.git",
"reference": "8261d80c71c97e994c1021fe5c3bd2a1c27106fc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/beyondcode/laravel-query-detector/zipball/8261d80c71c97e994c1021fe5c3bd2a1c27106fc",
"reference": "8261d80c71c97e994c1021fe5c3bd2a1c27106fc",
"shasum": ""
},
"require": {
"illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
"php": "^7.1 || ^8.0"
},
"require-dev": {
"laravel/legacy-factories": "^1.0",
"orchestra/testbench": "^3.0 || ^4.0 || ^5.0 || ^6.0",
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"BeyondCode\\QueryDetector\\QueryDetectorServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"BeyondCode\\QueryDetector\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Marcel Pociot",
"email": "marcel@beyondco.de",
"homepage": "https://beyondcode.de",
"role": "Developer"
}
],
"description": "Laravel N+1 Query Detector",
"homepage": "https://github.com/beyondcode/laravel-query-detector",
"keywords": [
"beyondcode",
"laravel-query-detector"
],
"support": {
"issues": "https://github.com/beyondcode/laravel-query-detector/issues",
"source": "https://github.com/beyondcode/laravel-query-detector/tree/1.6.0"
},
"time": "2022-02-12T16:23:40+00:00"
},
{
"name": "brianium/paratest",
"version": "v6.6.1",
@ -16650,5 +16707,5 @@
"platform-dev": {
"php": "^7.4|^8.0"
},
"plugin-api-version": "2.3.0"
"plugin-api-version": "2.1.0"
}

View File

@ -39,9 +39,9 @@ class ApplePayDomainMerchantUrlTest extends TestCase
public function testMerchantFieldGet()
{
if (! config('ninja.testvars.stripe')) {
// if (! config('ninja.testvars.stripe')) {
$this->markTestSkipped('Skip test no company gateways installed');
}
// }
$config = new \stdClass;
$config->publishableKey = 'pk_test';
@ -56,7 +56,7 @@ class ApplePayDomainMerchantUrlTest extends TestCase
$cg->require_billing_address = true;
$cg->require_shipping_address = true;
$cg->update_details = true;
$cg->config = encrypt(json_encode($config));
$cg->setConfig(json_encode($config));
$cg->fees_and_limits = '';
$cg->save();