Support updating lookup data

This commit is contained in:
Hillel Coren 2017-05-25 14:52:29 +03:00
parent 54726d3565
commit 7510ddd3b9

View File

@ -21,7 +21,7 @@ class InitLookup extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'ninja:init-lookup {--truncate=} {--validate=} {--company_id=} {--page_size=100} {--database=db-ninja-1}'; protected $signature = 'ninja:init-lookup {--truncate=} {--validate=} {--update=} {--company_id=} {--page_size=100} {--database=db-ninja-1}';
/** /**
* The console command description. * The console command description.
@ -106,15 +106,20 @@ class InitLookup extends Command
config(['database.default' => DB_NINJA_LOOKUP]); config(['database.default' => DB_NINJA_LOOKUP]);
$validate = $this->option('validate');
$update = $this->option('update');
foreach ($data as $companyId => $company) { foreach ($data as $companyId => $company) {
if ($this->option('validate')) { $lookupCompany = false;
if ($validate || $update) {
$lookupCompany = LookupCompany::whereDbServerId($dbServerId)->whereCompanyId($companyId)->first(); $lookupCompany = LookupCompany::whereDbServerId($dbServerId)->whereCompanyId($companyId)->first();
if (! $lookupCompany) { }
$this->logError("LookupCompany - dbServerId: {$dbServerId}, companyId: {$companyId} | Not found!"); if ($validate && ! $lookupCompany) {
continue; $this->logError("LookupCompany - dbServerId: {$dbServerId}, companyId: {$companyId} | Not found!");
} continue;
} else { }
if (! $lookupCompany) {
$lookupCompany = LookupCompany::create([ $lookupCompany = LookupCompany::create([
'db_server_id' => $dbServerId, 'db_server_id' => $dbServerId,
'company_id' => $companyId, 'company_id' => $companyId,
@ -122,13 +127,16 @@ class InitLookup extends Command
} }
foreach ($company as $accountKey => $account) { foreach ($company as $accountKey => $account) {
if ($this->option('validate')) {
$lookupAccount = false;
if ($validate || $update) {
$lookupAccount = LookupAccount::whereLookupCompanyId($lookupCompany->id)->whereAccountKey($accountKey)->first(); $lookupAccount = LookupAccount::whereLookupCompanyId($lookupCompany->id)->whereAccountKey($accountKey)->first();
if (! $lookupAccount) { }
$this->logError("LookupAccount - lookupCompanyId: {$lookupCompany->id}, accountKey {$accountKey} | Not found!"); if ($validate && ! $lookupAccount) {
continue; $this->logError("LookupAccount - lookupCompanyId: {$lookupCompany->id}, accountKey {$accountKey} | Not found!");
} continue;
} else { }
if (! $lookupAccount) {
$lookupAccount = LookupAccount::create([ $lookupAccount = LookupAccount::create([
'lookup_company_id' => $lookupCompany->id, 'lookup_company_id' => $lookupCompany->id,
'account_key' => $accountKey 'account_key' => $accountKey
@ -136,17 +144,27 @@ class InitLookup extends Command
} }
foreach ($account['users'] as $user) { foreach ($account['users'] as $user) {
if ($this->option('validate')) { $lookupUser = false;
if ($validate || $update) {
$lookupUser = LookupUser::whereLookupAccountId($lookupAccount->id)->whereUserId($user['user_id'])->first(); $lookupUser = LookupUser::whereLookupAccountId($lookupAccount->id)->whereUserId($user['user_id'])->first();
}
if ($validate) {
if (! $lookupUser) { if (! $lookupUser) {
$this->logError("LookupUser - lookupAccountId: {$lookupAccount->id}, userId: {$user['user_id']} | Not found!"); $this->logError("LookupUser - lookupAccountId: {$lookupAccount->id}, userId: {$user['user_id']} | Not found!");
continue; continue;
} } elseif ($user['email'] != $lookupUser->email || $user['oauth_user_key'] != $lookupUser->oauth_user_key || $user['referral_code'] != $lookupUser->referral_code) {
if ($user['email'] != $lookupUser->email || $user['oauth_user_key'] != $lookupUser->oauth_user_key || $user['referral_code'] != $lookupUser->referral_code) {
$this->logError("LookupUser - lookupAccountId: {$lookupAccount->id}, userId: {$user['user_id']} | Out of date!"); $this->logError("LookupUser - lookupAccountId: {$lookupAccount->id}, userId: {$user['user_id']} | Out of date!");
continue; continue;
} }
} else { }
if ($update && $lookupUser) {
if ($user['email'] != $lookupUser->email || $user['oauth_user_key'] != $lookupUser->oauth_user_key || $user['referral_code'] != $lookupUser->referral_code) {
$lookupUser->email = $user['email'];
$lookupUser->oauth_user_key = $user['oauth_user_key'];
$lookupUser->referral_code = $user['referral_code'];
$lookupUser->save();
}
} elseif (! $lookupUser) {
LookupUser::create([ LookupUser::create([
'lookup_account_id' => $lookupAccount->id, 'lookup_account_id' => $lookupAccount->id,
'email' => $user['email'] ?: null, 'email' => $user['email'] ?: null,
@ -158,13 +176,15 @@ class InitLookup extends Command
} }
foreach ($account['contacts'] as $contact) { foreach ($account['contacts'] as $contact) {
if ($this->option('validate')) { $lookupContact = false;
if ($validate || $update) {
$lookupContact = LookupContact::whereLookupAccountId($lookupAccount->id)->whereContactKey($contact['contact_key'])->first(); $lookupContact = LookupContact::whereLookupAccountId($lookupAccount->id)->whereContactKey($contact['contact_key'])->first();
if (! $lookupContact) { }
$this->logError("LookupContact - lookupAccountId: {$lookupAccount->id}, contactKey: {$contact['contact_key']} | Not found!"); if ($validate && ! $lookupContact) {
continue; $this->logError("LookupContact - lookupAccountId: {$lookupAccount->id}, contactKey: {$contact['contact_key']} | Not found!");
} continue;
} else { }
if (! $lookupContact) {
LookupContact::create([ LookupContact::create([
'lookup_account_id' => $lookupAccount->id, 'lookup_account_id' => $lookupAccount->id,
'contact_key' => $contact['contact_key'], 'contact_key' => $contact['contact_key'],
@ -173,13 +193,25 @@ class InitLookup extends Command
} }
foreach ($account['invitations'] as $invitation) { foreach ($account['invitations'] as $invitation) {
if ($this->option('validate')) { $lookupInvitation = false;
if ($validate || $update) {
$lookupInvitation = LookupInvitation::whereLookupAccountId($lookupAccount->id)->whereInvitationKey($invitation['invitation_key'])->first(); $lookupInvitation = LookupInvitation::whereLookupAccountId($lookupAccount->id)->whereInvitationKey($invitation['invitation_key'])->first();
}
if ($validate) {
if (! $lookupInvitation) { if (! $lookupInvitation) {
$this->logError("LookupInvitation - lookupAccountId: {$lookupAccount->id}, invitationKey: {$invitation['invitation_key']} | Not found!"); $this->logError("LookupInvitation - lookupAccountId: {$lookupAccount->id}, invitationKey: {$invitation['invitation_key']} | Not found!");
continue; continue;
} elseif ($invitation['message_id'] && $lookupInvitation->message_id != $invitation['message_id']) {
$this->logError("LookupInvitation - lookupAccountId: {$lookupAccount->id}, invitationKey: {$invitation['invitation_key']} | Not the same!");
continue;
} }
} else { }
if ($update && $lookupInvitation) {
if ($invitation['message_id'] && $lookupInvitation->message_id != $invitation['message_id']) {
$lookupInvitation->message_id = $invitation['message_id'];
$lookupInvitation->save();
}
} elseif (! $lookupInvitation) {
LookupInvitation::create([ LookupInvitation::create([
'lookup_account_id' => $lookupAccount->id, 'lookup_account_id' => $lookupAccount->id,
'invitation_key' => $invitation['invitation_key'], 'invitation_key' => $invitation['invitation_key'],
@ -189,13 +221,15 @@ class InitLookup extends Command
} }
foreach ($account['tokens'] as $token) { foreach ($account['tokens'] as $token) {
if ($this->option('validate')) { $lookupToken = false;
if ($validate || $update) {
$lookupToken = LookupAccountToken::whereLookupAccountId($lookupAccount->id)->whereToken($token['token'])->first(); $lookupToken = LookupAccountToken::whereLookupAccountId($lookupAccount->id)->whereToken($token['token'])->first();
if (! $lookupToken) { }
$this->logError("LookupAccountToken - lookupAccountId: {$lookupAccount->id}, token: {$token['token']} | Not found!"); if ($validate && ! $lookupToken) {
continue; $this->logError("LookupAccountToken - lookupAccountId: {$lookupAccount->id}, token: {$token['token']} | Not found!");
} continue;
} else { }
if (! $lookupToken) {
LookupAccountToken::create([ LookupAccountToken::create([
'lookup_account_id' => $lookupAccount->id, 'lookup_account_id' => $lookupAccount->id,
'token' => $token['token'], 'token' => $token['token'],