From 37f295d49e9d02956483a1fdb6c3d9e3dd525385 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 8 Mar 2020 16:59:06 +1100 Subject: [PATCH] Improve notification performance (#3452) * Improve notification performance * Cascade deletes * Fixes for company deletes * Fixes for formatting * todos for company * Set currency id on payment * Minor fixes for payment controller --- app/Console/Commands/CreateTestData.php | 1 + .../TestData/CreateTestInvoiceJob.php | 1 + app/Http/Controllers/CompanyController.php | 41 ++++++++++++++++--- app/Http/Controllers/PreviewController.php | 2 - app/Http/Controllers/SelfUpdateController.php | 9 ++-- app/Jobs/Util/VersionCheck.php | 11 ++--- app/Notifications/Ninja/NewAccountCreated.php | 6 ++- app/PaymentDrivers/BasePaymentDriver.php | 1 + app/Services/Invoice/MarkPaid.php | 1 + app/Services/Payment/PaymentService.php | 1 + app/Transformers/AccountTransformer.php | 1 + config/ninja.php | 2 +- .../2014_10_13_000000_create_users_table.php | 1 + 13 files changed, 55 insertions(+), 23 deletions(-) diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 374416f590ce..c3ba4525ecc1 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -488,6 +488,7 @@ class CreateTestData extends Command $payment->type_id = PaymentType::CREDIT_CARD_OTHER; $payment->status_id = Payment::STATUS_COMPLETED; $payment->number = $client->getNextPaymentNumber($client); + $payment->currency_id = 1; $payment->save(); $payment->invoices()->save($invoice); diff --git a/app/Console/Commands/TestData/CreateTestInvoiceJob.php b/app/Console/Commands/TestData/CreateTestInvoiceJob.php index 3fa3c4f6dca3..02a8ce9634e6 100644 --- a/app/Console/Commands/TestData/CreateTestInvoiceJob.php +++ b/app/Console/Commands/TestData/CreateTestInvoiceJob.php @@ -108,6 +108,7 @@ class CreateTestInvoiceJob implements ShouldQueue $payment->type_id = PaymentType::CREDIT_CARD_OTHER; $payment->status_id = Payment::STATUS_COMPLETED; $payment->number = $this->client->getNextPaymentNumber($this->client); + $payment->currency_id = 1; $payment->save(); $payment->invoices()->save($invoice); diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 0c0b4d5b524f..f3bc71a03287 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -459,12 +459,43 @@ class CompanyController extends BaseController */ public function destroy(DestroyCompanyRequest $request, Company $company) { + $company_count = $company->account->companies->count(); - $company->delete(); - - //@TODO if last company, delete the account also. - - return response()->json([], 200); + if($company_count == 1){ + + $company->company_users->each(function ($company_user) { + + $company_user->user->forceDelete(); + + }); + + $company->account->delete(); + + } + else { + + $account = $company->account; + $company_id = $company->id; + $company->delete(); + + //If we are deleting the default companies, we'll need to make a new company the default. + if($account->default_company == $company_id){ + + $account->fresh(); + $account->default_company = $account->companies->first()->id(); + $account->save(); + + } + + + } + + //@todo delete documents also!! + + //@todo in the hosted version deleting the last + //account will trigger an account refund. + + return response()->json(['message' => 'success'], 200); } } diff --git a/app/Http/Controllers/PreviewController.php b/app/Http/Controllers/PreviewController.php index 41fdd50b8906..1b4145efba88 100644 --- a/app/Http/Controllers/PreviewController.php +++ b/app/Http/Controllers/PreviewController.php @@ -148,8 +148,6 @@ class PreviewController extends BaseController $invoice->setRelation('company', auth()->user()->company()); $invoice->load('client'); -\Log::error(print_r(request()->input('body'),1)); - $design_object = json_decode(request()->input('body')); if(!is_object($design_object)) diff --git a/app/Http/Controllers/SelfUpdateController.php b/app/Http/Controllers/SelfUpdateController.php index 07fd8632bd8b..f839ab56a237 100644 --- a/app/Http/Controllers/SelfUpdateController.php +++ b/app/Http/Controllers/SelfUpdateController.php @@ -13,6 +13,7 @@ namespace App\Http\Controllers; use Codedge\Updater\UpdaterManager; use Illuminate\Foundation\Bus\DispatchesJobs; +use Illuminate\Support\Facades\Storage; class SelfUpdateController extends BaseController { @@ -61,18 +62,14 @@ class SelfUpdateController extends BaseController $res = $updater->update(); return response()->json(['message'=>$res], 200); + } public function checkVersion(UpdaterManager $updater) { - $file_version = storage_path().'app/local_version.txt'; - if(file_exists($file_version)){ - return response()->json(['message' => file_get_contents($file_version)]); - }else{ - return response()->json(['message' => '0.0.0']); - } } + } \ No newline at end of file diff --git a/app/Jobs/Util/VersionCheck.php b/app/Jobs/Util/VersionCheck.php index 8ba5575e8fcd..7838905b5615 100644 --- a/app/Jobs/Util/VersionCheck.php +++ b/app/Jobs/Util/VersionCheck.php @@ -2,7 +2,7 @@ namespace App\Jobs\Util; -use App\Utils\Traits\BulkOptions; +use App\Models\Account; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -14,7 +14,6 @@ class VersionCheck implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - public function __construct() { @@ -27,15 +26,11 @@ class VersionCheck implements ShouldQueue */ public function handle() { - //$local_version = storage_path() . '/app/local_version.txt'; - $local_version = 'local_version.txt'; $version_file = file_get_contents(config('ninja.version_url')); - if(Storage::exists($local_version)); - Storage::delete($local_version); - - Storage::disk('local')->put('local_version.txt', $version_file); + if($version_file) + Account::whereNotNull('id')->update(['latest_version' => $version_file]); } diff --git a/app/Notifications/Ninja/NewAccountCreated.php b/app/Notifications/Ninja/NewAccountCreated.php index 37101ae81762..7b0bbb6438bc 100644 --- a/app/Notifications/Ninja/NewAccountCreated.php +++ b/app/Notifications/Ninja/NewAccountCreated.php @@ -5,13 +5,17 @@ namespace App\Notifications\Ninja; use App\Mail\Signup\NewSignup; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\SerializesModels; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; class NewAccountCreated extends Notification implements ShouldQueue { - use Queueable; + + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new notification instance. diff --git a/app/PaymentDrivers/BasePaymentDriver.php b/app/PaymentDrivers/BasePaymentDriver.php index 0253fc1f9dbf..f838913f0995 100644 --- a/app/PaymentDrivers/BasePaymentDriver.php +++ b/app/PaymentDrivers/BasePaymentDriver.php @@ -267,6 +267,7 @@ class BasePaymentDriver $payment->client_id = $this->client->id; $payment->company_gateway_id = $this->company_gateway->id; $payment->status_id = Payment::STATUS_COMPLETED; + $payment->currency_id = $this->client->getSetting('currency_id'); $payment->date = Carbon::now(); return $payment; diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index 6cf1230faaec..33d58483e78b 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -50,6 +50,7 @@ class MarkPaid extends AbstractService $payment->status_id = Payment::STATUS_COMPLETED; $payment->client_id = $this->invoice->client_id; $payment->transaction_reference = ctrans('texts.manual_entry'); + $payment->currency_id = $this->invoice->client->getSetting('currency_id'); /* Create a payment relationship to the invoice entity */ $payment->save(); diff --git a/app/Services/Payment/PaymentService.php b/app/Services/Payment/PaymentService.php index 5945c971ac92..c37832ad5305 100644 --- a/app/Services/Payment/PaymentService.php +++ b/app/Services/Payment/PaymentService.php @@ -35,6 +35,7 @@ class PaymentService $payment->status_id = Payment::STATUS_COMPLETED; $payment->client_id = $invoice->client_id; $payment->transaction_reference = ctrans('texts.manual_entry'); + $payment->currency_id = $invoice->client->getSetting('currency_id'); /* Create a payment relationship to the invoice entity */ $payment->save(); diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index 652da62cc6ec..f2ea3555bb84 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -60,6 +60,7 @@ class AccountTransformer extends EntityTransformer 'id' => (string)$this->encodePrimaryKey($account->id), 'default_url' => config('ninja.site_url'), 'plan' => $account->getPlan(), + 'latest_version' => (string)$account->latest_version, 'updated_at' => (int)$account->updated_at, 'archived_at' => (int)$account->deleted_at, ]; diff --git a/config/ninja.php b/config/ninja.php index 6574615fd0be..6de3b7f3287c 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -7,7 +7,7 @@ return [ 'production' => env('NINJA_PROD', false), 'license' => env('NINJA_LICENSE', ''), 'app_name' => env('APP_NAME'), - 'version_url' => 'https://github.com/invoiceninja/invoiceninja/tree/v2/VERSION.txt', + 'version_url' => 'https://raw.githubusercontent.com/invoiceninja/invoiceninja/v2/VERSION.txt', 'site_url' => env('APP_URL', ''), 'app_domain' => env('APP_DOMAIN', 'invoicing.co'), 'app_version' => '0.0.1', diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 1dc2282536a4..47b969269625 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -127,6 +127,7 @@ class CreateUsersTable extends Migration $table->string('utm_campaign')->nullable(); $table->string('utm_term')->nullable(); $table->string('utm_content')->nullable(); + $table->string('latest_version')->default('0.0.0'); $table->float('discount')->default(0); $table->date('discount_expires')->nullable();