diff --git a/app/Console/Commands/ImportMigrations.php b/app/Console/Commands/ImportMigrations.php index bfbc60cd6d08..069cf5ebcfe9 100644 --- a/app/Console/Commands/ImportMigrations.php +++ b/app/Console/Commands/ImportMigrations.php @@ -12,7 +12,15 @@ namespace App\Console\Commands; use App\DataMapper\CompanySettings; +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\Jobs\Util\StartMigration; +use App\Libraries\MultiDB; +use App\Mail\MigrationFailed; use App\Models\Account; use App\Models\Company; use App\Models\CompanyToken; @@ -24,6 +32,7 @@ use Faker\Factory; use Faker\Generator; use Illuminate\Console\Command; use Illuminate\Support\Str; +use ZipArchive; class ImportMigrations extends Command { @@ -69,17 +78,45 @@ class ImportMigrations extends Command public function handle() { $this->buildCache(); - - $path = $this->option('path') ?? storage_path('migrations/import'); + + $path = $this->option('path') ?? public_path('storage/migrations/import'); $directory = new DirectoryIterator($path); foreach ($directory as $file) { if ($file->getExtension() === 'zip') { + + $user = $this->getUser(); + $company = $this->getUser()->companies()->first(); + $this->info('Started processing: '.$file->getBasename().' at '.now()); - StartMigration::dispatch($file->getRealPath(), $this->getUser(), $this->getUser()->companies()->first()); - } - } + + $zip = new ZipArchive(); + $archive = $zip->open($file->getRealPath()); + + try { + if (! $archive) { + throw new ProcessingMigrationArchiveFailed('Processing migration archive failed. Migration file is possibly corrupted.'); + } + + $filename = pathinfo($file->getRealPath(), PATHINFO_FILENAME); + + $zip->extractTo(public_path("storage/migrations/{$filename}")); + $zip->close(); + + $import_file = public_path("storage/migrations/$filename/migration.json"); + + Import::dispatch($import_file, $this->getUser()->companies()->first(), $this->getUser()); + // StartMigration::dispatch($file->getRealPath(), $this->getUser(), $this->getUser()->companies()->first()); + } + catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) { + \Mail::to($this->user)->send(new MigrationFailed($e, $e->getMessage())); + + if (app()->environment() !== 'production') { + info($e->getMessage()); + } + + }}} } public function getUser(): User diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 3d9a8639069d..b1febefdab7a 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -55,7 +55,7 @@ class CompanySettings extends BaseSettings public $default_task_rate = 0; // @TODO Where do we inject this? public $payment_terms = ''; //@implemented - public $send_reminders = false; //@TODO + public $send_reminders = true; //@TODO public $custom_message_dashboard = ''; // @TODO There currently is no dashboard so this is pending public $custom_message_unpaid_invoice = ''; diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 7b210f32cb79..54c5378c5efb 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -177,6 +177,9 @@ class Import implements ShouldQueue { set_time_limit(0); + auth()->login($this->user, false); + auth()->user()->setCompany($this->company); + // $jsonStream = \JsonMachine\JsonMachine::fromFile($this->file_path, "/data"); $array = json_decode(file_get_contents($this->file_path), 1); $data = $array['data']; diff --git a/app/Transformers/ActivityTransformer.php b/app/Transformers/ActivityTransformer.php index 887f0b776c34..1acf4f1b49a1 100644 --- a/app/Transformers/ActivityTransformer.php +++ b/app/Transformers/ActivityTransformer.php @@ -39,6 +39,7 @@ class ActivityTransformer extends EntityTransformer 'company_id' => $activity->company_id ? (string) $this->encodePrimaryKey($activity->company_id) : '', 'user_id' => (string) $this->encodePrimaryKey($activity->user_id), 'invoice_id' => $activity->invoice_id ? (string) $this->encodePrimaryKey($activity->invoice_id) : '', + 'quote_id' => $activity->quote_id ? (string) $this->encodePrimaryKey($activity->quote_id) : '', 'payment_id' => $activity->payment_id ? (string) $this->encodePrimaryKey($activity->payment_id) : '', 'credit_id' => $activity->credit_id ? (string) $this->encodePrimaryKey($activity->credit_id) : '', 'updated_at' => (int) $activity->updated_at,