Migration improvements (#3662)

* scaffold the cases for migration

* Improve migration errors
This commit is contained in:
Benjamin Beganović 2020-04-30 00:01:44 +02:00 committed by GitHub
parent 9c5e087b87
commit a70f42b31e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 52 deletions

View File

@ -191,45 +191,15 @@ class MigrationController extends BaseController
$existing_company = Company::where('company_key', $request->company_key)->first();
if ($request->company_key !== $company->company_key) {
info('Migration type: Fresh migration with new company. MigrationController::203');
$checks = [
'same_keys' => $request->company_key == $company->company_key,
'existing_company' => (bool) $existing_company,
'with_force' => (bool) ($request->has('force') && !empty($request->force)),
];
if ($existing_company && !$request->force) {
info('Migration type: Migration with pre-existing company key, that doesn\t belong to user. No force option.');
Mail::to($user)->send(new ExistingMigration());
return response()->json([
'_id' => Str::uuid(),
'method' => config('queue.default'),
'started_at' => now(),
], 200);
}
$account = auth()->user()->account;
$company = (new ImportMigrations())->getCompany($account);
$company_token = new CompanyToken();
$company_token->user_id = $user->id;
$company_token->company_id = $company->id;
$company_token->account_id = $account->id;
$company_token->name = $request->token_name ?? Str::random(12);
$company_token->token = $request->token ?? \Illuminate\Support\Str::random(64);
$company_token->save();
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'is_locked' => 0,
'notifications' => CompanySettings::notificationDefaults(),
'permissions' => '',
'settings' => null,
]);
}
if (($request->company_key == $company->company_key) && ($request->has('force') && !empty($request->force))) {
info('Migration type: Completely wipe company and start again. MigrationController::228');
// If same company keys, and force provided.
if ($checks['same_keys'] && $checks['with_force']) {
info('Migrating: Same company keys, with force.');
$this->purgeCompany($company);
@ -255,8 +225,9 @@ class MigrationController extends BaseController
]);
}
if (($request->company_key == $company->company_key) && !$request->force) {
info('Migration type: Nothing, skip this since no "force" is provided.. MigrationController::255');
// If keys are same and no force has been provided.
if ($checks['same_keys'] && !$checks['with_force']) {
info('Migrating: Same company keys, no force provided.');
Mail::to($user)->send(new ExistingMigration());
@ -267,6 +238,101 @@ class MigrationController extends BaseController
], 200);
}
// If keys ain't same, but existing company without force.
if (!$checks['same_keys'] && $checks['existing_company'] && !$checks['with_force']) {
info('Migrating: Different keys, existing company with the key without the force option.');
Mail::to($user)->send(new ExistingMigration());
return response()->json([
'_id' => Str::uuid(),
'method' => config('queue.default'),
'started_at' => now(),
], 200);
}
// If keys ain't same, but existing company with force.
if (!$checks['same_keys'] && $checks['existing_company'] && $checks['with_force']) {
info('Migrating: Different keys, exisiting company with force option.');
$this->purgeCompany($company);
$account = auth()->user()->account;
$company = (new ImportMigrations())->getCompany($account);
$company_token = new CompanyToken();
$company_token->user_id = $user->id;
$company_token->company_id = $company->id;
$company_token->account_id = $account->id;
$company_token->name = $request->token_name ?? Str::random(12);
$company_token->token = $request->token ?? \Illuminate\Support\Str::random(64);
$company_token->save();
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'is_locked' => 0,
'notifications' => CompanySettings::notificationDefaults(),
'permissions' => '',
'settings' => null,
]);
}
// If keys ain't same, but with force.
if (!$checks['same_keys'] && $checks['with_force']) {
info('Migrating: Different keys with force.');
$this->purgeCompany($existing_company);
$account = auth()->user()->account;
$company = (new ImportMigrations())->getCompany($account);
$company_token = new CompanyToken();
$company_token->user_id = $user->id;
$company_token->company_id = $company->id;
$company_token->account_id = $account->id;
$company_token->name = $request->token_name ?? Str::random(12);
$company_token->token = $request->token ?? \Illuminate\Support\Str::random(64);
$company_token->save();
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'is_locked' => 0,
'notifications' => CompanySettings::notificationDefaults(),
'permissions' => '',
'settings' => null,
]);
}
// If keys ain't same, fresh migrate.
if (!$checks['same_keys'] && !$checks['with_force']) {
info('Migrating: Vanilla, fresh migrate.');
$account = auth()->user()->account;
$company = (new ImportMigrations())->getCompany($account);
$company_token = new CompanyToken();
$company_token->user_id = $user->id;
$company_token->company_id = $company->id;
$company_token->account_id = $account->id;
$company_token->name = $request->token_name ?? Str::random(12);
$company_token->token = $request->token ?? \Illuminate\Support\Str::random(64);
$company_token->save();
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'is_locked' => 0,
'notifications' => CompanySettings::notificationDefaults(),
'permissions' => '',
'settings' => null,
]);
}
$migration_file = $request->file('migration')
->storeAs('migrations', $request->file('migration')->getClientOriginalName());

View File

@ -419,10 +419,6 @@ class Import implements ShouldQueue
unset($modified['id']);
if (array_key_exists('invitations', $modified)) {
unset($modified['invitations']);
}
$invoice = $invoice_repository->save(
$modified,
InvoiceFactory::create($this->company->id, $modified['user_id'])
@ -468,10 +464,6 @@ class Import implements ShouldQueue
unset($modified['id']);
if (array_key_exists('invitations', $modified)) {
unset($modified['invitations']);
}
$credit = $credit_repository->save(
$modified,
CreditFactory::create($this->company->id, $modified['user_id'])
@ -518,10 +510,6 @@ class Import implements ShouldQueue
unset($modified['id']);
if (array_key_exists('invitations', $modified)) {
unset($modified['invitations']);
}
$invoice = $quote_repository->save(
$modified,
QuoteFactory::create($this->company->id, $modified['user_id'])