diff --git a/app/Jobs/PurgeAccountData.php b/app/Jobs/PurgeAccountData.php index c1f289e2cae3..c06425ea8db4 100644 --- a/app/Jobs/PurgeAccountData.php +++ b/app/Jobs/PurgeAccountData.php @@ -4,6 +4,7 @@ namespace App\Jobs; use App\Jobs\Job; use App\Models\Document; +use App\Models\LookupAccount; use Auth; use DB; use Exception; @@ -57,5 +58,16 @@ class PurgeAccountData extends Job $account->quote_number_counter = 1; $account->client_number_counter = 1; $account->save(); + + if (env('MULTI_DB_ENABLED')) { + $current = config('database.default'); + config(['database.default' => DB_NINJA_LOOKUP]); + + $lookupAccount = LookupAccount::whereAccountKey($account->account_key)->first(); + DB::table('lookup_contacts')->where('lookup_account_id', '=', $lookupAccount->id)->delete(); + DB::table('lookup_invitations')->where('lookup_account_id', '=', $lookupAccount->id)->delete(); + + config(['database.default' => $current]); + } } } diff --git a/app/Models/Account.php b/app/Models/Account.php index 5e830735c943..b5b8b4b2d9c6 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -1661,7 +1661,6 @@ Account::creating(function ($account) LookupAccount::createAccount($account->account_key, $account->company_id); }); - Account::updated(function ($account) { // prevent firing event if the invoice/quote counter was changed // TODO: remove once counters are moved to separate table @@ -1672,3 +1671,10 @@ Account::updated(function ($account) { Event::fire(new UserSettingsChanged()); }); + +Account::deleted(function ($account) +{ + LookupAccount::deleteWhere([ + 'account_key' => $account->account_key + ]); +}); diff --git a/app/Models/AccountToken.php b/app/Models/AccountToken.php index baf3bd9477c7..0196c64f47f4 100644 --- a/app/Models/AccountToken.php +++ b/app/Models/AccountToken.php @@ -47,3 +47,10 @@ AccountToken::creating(function ($token) 'token' => $token->token, ]); }); + +AccountToken::deleted(function ($token) +{ + LookupAccountToken::deleteWhere([ + 'token' => $token->token + ]); +}); diff --git a/app/Models/Company.php b/app/Models/Company.php index cfa121d556f4..ce580d884018 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -169,3 +169,17 @@ class Company extends Eloquent return false; } } + +Company::deleted(function ($company) +{ + if (! env('MULTI_DB_ENABLED')) { + return; + } + + $server = \App\Models\DbServer::whereName(config('database.default'))->firstOrFail(); + + LookupCompany::deleteWhere([ + 'company_id' => $company->id, + 'db_server_id' => $server->id, + ]); +}); diff --git a/app/Models/Contact.php b/app/Models/Contact.php index fe3547f99742..adc0fa752beb 100644 --- a/app/Models/Contact.php +++ b/app/Models/Contact.php @@ -173,3 +173,10 @@ Contact::creating(function ($contact) 'contact_key' => $contact->contact_key, ]); }); + +Contact::deleted(function ($contact) +{ + LookupContact::deleteWhere([ + 'contact_key' => $contact->contact_key, + ]); +}); diff --git a/app/Models/Invitation.php b/app/Models/Invitation.php index a33785af452d..1447ff4da9f1 100644 --- a/app/Models/Invitation.php +++ b/app/Models/Invitation.php @@ -170,3 +170,10 @@ Invitation::creating(function ($invitation) 'invitation_key' => $invitation->invitation_key, ]); }); + +Invitation::deleted(function ($invitation) +{ + LookupInvitation::deleteWhere([ + 'invitation_key' => $invitation->invitation_key, + ]); +}); diff --git a/app/Models/LookupModel.php b/app/Models/LookupModel.php index 0e8a634bb636..a43577482299 100644 --- a/app/Models/LookupModel.php +++ b/app/Models/LookupModel.php @@ -41,6 +41,21 @@ class LookupModel extends Eloquent config(['database.default' => $current]); } + public static function deleteWhere($where) + { + if (! env('MULTI_DB_ENABLED')) { + return; + } + + $current = config('database.default'); + config(['database.default' => DB_NINJA_LOOKUP]); + + static::where($where)->delete(); + + config(['database.default' => $current]); + + } + public static function setServerByField($field, $value) { if (! env('MULTI_DB_ENABLED')) { @@ -60,7 +75,7 @@ class LookupModel extends Eloquent $current = config('database.default'); config(['database.default' => DB_NINJA_LOOKUP]); - if ($lookupUser = static::where($field, '=', $value)->first()) { + if ($value && $lookupUser = static::where($field, '=', $value)->first()) { $entity = new $className(); $server = $lookupUser->getDbServer(); static::setDbServer($server); diff --git a/app/Models/User.php b/app/Models/User.php index b9edafa6da3b..89506050c805 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -433,3 +433,14 @@ User::updating(function ($user) { User::updated(function ($user) { User::onUpdatedUser($user); }); + +User::deleted(function ($user) +{ + if (! $user->email) { + return; + } + + LookupUser::deleteWhere([ + 'email' => $user->email + ]); +}); diff --git a/database/migrations/2017_04_30_174702_add_multiple_database_support.php b/database/migrations/2017_04_30_174702_add_multiple_database_support.php index a0fbbeb6fe61..3043cbf60a1e 100644 --- a/database/migrations/2017_04_30_174702_add_multiple_database_support.php +++ b/database/migrations/2017_04_30_174702_add_multiple_database_support.php @@ -47,7 +47,10 @@ class AddMultipleDatabaseSupport extends Migration }); Schema::rename('lookup_tokens', 'lookup_account_tokens'); - } + + DB::table('db_servers')->insert( + ['name' => 'db-server-1'] + ); /** * Reverse the migrations.