2020-03-10 22:10:27 +01:00
|
|
|
<?php
|
2020-09-07 12:18:56 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-07 12:18:56 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-09-07 12:18:56 +02:00
|
|
|
*/
|
2020-03-10 22:10:27 +01:00
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\DataMapper\CompanySettings;
|
2020-11-30 08:43:33 +01:00
|
|
|
use App\Exceptions\MigrationValidatorFailed;
|
|
|
|
use App\Exceptions\NonExistingMigrationFile;
|
|
|
|
use App\Exceptions\ProcessingMigrationArchiveFailed;
|
|
|
|
use App\Exceptions\ResourceDependencyMissing;
|
|
|
|
use App\Exceptions\ResourceNotAvailableForMigration;
|
|
|
|
use App\Jobs\Util\Import;
|
|
|
|
use App\Mail\MigrationFailed;
|
2020-03-10 22:10:27 +01:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CompanyToken;
|
|
|
|
use App\Models\User;
|
2020-11-18 11:46:36 +01:00
|
|
|
use App\Utils\Traits\AppSetup;
|
2020-03-10 22:10:27 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-10-28 11:10:49 +01:00
|
|
|
use DirectoryIterator;
|
|
|
|
use Faker\Factory;
|
|
|
|
use Faker\Generator;
|
2020-03-10 22:10:27 +01:00
|
|
|
use Illuminate\Console\Command;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Support\Str;
|
2020-11-30 08:43:33 +01:00
|
|
|
use ZipArchive;
|
2020-03-10 22:10:27 +01:00
|
|
|
|
|
|
|
class ImportMigrations extends Command
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2020-11-18 11:46:36 +01:00
|
|
|
use AppSetup;
|
|
|
|
|
2020-03-10 22:10:27 +01:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-09-05 12:31:08 +02:00
|
|
|
protected $signature = 'ninja:old-import {--path=}';
|
2020-03-10 22:10:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Massively import the migrations.';
|
|
|
|
|
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @var Generator
|
2020-03-10 22:10:27 +01:00
|
|
|
*/
|
|
|
|
private $faker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2023-07-07 07:36:49 +02:00
|
|
|
$this->faker = Factory::create();
|
|
|
|
|
2020-11-30 08:43:33 +01:00
|
|
|
$path = $this->option('path') ?? public_path('storage/migrations/import');
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
$directory = new DirectoryIterator($path);
|
2020-03-10 22:10:27 +01:00
|
|
|
|
|
|
|
foreach ($directory as $file) {
|
|
|
|
if ($file->getExtension() === 'zip') {
|
2020-11-30 08:43:33 +01:00
|
|
|
$user = $this->getUser();
|
|
|
|
$company = $this->getUser()->companies()->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->info('Started processing: '.$file->getBasename().' at '.now());
|
2020-11-30 08:43:33 +01:00
|
|
|
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
$archive = $zip->open($file->getRealPath());
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (! $archive) {
|
|
|
|
throw new ProcessingMigrationArchiveFailed('Processing migration archive failed. Migration file is possibly corrupted.');
|
|
|
|
}
|
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
$filename = pathinfo($file->getRealPath(), PATHINFO_FILENAME);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
$zip->extractTo(public_path("storage/migrations/{$filename}"));
|
|
|
|
$zip->close();
|
2020-11-30 08:43:33 +01:00
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
$import_file = public_path("storage/migrations/$filename/migration.json");
|
2020-11-30 08:43:33 +01:00
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
Import::dispatch($import_file, $this->getUser()->companies()->first(), $this->getUser());
|
2023-07-04 00:37:09 +02:00
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
} catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) {
|
2023-04-26 08:55:49 +02:00
|
|
|
\Mail::to($user)->send(new MigrationFailed($e, $company));
|
2020-11-30 08:43:33 +01:00
|
|
|
|
2020-12-03 15:33:18 +01:00
|
|
|
if (app()->environment() !== 'production') {
|
|
|
|
info($e->getMessage());
|
|
|
|
}
|
2020-11-30 08:43:33 +01:00
|
|
|
}
|
2020-12-03 15:33:18 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 22:10:27 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 21:38:22 +01:00
|
|
|
public function getUser(): User
|
2020-03-10 22:10:27 +01:00
|
|
|
{
|
2020-03-24 10:15:30 +01:00
|
|
|
$account = $this->getAccount();
|
|
|
|
$company = $this->getCompany($account);
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
$user = User::factory()->create([
|
2020-03-24 10:15:30 +01:00
|
|
|
'account_id' => $account->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'email' => Str::random(10).'@example.com',
|
2021-11-06 01:46:12 +01:00
|
|
|
'confirmation_code' => $this->createDbHash($company->db),
|
2020-03-10 22:10:27 +01:00
|
|
|
]);
|
|
|
|
|
2020-11-16 11:29:17 +01:00
|
|
|
CompanyToken::unguard();
|
|
|
|
|
2020-03-10 22:10:27 +01:00
|
|
|
$company_token = CompanyToken::create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'company_id' => $company->id,
|
|
|
|
'account_id' => $account->id,
|
2020-11-16 05:40:52 +01:00
|
|
|
'name' => 'First token',
|
2020-10-28 11:10:49 +01:00
|
|
|
'token' => Str::random(64),
|
2020-03-10 22:10:27 +01:00
|
|
|
]);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-03-10 22:10:27 +01:00
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'is_locked' => 0,
|
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
|
|
|
'permissions' => '',
|
|
|
|
'settings' => null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:38:22 +01:00
|
|
|
public function getAccount(): Account
|
2020-03-10 22:10:27 +01:00
|
|
|
{
|
2020-10-01 12:49:47 +02:00
|
|
|
return Account::factory()->create();
|
2020-03-10 22:10:27 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 21:38:22 +01:00
|
|
|
public function getCompany(Account $account): Company
|
2020-03-10 22:10:27 +01:00
|
|
|
{
|
2020-10-02 00:19:42 +02:00
|
|
|
$company = Company::factory()->create([
|
2020-03-10 22:10:27 +01:00
|
|
|
'account_id' => $account->id,
|
2020-11-23 13:55:04 +01:00
|
|
|
'is_disabled' => true,
|
2020-03-10 22:10:27 +01:00
|
|
|
]);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $account->default_company_id) {
|
|
|
|
$account->default_company_id = $company->id;
|
2020-04-19 12:29:58 +02:00
|
|
|
$account->save();
|
|
|
|
}
|
2020-03-10 22:10:27 +01:00
|
|
|
|
|
|
|
return $company;
|
|
|
|
}
|
|
|
|
}
|