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:
parent
dc0879b5b6
commit
661028b4e7
@ -142,12 +142,17 @@ class InitLookup extends Command
|
||||
$this->logError("LookupUser - lookupAccountId: {$lookupAccount->id}, userId: {$user['user_id']} | Not found!");
|
||||
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 {
|
||||
LookupUser::create([
|
||||
'lookup_account_id' => $lookupAccount->id,
|
||||
'email' => $user['email'] ?: null,
|
||||
'user_id' => $user['user_id'],
|
||||
'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')]);
|
||||
|
||||
$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) {
|
||||
$data[$account->account_key] = $this->parseAccount($account->id);
|
||||
}
|
||||
@ -224,23 +231,35 @@ class InitLookup extends Command
|
||||
'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) {
|
||||
$data['users'][] = [
|
||||
'email' => $user->email,
|
||||
'user_id' => $user->id,
|
||||
'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) {
|
||||
$data['contacts'][] = [
|
||||
'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) {
|
||||
$data['invitations'][] = [
|
||||
'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) {
|
||||
$data['tokens'][] = [
|
||||
'token' => $token->token,
|
||||
|
@ -399,7 +399,7 @@ class AccountController extends BaseController
|
||||
'user' => Auth::user(),
|
||||
'oauthProviderName' => AuthService::getProviderName(Auth::user()->oauth_provider_id),
|
||||
'oauthLoginUrls' => $oauthLoginUrls,
|
||||
'referralCounts' => $this->referralRepository->getCounts(Auth::user()->id),
|
||||
'referralCounts' => $this->referralRepository->getCounts(Auth::user()->referral_code),
|
||||
];
|
||||
|
||||
return View::make('accounts.user_details', $data);
|
||||
@ -1131,7 +1131,7 @@ class AccountController extends BaseController
|
||||
|
||||
if (Utils::isNinja()) {
|
||||
if (Input::get('referral_code') && ! $user->referral_code) {
|
||||
$user->referral_code = $this->accountRepo->getReferralCode();
|
||||
$user->referral_code = strtolower(str_random(RANDOM_KEY_LENGTH));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,6 +153,12 @@ class Company extends Eloquent
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getPlanDetails($includeInactive = false, $includeTrial = true)
|
||||
{
|
||||
$account = $this->accounts()->first();
|
||||
return $account->getPlanDetails($includeInactive, $includeTrial);
|
||||
}
|
||||
|
||||
public function processRefund($user)
|
||||
{
|
||||
if (! $this->payment) {
|
||||
|
@ -19,6 +19,7 @@ class LookupUser extends LookupModel
|
||||
'user_id',
|
||||
'confirmation_code',
|
||||
'oauth_user_key',
|
||||
'referral_code',
|
||||
];
|
||||
|
||||
public static function updateUser($accountKey, $user)
|
||||
@ -40,6 +41,7 @@ class LookupUser extends LookupModel
|
||||
$lookupUser->email = $user->email;
|
||||
$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->referral_code = $user->referral_code;
|
||||
$lookupUser->save();
|
||||
|
||||
config(['database.default' => $current]);
|
||||
|
@ -429,7 +429,8 @@ User::updating(function ($user) {
|
||||
if (array_key_exists('email', $dirty)
|
||||
|| array_key_exists('confirmation_code', $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);
|
||||
}
|
||||
});
|
||||
|
@ -37,6 +37,7 @@ class AccountRepository
|
||||
$company->utm_campaign = Input::get('utm_campaign');
|
||||
$company->utm_term = Input::get('utm_term');
|
||||
$company->utm_content = Input::get('utm_content');
|
||||
$company->referral_code = Session::get(SESSION_REFERRAL_CODE);
|
||||
$company->save();
|
||||
}
|
||||
|
||||
@ -45,13 +46,6 @@ class AccountRepository
|
||||
$account->account_key = strtolower(str_random(RANDOM_KEY_LENGTH));
|
||||
$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 ($language = Language::whereLocale($locale)->first()) {
|
||||
$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();
|
||||
}
|
||||
|
||||
public function getReferralCode()
|
||||
{
|
||||
do {
|
||||
$code = strtoupper(str_random(8));
|
||||
$match = User::whereReferralCode($code)
|
||||
->withTrashed()
|
||||
->first();
|
||||
} while ($match);
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
public function createTokens($user, $name)
|
||||
{
|
||||
$name = trim($name) ?: 'TOKEN';
|
||||
|
@ -2,32 +2,45 @@
|
||||
|
||||
namespace App\Ninja\Repositories;
|
||||
|
||||
use App\Models\Account;
|
||||
use App\Models\Company;
|
||||
use App\Models\DbServer;
|
||||
|
||||
class ReferralRepository
|
||||
{
|
||||
public function getCounts($userId)
|
||||
public function getCounts($referralCode)
|
||||
{
|
||||
$accounts = Account::where('referral_user_id', $userId)->get();
|
||||
|
||||
$counts = [
|
||||
'free' => 0,
|
||||
'pro' => 0,
|
||||
'enterprise' => 0,
|
||||
];
|
||||
|
||||
foreach ($accounts as $account) {
|
||||
$counts['free']++;
|
||||
$plan = $account->getPlanDetails(false, false);
|
||||
if (! $referralCode) {
|
||||
return $counts;
|
||||
}
|
||||
|
||||
if ($plan) {
|
||||
$counts['pro']++;
|
||||
if ($plan['plan'] == PLAN_ENTERPRISE) {
|
||||
$counts['enterprise']++;
|
||||
$current = config('database.default');
|
||||
$databases = env('MULTI_DB_ENABLED') ? DbServer::all()->pluck('name')->toArray() : [$current];
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,23 @@ class AddOauthToLookups extends Migration
|
||||
{
|
||||
Schema::table('lookup_users', function ($table) {
|
||||
$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) {
|
||||
$table->dropColumn('oauth_user_key');
|
||||
$table->dropColumn('referral_code');
|
||||
});
|
||||
|
||||
Schema::table('companies', function ($table) {
|
||||
$table->dropColumn('referral_code');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user