1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00

Multi-db fixes

This commit is contained in:
Hillel Coren 2017-05-14 12:11:38 +03:00
parent dc0879b5b6
commit 661028b4e7
8 changed files with 85 additions and 38 deletions

View File

@ -142,12 +142,17 @@ class InitLookup extends Command
$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;
} }
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!");
continue;
}
} else { } else {
LookupUser::create([ LookupUser::create([
'lookup_account_id' => $lookupAccount->id, 'lookup_account_id' => $lookupAccount->id,
'email' => $user['email'] ?: null, 'email' => $user['email'] ?: null,
'user_id' => $user['user_id'], 'user_id' => $user['user_id'],
'oauth_user_key' => $user['oauth_user_key'], 'oauth_user_key' => $user['oauth_user_key'],
'referral_code' => $user['referral_code'],
]); ]);
} }
} }
@ -207,7 +212,9 @@ class InitLookup extends Command
config(['database.default' => $this->option('database')]); config(['database.default' => $this->option('database')]);
$accounts = DB::table('accounts')->whereCompanyId($companyId)->orderBy('id')->get(['id', 'account_key']); $accounts = DB::table('accounts')->whereCompanyId($companyId)->orderBy('id')->get([
'id', 'account_key'
]);
foreach ($accounts as $account) { foreach ($accounts as $account) {
$data[$account->account_key] = $this->parseAccount($account->id); $data[$account->account_key] = $this->parseAccount($account->id);
} }
@ -224,23 +231,35 @@ class InitLookup extends Command
'tokens' => [], 'tokens' => [],
]; ];
$users = DB::table('users')->whereAccountId($accountId)->orderBy('id')->get(['email', 'id', 'oauth_user_id', 'oauth_provider_id']); $users = DB::table('users')->whereAccountId($accountId)->orderBy('id')->get([
'email',
'id',
'oauth_user_id',
'oauth_provider_id',
'referral_code',
]);
foreach ($users as $user) { foreach ($users as $user) {
$data['users'][] = [ $data['users'][] = [
'email' => $user->email, 'email' => $user->email,
'user_id' => $user->id, 'user_id' => $user->id,
'oauth_user_key' => ($user->oauth_provider_id && $user->oauth_user_id) ? ($user->oauth_provider_id . '-' . $user->oauth_user_id) : null, 'oauth_user_key' => ($user->oauth_provider_id && $user->oauth_user_id) ? ($user->oauth_provider_id . '-' . $user->oauth_user_id) : null,
'referral_code' => $user->referral_code,
]; ];
} }
$contacts = DB::table('contacts')->whereAccountId($accountId)->orderBy('id')->get(['contact_key']); $contacts = DB::table('contacts')->whereAccountId($accountId)->orderBy('id')->get([
'contact_key'
]);
foreach ($contacts as $contact) { foreach ($contacts as $contact) {
$data['contacts'][] = [ $data['contacts'][] = [
'contact_key' => $contact->contact_key, 'contact_key' => $contact->contact_key,
]; ];
} }
$invitations = DB::table('invitations')->whereAccountId($accountId)->orderBy('id')->get(['invitation_key', 'message_id']); $invitations = DB::table('invitations')->whereAccountId($accountId)->orderBy('id')->get([
'invitation_key',
'message_id'
]);
foreach ($invitations as $invitation) { foreach ($invitations as $invitation) {
$data['invitations'][] = [ $data['invitations'][] = [
'invitation_key' => $invitation->invitation_key, 'invitation_key' => $invitation->invitation_key,
@ -248,7 +267,9 @@ class InitLookup extends Command
]; ];
} }
$tokens = DB::table('account_tokens')->whereAccountId($accountId)->orderBy('id')->get(['token']); $tokens = DB::table('account_tokens')->whereAccountId($accountId)->orderBy('id')->get([
'token'
]);
foreach ($tokens as $token) { foreach ($tokens as $token) {
$data['tokens'][] = [ $data['tokens'][] = [
'token' => $token->token, 'token' => $token->token,

View File

@ -399,7 +399,7 @@ class AccountController extends BaseController
'user' => Auth::user(), 'user' => Auth::user(),
'oauthProviderName' => AuthService::getProviderName(Auth::user()->oauth_provider_id), 'oauthProviderName' => AuthService::getProviderName(Auth::user()->oauth_provider_id),
'oauthLoginUrls' => $oauthLoginUrls, 'oauthLoginUrls' => $oauthLoginUrls,
'referralCounts' => $this->referralRepository->getCounts(Auth::user()->id), 'referralCounts' => $this->referralRepository->getCounts(Auth::user()->referral_code),
]; ];
return View::make('accounts.user_details', $data); return View::make('accounts.user_details', $data);
@ -1131,7 +1131,7 @@ class AccountController extends BaseController
if (Utils::isNinja()) { if (Utils::isNinja()) {
if (Input::get('referral_code') && ! $user->referral_code) { if (Input::get('referral_code') && ! $user->referral_code) {
$user->referral_code = $this->accountRepo->getReferralCode(); $user->referral_code = strtolower(str_random(RANDOM_KEY_LENGTH));
} }
} }

View File

@ -153,6 +153,12 @@ class Company extends Eloquent
return false; return false;
} }
public function getPlanDetails($includeInactive = false, $includeTrial = true)
{
$account = $this->accounts()->first();
return $account->getPlanDetails($includeInactive, $includeTrial);
}
public function processRefund($user) public function processRefund($user)
{ {
if (! $this->payment) { if (! $this->payment) {

View File

@ -19,6 +19,7 @@ class LookupUser extends LookupModel
'user_id', 'user_id',
'confirmation_code', 'confirmation_code',
'oauth_user_key', 'oauth_user_key',
'referral_code',
]; ];
public static function updateUser($accountKey, $user) public static function updateUser($accountKey, $user)
@ -40,6 +41,7 @@ class LookupUser extends LookupModel
$lookupUser->email = $user->email; $lookupUser->email = $user->email;
$lookupUser->confirmation_code = $user->confirmation_code; $lookupUser->confirmation_code = $user->confirmation_code;
$lookupUser->oauth_user_key = ($user->oauth_provider_id && $user->oauth_user_id) ? ($user->oauth_provider_id . '-' . $user->oauth_user_id) : null; $lookupUser->oauth_user_key = ($user->oauth_provider_id && $user->oauth_user_id) ? ($user->oauth_provider_id . '-' . $user->oauth_user_id) : null;
$lookupUser->referral_code = $user->referral_code;
$lookupUser->save(); $lookupUser->save();
config(['database.default' => $current]); config(['database.default' => $current]);

View File

@ -429,7 +429,8 @@ User::updating(function ($user) {
if (array_key_exists('email', $dirty) if (array_key_exists('email', $dirty)
|| array_key_exists('confirmation_code', $dirty) || array_key_exists('confirmation_code', $dirty)
|| array_key_exists('oauth_user_id', $dirty) || array_key_exists('oauth_user_id', $dirty)
|| array_key_exists('oauth_provider_id', $dirty)) { || array_key_exists('oauth_provider_id', $dirty)
|| array_key_exists('referral_code', $dirty)) {
LookupUser::updateUser($user->account->account_key, $user); LookupUser::updateUser($user->account->account_key, $user);
} }
}); });

