1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Console/Commands/InitLookup.php

381 lines
14 KiB
PHP
Raw Normal View History

2017-04-30 18:09:13 +02:00
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
2017-05-02 20:55:36 +02:00
use Mail;
use Exception;
2017-04-30 18:09:13 +02:00
use App\Models\DbServer;
2017-04-30 19:08:49 +02:00
use App\Models\LookupCompany;
use App\Models\LookupAccount;
use App\Models\LookupUser;
use App\Models\LookupContact;
2017-05-01 09:03:42 +02:00
use App\Models\LookupAccountToken;
2017-04-30 19:08:49 +02:00
use App\Models\LookupInvitation;
2017-04-30 18:09:13 +02:00
class InitLookup extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2017-11-15 13:51:02 +01:00
protected $signature = 'ninja:init-lookup {--truncate=} {--subdomain} {--validate=} {--update=} {--company_id=} {--page_size=100} {--database=db-ninja-1}';
2017-04-30 18:09:13 +02:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Initialize lookup tables';
2017-05-02 11:52:22 +02:00
protected $log = '';
protected $isValid = true;
2017-04-30 18:09:13 +02:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2017-05-02 11:52:22 +02:00
$this->logMessage('Running InitLookup...');
2017-04-30 18:09:13 +02:00
2017-04-30 20:44:52 +02:00
config(['database.default' => DB_NINJA_LOOKUP]);
2017-04-30 18:09:13 +02:00
2017-05-03 20:00:42 +02:00
$database = $this->option('database');
$dbServer = DbServer::whereName($database)->first();
2017-04-30 19:08:49 +02:00
2017-11-15 13:51:02 +01:00
if ($this->option('subdomain')) {
$this->logMessage('Updating subdomains...');
$this->popuplateSubdomains();
} else if ($this->option('truncate')) {
$this->logMessage('Truncating data...');
2017-04-30 20:44:52 +02:00
$this->truncateTables();
2017-05-02 11:52:22 +02:00
} else {
config(['database.default' => $this->option('database')]);
2017-04-30 20:44:52 +02:00
2017-05-02 11:52:22 +02:00
$count = DB::table('companies')
->where('id', '>=', $this->option('company_id') ?: 1)
->count();
2017-04-30 19:08:49 +02:00
2017-05-02 11:52:22 +02:00
for ($i=0; $i<$count; $i += (int) $this->option('page_size')) {
$this->initCompanies($dbServer->id, $i);
}
2017-04-30 19:08:49 +02:00
}
2017-05-02 11:52:22 +02:00
2017-06-19 09:55:32 +02:00
$this->logMessage('Results: ' . ($this->isValid ? RESULT_SUCCESS : RESULT_FAILURE));
2017-05-02 20:55:36 +02:00
2017-05-02 21:50:45 +02:00
if ($this->option('validate')) {
if ($errorEmail = env('ERROR_EMAIL')) {
Mail::raw($this->log, function ($message) use ($errorEmail, $database) {
$message->to($errorEmail)
->from(CONTACT_EMAIL)
->subject("Check-Lookups [{$database}]: " . strtoupper($this->isValid ? RESULT_SUCCESS : RESULT_FAILURE));
});
} elseif (! $this->isValid) {
throw new Exception('Check lookups failed!!');
}
2017-05-02 20:55:36 +02:00
}
2017-04-30 19:08:49 +02:00
}
2017-11-15 13:51:02 +01:00
private function popuplateSubdomains()
{
$data = [];
config(['database.default' => $this->option('database')]);
$accounts = DB::table('accounts')
->orderBy('id')
->where('subdomain', '!=', '')
->get(['account_key', 'subdomain']);
foreach ($accounts as $account) {
$data[$account->account_key] = $account->subdomain;
}
config(['database.default' => DB_NINJA_LOOKUP]);
$validate = $this->option('validate');
$update = $this->option('update');
foreach ($data as $accountKey => $subdomain) {
LookupAccount::whereAccountKey($accountKey)->update(['subdomain' => $subdomain]);
}
}
2017-04-30 20:44:52 +02:00
private function initCompanies($dbServerId, $offset = 0)
2017-04-30 19:08:49 +02:00
{
2017-04-30 20:44:52 +02:00
$data = [];
2017-04-30 19:08:49 +02:00
2017-05-02 11:52:22 +02:00
config(['database.default' => $this->option('database')]);
2017-04-30 19:08:49 +02:00
2017-04-30 20:44:52 +02:00
$companies = DB::table('companies')
->offset($offset)
->limit((int) $this->option('page_size'))
2017-04-30 20:44:52 +02:00
->orderBy('id')
->where('id', '>=', $this->option('company_id') ?: 1)
->get(['id']);
2017-04-30 19:08:49 +02:00
foreach ($companies as $company) {
2017-04-30 20:44:52 +02:00
$data[$company->id] = $this->parseCompany($company->id);
}
config(['database.default' => DB_NINJA_LOOKUP]);
2017-05-25 13:52:29 +02:00
$validate = $this->option('validate');
$update = $this->option('update');
2017-04-30 20:44:52 +02:00
foreach ($data as $companyId => $company) {
2017-05-25 13:52:29 +02:00
$lookupCompany = false;
if ($validate || $update) {
2017-05-02 11:52:22 +02:00
$lookupCompany = LookupCompany::whereDbServerId($dbServerId)->whereCompanyId($companyId)->first();
2017-05-25 13:52:29 +02:00
}
if ($validate && ! $lookupCompany) {
$this->logError("LookupCompany - dbServerId: {$dbServerId}, companyId: {$companyId} | Not found!");
continue;
}
if (! $lookupCompany) {
2017-05-02 11:52:22 +02:00
$lookupCompany = LookupCompany::create([
'db_server_id' => $dbServerId,
'company_id' => $companyId,
]);
}
2017-04-30 20:44:52 +02:00
foreach ($company as $accountKey => $account) {
2017-05-25 13:52:29 +02:00
$lookupAccount = false;
if ($validate || $update) {
2017-05-02 11:52:22 +02:00
$lookupAccount = LookupAccount::whereLookupCompanyId($lookupCompany->id)->whereAccountKey($accountKey)->first();
2017-05-25 13:52:29 +02:00
}
if ($validate && ! $lookupAccount) {
$this->logError("LookupAccount - lookupCompanyId: {$lookupCompany->id}, accountKey {$accountKey} | Not found!");
continue;
}
if (! $lookupAccount) {
2017-05-02 11:52:22 +02:00
$lookupAccount = LookupAccount::create([
'lookup_company_id' => $lookupCompany->id,
'account_key' => $accountKey
2017-04-30 20:44:52 +02:00
]);
}
2017-05-02 11:52:22 +02:00
foreach ($account['users'] as $user) {
2017-05-25 13:52:29 +02:00
$lookupUser = false;
if ($validate || $update) {
2017-05-02 11:52:22 +02:00
$lookupUser = LookupUser::whereLookupAccountId($lookupAccount->id)->whereUserId($user['user_id'])->first();
2017-05-25 13:52:29 +02:00
}
if ($validate) {
2017-05-02 11:52:22 +02:00
if (! $lookupUser) {
$this->logError("LookupUser - lookupAccountId: {$lookupAccount->id}, userId: {$user['user_id']} | Not found!");
continue;
2017-05-25 13:52:29 +02:00
} elseif ($user['email'] != $lookupUser->email || $user['oauth_user_key'] != $lookupUser->oauth_user_key || $user['referral_code'] != $lookupUser->referral_code) {
2017-05-14 11:11:38 +02:00
$this->logError("LookupUser - lookupAccountId: {$lookupAccount->id}, userId: {$user['user_id']} | Out of date!");
continue;
}
2017-05-25 13:52:29 +02:00
}
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) {
2017-05-02 11:52:22 +02:00
LookupUser::create([
'lookup_account_id' => $lookupAccount->id,
2017-05-03 18:26:03 +02:00
'email' => $user['email'] ?: null,
2017-05-02 11:52:22 +02:00
'user_id' => $user['user_id'],
2017-05-10 17:18:48 +02:00
'oauth_user_key' => $user['oauth_user_key'],
2017-05-14 11:11:38 +02:00
'referral_code' => $user['referral_code'],
2017-05-02 11:52:22 +02:00
]);
}
}
2017-04-30 20:44:52 +02:00
foreach ($account['contacts'] as $contact) {
2017-05-25 13:52:29 +02:00
$lookupContact = false;
if ($validate || $update) {
2017-05-02 11:52:22 +02:00
$lookupContact = LookupContact::whereLookupAccountId($lookupAccount->id)->whereContactKey($contact['contact_key'])->first();
2017-05-25 13:52:29 +02:00
}
if ($validate && ! $lookupContact) {
$this->logError("LookupContact - lookupAccountId: {$lookupAccount->id}, contactKey: {$contact['contact_key']} | Not found!");
continue;
}
if (! $lookupContact) {
2017-05-02 11:52:22 +02:00
LookupContact::create([
'lookup_account_id' => $lookupAccount->id,
'contact_key' => $contact['contact_key'],
]);
}
2017-04-30 20:44:52 +02:00
}
2017-05-02 11:52:22 +02:00
2017-04-30 20:44:52 +02:00
foreach ($account['invitations'] as $invitation) {
2017-05-25 13:52:29 +02:00
$lookupInvitation = false;
if ($validate || $update) {
2017-05-02 11:52:22 +02:00
$lookupInvitation = LookupInvitation::whereLookupAccountId($lookupAccount->id)->whereInvitationKey($invitation['invitation_key'])->first();
2017-05-25 13:52:29 +02:00
}
if ($validate) {
2017-05-02 11:52:22 +02:00
if (! $lookupInvitation) {
$this->logError("LookupInvitation - lookupAccountId: {$lookupAccount->id}, invitationKey: {$invitation['invitation_key']} | Not found!");
continue;
2017-05-25 13:52:29 +02:00
} elseif ($invitation['message_id'] && $lookupInvitation->message_id != $invitation['message_id']) {
$this->logError("LookupInvitation - lookupAccountId: {$lookupAccount->id}, invitationKey: {$invitation['invitation_key']} | Not the same!");
continue;
2017-05-02 11:52:22 +02:00
}
2017-05-25 13:52:29 +02:00
}
if ($update && $lookupInvitation) {
if ($invitation['message_id'] && $lookupInvitation->message_id != $invitation['message_id']) {
$lookupInvitation->message_id = $invitation['message_id'];
$lookupInvitation->save();
}
} elseif (! $lookupInvitation) {
2017-05-02 11:52:22 +02:00
LookupInvitation::create([
'lookup_account_id' => $lookupAccount->id,
'invitation_key' => $invitation['invitation_key'],
'message_id' => $invitation['message_id'] ?: null,
]);
}
2017-04-30 20:44:52 +02:00
}
2017-05-02 11:52:22 +02:00
2017-04-30 20:44:52 +02:00
foreach ($account['tokens'] as $token) {
2017-05-25 13:52:29 +02:00
$lookupToken = false;
if ($validate || $update) {
2017-05-02 11:52:22 +02:00
$lookupToken = LookupAccountToken::whereLookupAccountId($lookupAccount->id)->whereToken($token['token'])->first();
2017-05-25 13:52:29 +02:00
}
if ($validate && ! $lookupToken) {
$this->logError("LookupAccountToken - lookupAccountId: {$lookupAccount->id}, token: {$token['token']} | Not found!");
continue;
}
if (! $lookupToken) {
2017-05-02 11:52:22 +02:00
LookupAccountToken::create([
'lookup_account_id' => $lookupAccount->id,
'token' => $token['token'],
]);
}
2017-04-30 20:44:52 +02:00
}
}
2017-04-30 19:08:49 +02:00
}
}
2017-04-30 20:44:52 +02:00
private function parseCompany($companyId)
2017-04-30 19:08:49 +02:00
{
$data = [];
2017-05-02 11:52:22 +02:00
config(['database.default' => $this->option('database')]);
2017-04-30 19:08:49 +02:00
2017-05-14 11:11:38 +02:00
$accounts = DB::table('accounts')->whereCompanyId($companyId)->orderBy('id')->get([
'id', 'account_key'
]);
2017-04-30 19:08:49 +02:00
foreach ($accounts as $account) {
2017-04-30 20:44:52 +02:00
$data[$account->account_key] = $this->parseAccount($account->id);
2017-04-30 19:08:49 +02:00
}
2017-04-30 20:44:52 +02:00
return $data;
2017-04-30 19:08:49 +02:00
}
private function parseAccount($accountId)
{
$data = [
'users' => [],
'contacts' => [],
'invitations' => [],
'tokens' => [],
];
2017-05-14 11:11:38 +02:00
$users = DB::table('users')->whereAccountId($accountId)->orderBy('id')->get([
'email',
'id',
'oauth_user_id',
'oauth_provider_id',
'referral_code',
]);
2017-04-30 19:08:49 +02:00
foreach ($users as $user) {
2017-04-30 21:29:15 +02:00
$data['users'][] = [
'email' => $user->email,
'user_id' => $user->id,
2017-05-10 17:18:48 +02:00
'oauth_user_key' => ($user->oauth_provider_id && $user->oauth_user_id) ? ($user->oauth_provider_id . '-' . $user->oauth_user_id) : null,
2017-05-14 11:11:38 +02:00
'referral_code' => $user->referral_code,
2017-04-30 21:29:15 +02:00
];
2017-04-30 19:08:49 +02:00
}
2017-05-14 11:11:38 +02:00
$contacts = DB::table('contacts')->whereAccountId($accountId)->orderBy('id')->get([
'contact_key'
]);
2017-04-30 19:08:49 +02:00
foreach ($contacts as $contact) {
2017-04-30 21:29:15 +02:00
$data['contacts'][] = [
'contact_key' => $contact->contact_key,
];
2017-04-30 19:08:49 +02:00
}
2017-05-14 11:11:38 +02:00
$invitations = DB::table('invitations')->whereAccountId($accountId)->orderBy('id')->get([
'invitation_key',
'message_id'
]);
2017-04-30 19:08:49 +02:00
foreach ($invitations as $invitation) {
2017-04-30 21:29:15 +02:00
$data['invitations'][] = [
'invitation_key' => $invitation->invitation_key,
'message_id' => $invitation->message_id,
];
2017-04-30 19:08:49 +02:00
}
2017-05-14 11:11:38 +02:00
$tokens = DB::table('account_tokens')->whereAccountId($accountId)->orderBy('id')->get([
'token'
]);
2017-04-30 19:08:49 +02:00
foreach ($tokens as $token) {
2017-04-30 21:29:15 +02:00
$data['tokens'][] = [
'token' => $token->token,
];
2017-04-30 19:08:49 +02:00
}
return $data;
2017-04-30 18:09:13 +02:00
}
2017-04-30 20:44:52 +02:00
2017-05-02 11:52:22 +02:00
private function logMessage($str)
{
2017-05-30 12:03:06 +02:00
$str = date('Y-m-d h:i:s') . ' ' . $str;
$this->info($str);
$this->log .= $str . "\n";
2017-05-02 11:52:22 +02:00
}
private function logError($str)
{
$this->isValid = false;
$this->logMessage($str);
}
2017-04-30 20:44:52 +02:00
private function truncateTables()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
DB::statement('truncate lookup_companies');
DB::statement('truncate lookup_accounts');
DB::statement('truncate lookup_users');
DB::statement('truncate lookup_contacts');
DB::statement('truncate lookup_invitations');
2018-02-11 12:09:29 +01:00
DB::statement('truncate lookup_proposal_invitations');
DB::statement('truncate lookup_account_tokens');
2017-04-30 20:44:52 +02:00
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
protected function getOptions()
{
return [
2017-11-15 13:51:02 +01:00
['subdomain', null, InputOption::VALUE_OPTIONAL, 'Subdomain', null],
2017-04-30 20:44:52 +02:00
['truncate', null, InputOption::VALUE_OPTIONAL, 'Truncate', null],
['company_id', null, InputOption::VALUE_OPTIONAL, 'Company Id', null],
['page_size', null, InputOption::VALUE_OPTIONAL, 'Page Size', null],
2017-05-02 11:52:22 +02:00
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
['validate', null, InputOption::VALUE_OPTIONAL, 'Validate', null],
2017-04-30 20:44:52 +02:00
];
}
2017-04-30 18:09:13 +02:00
}