From 7a141f2631a0c4d4c5cf87964112600d0ff8a8bf Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 26 Apr 2021 15:47:54 +1000 Subject: [PATCH 1/6] cleanup --- app/Http/Controllers/SubdomainController.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Http/Controllers/SubdomainController.php b/app/Http/Controllers/SubdomainController.php index 73733e382c..9606da4618 100644 --- a/app/Http/Controllers/SubdomainController.php +++ b/app/Http/Controllers/SubdomainController.php @@ -28,9 +28,8 @@ class SubdomainController extends BaseController */ public function index() { - $subdomain_exists = MultiDB::findAndSetDbByDomain(request()->input('subdomain')); - if($subdomain_exists) + if( MultiDB::findAndSetDbByDomain(request()->input('subdomain')) ) return response()->json(['message' => 'Domain not available'] , 401); return response()->json(['message' => 'Domain available'], 200); From 1bcfa1b19d5cdb9ece0a49b1c336f368b86f0221 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 27 Apr 2021 09:34:35 +1000 Subject: [PATCH 2/6] Add ability to logout all tokens from company --- app/Http/Controllers/LogoutController.php | 70 +++++++++++++++++++++++ app/Libraries/MultiDB.php | 7 +++ app/Models/Task.php | 5 ++ app/Repositories/TaskRepository.php | 19 ++++++ config/ninja.php | 2 +- routes/api.php | 2 + 6 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/LogoutController.php diff --git a/app/Http/Controllers/LogoutController.php b/app/Http/Controllers/LogoutController.php new file mode 100644 index 0000000000..68bd4d934a --- /dev/null +++ b/app/Http/Controllers/LogoutController.php @@ -0,0 +1,70 @@ +header('X-API-TOKEN')]) + ->company + ->tokens() + ->forceDelete(); + + return response()->json(['message' => 'logged out.'], 200); + } + +} diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 7b69e22389..f2cc257242 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -16,6 +16,7 @@ use App\Models\ClientContact; use App\Models\Company; use App\Models\CompanyToken; use App\Models\User; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; /** @@ -293,10 +294,16 @@ class MultiDB { /* This will set the database connection for the request */ config(['database.default' => $database]); + + DB::purge($database); + DB::reconnect($database); } public static function setDefaultDatabase() { config(['database.default' => config('ninja.db.default')]); + + DB::purge(config('ninja.db.default')); + DB::reconnect(config('ninja.db.default')); } } diff --git a/app/Models/Task.php b/app/Models/Task.php index 3e3d902dcb..b34f9f062a 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -74,6 +74,11 @@ class Task extends BaseModel return $this->belongsTo(Client::class); } + public function status() + { + return $this->belongsTo(TaskStatus::class); + } + public function invoice() { return $this->belongsTo(Invoice::class); diff --git a/app/Repositories/TaskRepository.php b/app/Repositories/TaskRepository.php index 778b695763..45f1cf20b8 100644 --- a/app/Repositories/TaskRepository.php +++ b/app/Repositories/TaskRepository.php @@ -22,6 +22,7 @@ class TaskRepository extends BaseRepository { use GeneratesCounter; + public $new_task = true; /** * Saves the task and its contacts. @@ -33,10 +34,15 @@ class TaskRepository extends BaseRepository */ public function save(array $data, Task $task) : ?Task { + if($task->id) + $this->new_task = false; $task->fill($data); $task->save(); + if($this->new_task && !$task->status_id) + $this->setDefaultStatus($task); + $task->number = empty($task->number) || !array_key_exists('number', $data) ? $this->getNextTaskNumber($task) : $data['number']; if (isset($data['description'])) { @@ -103,6 +109,19 @@ class TaskRepository extends BaseRepository } + private function setDefaultStatus(Task $task) + { + $first_status = $task->company->task_statuses() + ->whereNull('deleted_at') + ->orderBy('id','asc') + ->first(); + + if($first_status) + return $first_status->id; + + return null; + } + /** * Sorts the task status order IF the old status has changed between requests * diff --git a/config/ninja.php b/config/ninja.php index fe0dab5e2a..2963c78e85 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -54,7 +54,7 @@ return [ 'db' => [ 'multi_db_enabled' => env('MULTI_DB_ENABLED', false), - 'default' => env('DB_CONNECTION', 'mysql'), + 'default' => env('DB_CONNECTION', 'db-ninja-01'), ], 'i18n' => [ diff --git a/routes/api.php b/routes/api.php index 1dd0e044e8..2e84a7977c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -90,6 +90,8 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::put('invoices/{invoice}/upload', 'InvoiceController@upload')->name('invoices.upload'); Route::get('invoice/{invitation_key}/download', 'InvoiceController@downloadPdf')->name('invoices.downloadPdf'); Route::post('invoices/bulk', 'InvoiceController@bulk')->name('invoices.bulk'); + + Route::post('logout', 'LogoutController@index')->name('logout'); Route::post('migrate', 'MigrationController@index')->name('migrate.start'); From 48da709d6e2002aa2aa7205876b24bfa325a86b1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 27 Apr 2021 09:44:20 +1000 Subject: [PATCH 3/6] Fixes for broken DBs --- app/Libraries/MultiDB.php | 8 ++++---- config/ninja.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index f2cc257242..91218652a4 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -295,15 +295,15 @@ class MultiDB /* This will set the database connection for the request */ config(['database.default' => $database]); - DB::purge($database); - DB::reconnect($database); + // DB::purge($database); + // DB::reconnect($database); } public static function setDefaultDatabase() { config(['database.default' => config('ninja.db.default')]); - DB::purge(config('ninja.db.default')); - DB::reconnect(config('ninja.db.default')); + // DB::purge(config('ninja.db.default')); + // DB::reconnect(config('ninja.db.default')); } } diff --git a/config/ninja.php b/config/ninja.php index 2963c78e85..fe0dab5e2a 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -54,7 +54,7 @@ return [ 'db' => [ 'multi_db_enabled' => env('MULTI_DB_ENABLED', false), - 'default' => env('DB_CONNECTION', 'db-ninja-01'), + 'default' => env('DB_CONNECTION', 'mysql'), ], 'i18n' => [ From b0f5ca8638798dc174ffdd9ea4a5b34edbd16fa1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 27 Apr 2021 16:03:21 +1000 Subject: [PATCH 4/6] Logout Controller --- app/Http/Controllers/LogoutController.php | 6 ++++-- app/Libraries/MultiDB.php | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/LogoutController.php b/app/Http/Controllers/LogoutController.php index 68bd4d934a..eda0c48d99 100644 --- a/app/Http/Controllers/LogoutController.php +++ b/app/Http/Controllers/LogoutController.php @@ -59,12 +59,14 @@ class LogoutController extends BaseController */ public function index(Request $request) { - CompanyToken::whereRaw('BINARY `token`= ?', [$request->header('X-API-TOKEN')]) + CompanyToken::with('company') + ->whereRaw('BINARY `token`= ?', [$request->header('X-API-TOKEN')]) ->company ->tokens() + ->where('is_system', true) ->forceDelete(); - return response()->json(['message' => 'logged out.'], 200); + return response()->json(['message' => 'All tokens deleted'], 200); } } diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 91218652a4..92132a118b 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -295,6 +295,7 @@ class MultiDB /* This will set the database connection for the request */ config(['database.default' => $database]); + // for some reason this breaks everything _hard_ // DB::purge($database); // DB::reconnect($database); } From d2bb8d6d81029cfac1bfb6e6ef25c0bf603c13e4 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 27 Apr 2021 20:38:34 +1000 Subject: [PATCH 5/6] Minor fixes for impmort --- app/Jobs/Util/Import.php | 23 +++++++++++++++++++---- database/factories/ProductFactory.php | 12 ++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index fdb46be634..a50b64db93 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -192,6 +192,15 @@ class Import implements ShouldQueue $array = json_decode(file_get_contents($this->file_path), 1); $data = $array['data']; + + // disable some functionality here: + // 1. disable update_products + + $update_product_state = $this->company->update_products; + + $this->company->update_products = false; + $this->company->save(); + foreach ($this->available_imports as $import) { if (! array_key_exists($import, $data)) { //throw new ResourceNotAvailableForMigration("Resource {$key} is not available for migration."); @@ -211,9 +220,10 @@ class Import implements ShouldQueue // $this->fixClientBalances(); $check_data = CheckCompanyData::dispatchNow($this->company, md5(time())); - // if($check_data['status'] == 'errors') - // throw new ProcessingMigrationArchiveFailed(implode("\n", $check_data)); - + //reset functionality here + $this->company->update_products = $update_product_state; + $this->company->save(); + try{ Mail::to($this->user->email, $this->user->name()) ->send(new MigrationCompleted($this->company, implode("
",$check_data))); @@ -224,7 +234,12 @@ class Import implements ShouldQueue /*After a migration first some basic jobs to ensure the system is up to date*/ VersionCheck::dispatch(); - CompanySizeCheck::dispatch(); + + //company size check + if ($this->company->invoices()->count() > 1000 || $this->company->products()->count() > 1000 || $this->company->clients()->count() > 1000) { + $company->is_large = true; + $company->save(); + } info('CompletedπŸš€πŸš€πŸš€πŸš€πŸš€ at '.now()); diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php index 12f41ccd03..3ef22ae4ba 100644 --- a/database/factories/ProductFactory.php +++ b/database/factories/ProductFactory.php @@ -35,12 +35,12 @@ class ProductFactory extends Factory 'cost' => $this->faker->numberBetween(1, 1000), 'price' => $this->faker->numberBetween(1, 1000), 'quantity' => $this->faker->numberBetween(1, 100), - 'tax_name1' => 'GST', - 'tax_rate1' => 10, - 'tax_name2' => 'VAT', - 'tax_rate2' => 17.5, - 'tax_name3' => 'THIRDTAX', - 'tax_rate3' => 5, + // 'tax_name1' => 'GST', + // 'tax_rate1' => 10, + // 'tax_name2' => 'VAT', + // 'tax_rate2' => 17.5, + // 'tax_name3' => 'THIRDTAX', + // 'tax_rate3' => 5, 'custom_value1' => $this->faker->text(20), 'custom_value2' => $this->faker->text(20), 'custom_value3' => $this->faker->text(20), From ac3c1f349ea21847075b4735716f94920e450fc7 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 27 Apr 2021 20:39:10 +1000 Subject: [PATCH 6/6] Minor fixes for self update --- app/Http/Controllers/SelfUpdateController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/SelfUpdateController.php b/app/Http/Controllers/SelfUpdateController.php index ae46dbe8bc..70e3e03698 100644 --- a/app/Http/Controllers/SelfUpdateController.php +++ b/app/Http/Controllers/SelfUpdateController.php @@ -66,7 +66,7 @@ class SelfUpdateController extends BaseController throw new FilePermissionsFailure('Cannot update system because files are not writable!'); // Check if new version is available - if($updater->source()->isNewVersionAvailable()) { + //if($updater->source()->isNewVersionAvailable()) { // Get the new version available $versionAvailable = $updater->source()->getVersionAvailable(); @@ -76,7 +76,7 @@ class SelfUpdateController extends BaseController $updater->source()->update($release); - } + //} $cacheCompiled = base_path('bootstrap/cache/compiled.php'); if (file_exists($cacheCompiled)) { unlink ($cacheCompiled); }