View File

@ -37,6 +37,7 @@ class AccountRepository
$company->utm_campaign = Input::get('utm_campaign'); $company->utm_campaign = Input::get('utm_campaign');
$company->utm_term = Input::get('utm_term'); $company->utm_term = Input::get('utm_term');
$company->utm_content = Input::get('utm_content'); $company->utm_content = Input::get('utm_content');
$company->referral_code = Session::get(SESSION_REFERRAL_CODE);
$company->save(); $company->save();
} }
@ -45,13 +46,6 @@ class AccountRepository
$account->account_key = strtolower(str_random(RANDOM_KEY_LENGTH)); $account->account_key = strtolower(str_random(RANDOM_KEY_LENGTH));
$account->company_id = $company->id; $account->company_id = $company->id;
// Track referal code
if ($referralCode = Session::get(SESSION_REFERRAL_CODE)) {
if ($user = User::whereReferralCode($referralCode)->first()) {
$account->referral_user_id = $user->id;
}
}
if ($locale = Session::get(SESSION_LOCALE)) { if ($locale = Session::get(SESSION_LOCALE)) {
if ($language = Language::whereLocale($locale)->first()) { if ($language = Language::whereLocale($locale)->first()) {
$account->language_id = $language->id; $account->language_id = $language->id;
@ -655,18 +649,6 @@ class AccountRepository
return Account::whereRaw('enable_reminder1 = 1 OR enable_reminder2 = 1 OR enable_reminder3 = 1')->get(); return Account::whereRaw('enable_reminder1 = 1 OR enable_reminder2 = 1 OR enable_reminder3 = 1')->get();
} }
public function getReferralCode()
{
do {
$code = strtoupper(str_random(8));
$match = User::whereReferralCode($code)
->withTrashed()
->first();
} while ($match);
return $code;
}
public function createTokens($user, $name) public function createTokens($user, $name)
{ {
$name = trim($name) ?: 'TOKEN'; $name = trim($name) ?: 'TOKEN';

View File

@ -2,32 +2,45 @@
namespace App\Ninja\Repositories; namespace App\Ninja\Repositories;
use App\Models\Account; use App\Models\Company;
use App\Models\DbServer;
class ReferralRepository class ReferralRepository
{ {
public function getCounts($userId) public function getCounts($referralCode)
{ {
$accounts = Account::where('referral_user_id', $userId)->get();
$counts = [ $counts = [
'free' => 0, 'free' => 0,
'pro' => 0, 'pro' => 0,
'enterprise' => 0, 'enterprise' => 0,
]; ];
foreach ($accounts as $account) { if (! $referralCode) {
$counts['free']++; return $counts;
$plan = $account->getPlanDetails(false, false); }
if ($plan) { $current = config('database.default');
$counts['pro']++; $databases = env('MULTI_DB_ENABLED') ? DbServer::all()->pluck('name')->toArray() : [$current];
if ($plan['plan'] == PLAN_ENTERPRISE) {
$counts['enterprise']++; foreach ($databases as $database) {
config(['database.default' => $database]);
$accounts = Company::whereReferralCode($referralCode)->get();
foreach ($accounts as $account) {
$counts['free']++;
$plan = $account->getPlanDetails(false, false);
if ($plan) {
$counts['pro']++;
if ($plan['plan'] == PLAN_ENTERPRISE) {
$counts['enterprise']++;
}
} }
} }
} }
config(['database.default' => $current]);
return $counts; return $counts;
} }
} }

View File

@ -14,6 +14,23 @@ class AddOauthToLookups extends Migration
{ {
Schema::table('lookup_users', function ($table) { Schema::table('lookup_users', function ($table) {
$table->string('oauth_user_key')->nullable()->unique(); $table->string('oauth_user_key')->nullable()->unique();
$table->string('referral_code')->nullable()->unique();
});
Schema::table('companies', function ($table) {
$table->string('referral_code')->nullable();
});
DB::statement('update companies
left join accounts on accounts.company_id = companies.id
left join users on users.id = accounts.referral_user_id
set companies.referral_code = users.referral_code
where users.id is not null');
Schema::table('accounts', function ($table) {
if (Schema::hasColumn('accounts', 'referral_user_id')) {
$table->dropColumn('referral_user_id');
}
}); });
} }
@ -26,6 +43,11 @@ class AddOauthToLookups extends Migration
{ {
Schema::table('lookup_users', function ($table) { Schema::table('lookup_users', function ($table) {
$table->dropColumn('oauth_user_key'); $table->dropColumn('oauth_user_key');
$table->dropColumn('referral_code');
});
Schema::table('companies', function ($table) {
$table->dropColumn('referral_code');
}); });
} }
} }