Send e-mail when migration has completed (#3573)

- New completed.blade.php
- Apply php-cs-fixer on Import.php
This commit is contained in:
Benjamin Beganović 2020-04-01 14:39:59 +02:00 committed by GitHub
parent ceb82ad275
commit f9567e8227
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 46 deletions

View File

@ -19,6 +19,7 @@ use App\Http\ValidationRules\ValidCompanyGatewayFeesAndLimitsRule;
use App\Http\ValidationRules\ValidUserForCompany; use App\Http\ValidationRules\ValidUserForCompany;
use App\Jobs\Company\CreateCompanyToken; use App\Jobs\Company\CreateCompanyToken;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Mail\MigrationCompleted;
use App\Mail\MigrationFailed; use App\Mail\MigrationFailed;
use App\Models\Client; use App\Models\Client;
use App\Models\ClientGatewayToken; use App\Models\ClientGatewayToken;
@ -135,24 +136,24 @@ class Import implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
foreach ($this->data as $key => $resource) { foreach ($this->data as $key => $resource) {
if (! in_array($key, $this->available_imports)) {
if (!in_array($key, $this->available_imports)) {
//throw new ResourceNotAvailableForMigration("Resource {$key} is not available for migration."); //throw new ResourceNotAvailableForMigration("Resource {$key} is not available for migration.");
\Log::error("cannot migrate {$key} yet...."); info("Resource {$key} is not available for migration.");
continue; continue;
} }
$method = sprintf("process%s", Str::ucfirst(Str::camel($key))); $method = sprintf('process%s', Str::ucfirst(Str::camel($key)));
\Log::error("Importing {$key}"); info("Importing {$key}");
$this->{$method}($resource); $this->{$method}($resource);
} }
info('Completed🚀🚀🚀🚀🚀 at ' . now()); Mail::to($this->user)->send(new MigrationCompleted());
info('Completed🚀🚀🚀🚀🚀 at '.now());
} }
/** /**
@ -170,7 +171,6 @@ class Import implements ShouldQueue
$validator = Validator::make($data, $rules); $validator = Validator::make($data, $rules);
if ($validator->fails()) { if ($validator->fails()) {
throw new MigrationValidatorFailed(json_encode($validator->errors())); throw new MigrationValidatorFailed(json_encode($validator->errors()));
} }
@ -186,32 +186,25 @@ class Import implements ShouldQueue
private function transformCompanyData(array $data): array private function transformCompanyData(array $data): array
{ {
$company_settings = CompanySettings::defaults(); $company_settings = CompanySettings::defaults();
if (array_key_exists('settings', $data) ) { if (array_key_exists('settings', $data)) {
foreach ($data['settings'] as $key => $value) { foreach ($data['settings'] as $key => $value) {
if ($key == 'invoice_design_id' || $key == 'quote_design_id' || $key == 'credit_design_id') {
if($key == 'invoice_design_id' || $key == 'quote_design_id' || $key == 'credit_design_id'){ $value = $this->encodePrimaryKey($value);
$value = $this->encodePrimaryKey($value);
} }
if($key == 'payment_terms' && $key = ''){ if ($key == 'payment_terms' && $key = '') {
$value = -1; $value = -1;
} }
$company_settings->{$key} = $value; $company_settings->{$key} = $value;
} }
$data['settings'] = $company_settings; $data['settings'] = $company_settings;
} }
return $data;
return $data;
} }
/** /**
@ -312,7 +305,7 @@ class Import implements ShouldQueue
{ {
Client::unguard(); Client::unguard();
$contact_repository = new ClientContactRepository(); $contact_repository = new ClientContactRepository();
$client_repository = new ClientRepository($contact_repository); $client_repository = new ClientRepository($contact_repository);
foreach ($data as $key => $resource) { foreach ($data as $key => $resource) {
@ -413,7 +406,7 @@ class Import implements ShouldQueue
foreach ($data as $resource) { foreach ($data as $resource) {
$modified = $resource; $modified = $resource;
if (array_key_exists('client_id', $resource) && !array_key_exists('clients', $this->ids)) { if (array_key_exists('client_id', $resource) && ! array_key_exists('clients', $this->ids)) {
throw new ResourceDependencyMissing('Processing invoices failed, because of missing dependency - clients.'); throw new ResourceDependencyMissing('Processing invoices failed, because of missing dependency - clients.');
} }
@ -423,8 +416,9 @@ class Import implements ShouldQueue
unset($modified['id']); unset($modified['id']);
if(array_key_exists('invitations', $modified)) if (array_key_exists('invitations', $modified)) {
unset($modified['invitations']); unset($modified['invitations']);
}
$invoice = $invoice_repository->save( $invoice = $invoice_repository->save(
$modified, $modified,
@ -461,7 +455,7 @@ class Import implements ShouldQueue
foreach ($data as $resource) { foreach ($data as $resource) {
$modified = $resource; $modified = $resource;
if (array_key_exists('client_id', $resource) && !array_key_exists('clients', $this->ids)) { if (array_key_exists('client_id', $resource) && ! array_key_exists('clients', $this->ids)) {
throw new ResourceDependencyMissing('Processing credits failed, because of missing dependency - clients.'); throw new ResourceDependencyMissing('Processing credits failed, because of missing dependency - clients.');
} }
@ -471,8 +465,9 @@ class Import implements ShouldQueue
unset($modified['id']); unset($modified['id']);
if(array_key_exists('invitations', $modified)) if (array_key_exists('invitations', $modified)) {
unset($modified['invitations']); unset($modified['invitations']);
}
$credit = $credit_repository->save( $credit = $credit_repository->save(
$modified, $modified,
@ -509,7 +504,7 @@ class Import implements ShouldQueue
foreach ($data as $resource) { foreach ($data as $resource) {
$modified = $resource; $modified = $resource;
if (array_key_exists('client_id', $resource) && !array_key_exists('clients', $this->ids)) { if (array_key_exists('client_id', $resource) && ! array_key_exists('clients', $this->ids)) {
throw new ResourceDependencyMissing('Processing quotes failed, because of missing dependency - clients.'); throw new ResourceDependencyMissing('Processing quotes failed, because of missing dependency - clients.');
} }
@ -520,8 +515,9 @@ class Import implements ShouldQueue
unset($modified['id']); unset($modified['id']);
if(array_key_exists('invitations', $modified)) if (array_key_exists('invitations', $modified)) {
unset($modified['invitations']); unset($modified['invitations']);
}
$invoice = $quote_repository->save( $invoice = $quote_repository->save(
$modified, $modified,
@ -561,7 +557,7 @@ class Import implements ShouldQueue
foreach ($data as $resource) { foreach ($data as $resource) {
$modified = $resource; $modified = $resource;
if (array_key_exists('client_id', $resource) && !array_key_exists('clients', $this->ids)) { if (array_key_exists('client_id', $resource) && ! array_key_exists('clients', $this->ids)) {
throw new ResourceDependencyMissing('Processing payments failed, because of missing dependency - clients.'); throw new ResourceDependencyMissing('Processing payments failed, because of missing dependency - clients.');
} }
@ -590,7 +586,7 @@ class Import implements ShouldQueue
"payments_{$old_user_key}" => [ "payments_{$old_user_key}" => [
'old' => $old_user_key, 'old' => $old_user_key,
'new' => $payment->id, 'new' => $payment->id,
] ],
]; ];
} }
@ -600,20 +596,20 @@ class Import implements ShouldQueue
private function processDocuments(array $data): void private function processDocuments(array $data): void
{ {
Document::unguard(); Document::unguard();
/** No validators since data provided by database is already valid. */ /* No validators since data provided by database is already valid. */
foreach ($data as $resource) { foreach ($data as $resource) {
$modified = $resource; $modified = $resource;
if (array_key_exists('invoice_id', $resource) && $resource['invoice_id'] && !array_key_exists('invoices', $this->ids)) { if (array_key_exists('invoice_id', $resource) && $resource['invoice_id'] && ! array_key_exists('invoices', $this->ids)) {
throw new ResourceDependencyMissing('Processing documents failed, because of missing dependency - invoices.'); throw new ResourceDependencyMissing('Processing documents failed, because of missing dependency - invoices.');
} }
if (array_key_exists('expense_id', $resource) && $resource['expense_id'] && !array_key_exists('expenses', $this->ids)) { if (array_key_exists('expense_id', $resource) && $resource['expense_id'] && ! array_key_exists('expenses', $this->ids)) {
throw new ResourceDependencyMissing('Processing documents failed, because of missing dependency - expenses.'); throw new ResourceDependencyMissing('Processing documents failed, because of missing dependency - expenses.');
} }
/** Remove because of polymorphic joins. */ /* Remove because of polymorphic joins. */
unset($modified['invoice_id']); unset($modified['invoice_id']);
unset($modified['expense_id']); unset($modified['expense_id']);
@ -641,7 +637,7 @@ class Import implements ShouldQueue
"documents_{$old_user_key}" => [ "documents_{$old_user_key}" => [
'old' => $resource['id'], 'old' => $resource['id'],
'new' => $document->id, 'new' => $document->id,
] ],
]; ];
} }
@ -687,7 +683,7 @@ class Import implements ShouldQueue
"company_gateways_{$old_user_key}" => [ "company_gateways_{$old_user_key}" => [
'old' => $resource['id'], 'old' => $resource['id'],
'new' => $company_gateway->id, 'new' => $company_gateway->id,
] ],
]; ];
} }
@ -714,7 +710,7 @@ class Import implements ShouldQueue
"client_gateway_tokens_{$old_user_key}" => [ "client_gateway_tokens_{$old_user_key}" => [
'old' => $resource['id'], 'old' => $resource['id'],
'new' => $cgt->id, 'new' => $cgt->id,
] ],
]; ];
} }
@ -740,7 +736,7 @@ class Import implements ShouldQueue
{ {
$user = MultiDB::hasUser(['email' => $data]); $user = MultiDB::hasUser(['email' => $data]);
if (!$user) { if (! $user) {
$user = UserFactory::create($this->company->account->id); $user = UserFactory::create($this->company->account->id);
} }
@ -755,11 +751,11 @@ class Import implements ShouldQueue
*/ */
public function transformId(string $resource, string $old): int public function transformId(string $resource, string $old): int
{ {
if (!array_key_exists($resource, $this->ids)) { if (! array_key_exists($resource, $this->ids)) {
throw new \Exception("Resource {$resource} not available."); throw new \Exception("Resource {$resource} not available.");
} }
if (!array_key_exists("{$resource}_{$old}", $this->ids[$resource])) { if (! array_key_exists("{$resource}_{$old}", $this->ids[$resource])) {
throw new \Exception("Missing resource key: {$resource}_{$old}"); throw new \Exception("Missing resource key: {$resource}_{$old}");
} }
@ -767,7 +763,7 @@ class Import implements ShouldQueue
} }
/** /**
* Process & handle user_id * Process & handle user_id.
* *
* @param array $resource * @param array $resource
* @return int|mixed * @return int|mixed
@ -775,21 +771,20 @@ class Import implements ShouldQueue
*/ */
public function processUserId(array $resource) public function processUserId(array $resource)
{ {
if (!array_key_exists('user_id', $resource)) { if (! array_key_exists('user_id', $resource)) {
return $this->user->id; return $this->user->id;
} }
if (array_key_exists('user_id', $resource) && !array_key_exists('users', $this->ids)) { if (array_key_exists('user_id', $resource) && ! array_key_exists('users', $this->ids)) {
return $this->user->id; return $this->user->id;
} }
return $this->transformId('users', $resource['user_id']); return $this->transformId('users', $resource['user_id']);
} }
public function failed($exception = null) public function failed($exception = null)
{ {
\Log::error("the job failed"); info('the job failed');
\Log::error(print_r($exception->getMessage(),1)); info(print_r($exception->getMessage(), 1));
} }
} }

View File

@ -0,0 +1,33 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MigrationCompleted extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('email.migration.completed');
}
}

View File

@ -0,0 +1,31 @@
@component('email.template.master', ['design' => 'light'])
@slot('header')
@component('email.components.header')
Migration completed
@endcomponent
@endslot
@slot('greeting')
Hello,
@endslot
We're happy to inform you that migration has been completed successfully. It is ready for you to review it.
@component('email.components.button', ['url' => url('/')])
Visit portal
@endcomponent
@slot('signature')
Thank you, <br>
Invoice Ninja
@endslot
@slot('footer')
@component('email.components.footer', ['url' => 'https://invoiceninja.com', 'url_text' => '&copy; InvoiceNinja'])
For any info, please visit InvoiceNinja.
@endcomponent
@endslot
@endcomponent