Working on company importer

This commit is contained in:
David Bomba 2021-05-31 08:55:27 +10:00
parent 6dc3668ff6
commit 5e820bbba7
2 changed files with 102 additions and 56 deletions

View File

@ -13,8 +13,11 @@ namespace App\Http\Controllers;
use App\Http\Requests\Import\ImportJsonRequest; use App\Http\Requests\Import\ImportJsonRequest;
use App\Jobs\Company\CompanyExport; use App\Jobs\Company\CompanyExport;
use App\Jobs\Company\CompanyImport;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
class ImportJsonController extends BaseController class ImportJsonController extends BaseController
{ {
@ -56,9 +59,38 @@ class ImportJsonController extends BaseController
public function index(ImportJsonRequest $request) public function index(ImportJsonRequest $request)
{ {
// CompanyExport::dispatch(auth()->user()->getCompany(), auth()->user()); $import_file = $request->file('files');
$contents = $this->unzipFile($import_file->getPathname());
$hash = Str::random(32);
Cache::put( $hash, base64_encode( $contents ), 3600 );
CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $hash, $request->all());
return response()->json(['message' => 'Processing'], 200); return response()->json(['message' => 'Processing'], 200);
} }
private function unzipFile($file_contents)
{
$zip = new ZipArchive();
$archive = $zip->open($file_contents);
$filename = pathinfo($file_contents, PATHINFO_FILENAME);
$zip->extractTo(public_path("storage/backups/{$filename}"));
$zip->close();
$file_location = public_path("storage/backups/$filename/backup.json");
if (! file_exists($file_location))
throw new NonExistingMigrationFile('Backup file does not exist, or it is corrupted.');
$data = json_decode(file_get_contents($file_location));
unlink($file_contents);
unlink($file_location);
return $data
}
} }

View File

@ -28,6 +28,7 @@ use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation; use App\Models\RecurringInvoiceInvitation;
use App\Models\User; use App\Models\User;
use App\Models\VendorContact; use App\Models\VendorContact;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
@ -46,48 +47,49 @@ class CompanyImport implements ShouldQueue
protected $current_app_version; protected $current_app_version;
public $company;
private $account; private $account;
public $file_path; public $company;
private $backup_file; public $user;
public $import_company; private $hash;
public $backup_file;
public $ids = []; public $ids = [];
private $options = ''; private $request_array = [];
private $importables = [ private $importables = [
'company', 'company',
'users', 'users',
// 'payment_terms', 'payment_terms',
// 'tax_rates', 'tax_rates',
// 'expense_categories', 'expense_categories',
// 'task_statuses', 'task_statuses',
// 'clients', 'clients',
// 'client_contacts', 'client_contacts',
// 'products', 'products',
// 'vendors', 'vendors',
// 'projects', 'projects',
// 'company_gateways', 'company_gateways',
// 'client_gateway_tokens', 'client_gateway_tokens',
// 'group_settings', 'group_settings',
// 'credits', 'credits',
// 'invoices', 'invoices',
// 'recurring_invoices', 'recurring_invoices',
// 'quotes', 'quotes',
// 'payments', 'payments',
// 'subscriptions', 'subscriptions',
// 'expenses', 'expenses',
// 'tasks', 'tasks',
// 'documents', 'documents',
// 'webhooks', 'webhooks',
// 'system_logs', 'activities',
// 'company_ledger', 'backups',
// 'backups', 'system_logs',
'company_ledger',
]; ];
/** /**
@ -95,13 +97,14 @@ class CompanyImport implements ShouldQueue
* *
* @param Company $company * @param Company $company
* @param User $user * @param User $user
* @param string $custom_token_name * @param string $hash - the cache hash of the import data.
* @param array $request->all()
*/ */
public function __construct(Company $company, string $file_path, array $options) public function __construct(Company $company, User $user, string $hash, array $request_array)
{ {
$this->company = $company; $this->company = $company;
$this->file_path = $file_path; $this->hash = $hash;
$this->options = $options; $this->request_array = $request_array;
$this->current_app_version = config('ninja.app_version'); $this->current_app_version = config('ninja.app_version');
} }
@ -112,16 +115,24 @@ class CompanyImport implements ShouldQueue
$this->company = Company::where('company_key', $this->company->company_key)->firstOrFail(); $this->company = Company::where('company_key', $this->company->company_key)->firstOrFail();
$this->account = $this->company->account; $this->account = $this->company->account;
$this->unzipFile() $this->backup_file = Cache::get($this->hash);
->preFlightChecks();
foreach($this->importables as $import){ if ( empty( $this->import_object ) )
throw new \Exception('No import data found, has the cache expired?');
$method = Str::ucfirst(Str::camel($import)); $this->backup_file = base64_decode($this->backup_file);
$this->{$method}();
} /* Determine what we have to import now - should we also purge existing data? */
// foreach($this->importables as $import){
// $method = Str::ucfirst(Str::camel($import));
// $this->{$method}();
// }
} }
@ -145,28 +156,31 @@ class CompanyImport implements ShouldQueue
return $this; return $this;
} }
private function unzipFile()
private function importSettings()
{ {
$zip = new ZipArchive();
$archive = $zip->open(public_path("storage/backups/{$this->file_path}"));
$filename = pathinfo($this->filepath, PATHINFO_FILENAME);
$zip->extractTo(public_path("storage/backups/{$filename}"));
$zip->close();
$file_location = public_path("storage/backups/$filename/backup.json");
if (! file_exists($file_location)) { $this->company->settings = $this->backup_file->company->settings;
throw new NonExistingMigrationFile('Backup file does not exist, or it is corrupted.'); $this->company->save();
}
$this->backup_file = json_decode(file_get_contents($file_location));
return $this; return $this;
} }
private function importCompany() private function importCompany()
{ {
$tmp_company = $this->backup_file->company;
$tmp_company->company_key = $this->createHash();
$tmp_company->db = config('database.default');
$tmp_company->account_id = $this->account->id;
if(Ninja::isHosted())
$tmp_company->subdomain = MultiDB::randomSubdomainGenerator();
else
$tmp_company->subdomain = '';
$this->company = $tmp_company;
$this->company->save();
//$this->import_company = ..
return $this; return $this;
} }