1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 12:12:48 +01:00

Streamlined hosted migration

This commit is contained in:
= 2021-07-17 15:26:47 +10:00
parent 05b12a6bb9
commit 0743b003ab
3 changed files with 162 additions and 4 deletions

View File

@ -72,11 +72,10 @@ class StepsController extends BaseController
session()->put('MIGRATION_ENDPOINT', 'https://v5-app1.invoicing.co');
//refactor here to make this a little more magical
//
return redirect(
url('/migration/companies')
url('/migration/companies?hosted=true')
);
// return redirect(
@ -216,7 +215,7 @@ class StepsController extends BaseController
);
}
if(Utils::isNinja())
if($request->has('hosted') && $request->input('hosted') == 'true')
{
//push a job with $request->all() and the auth()->user() reference;
@ -231,7 +230,8 @@ class StepsController extends BaseController
//and process as per normal.
//
//we should include a success failure email to contact@ so we can follow up.
HostedMigration::dispatch(auth()->user(), $request->all(), config('database.default'));
if ($completeService->isSuccessful()) {
return view('migration.completed');
}

View File

@ -0,0 +1,156 @@
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Libraries\Utils;
use App\Models\Account;
use App\Models\User;
use App\Services\Migration\CompleteService;
use App\Traits\GenerateMigrationResources;
use Illuminate\Support\Facades\Storage;
use Unirest\Request;
class HostedMigration extends Job
{
use GenerateMigrationResources;
public $db;
public $data;
public $user;
private $v4_secret;
private $migration_token;
public $account;
public function __construct(User $user, array $data, $db)
{
$this->user = $user;
$this->data = $data;
$this->db = $db;
$this->v4_secret = config('ninja.ninja_hosted_secret');
}
/**
* Execute the job.
*/
public function handle()
{
config(['database.default' => $this->db]);
//Create or get a token
$this->getToken();
//build the contents to be posted
$completeService = (new CompleteService($this->migration_token));
$migrationData = $this->generateMigrationData($data);
$completeService->data($migrationData)
->endpoint('https://v5-app1.invoicing.co')
->start();
}
private function getToken()
{
$url = 'https://invoicing.co/api/v1/get_migration_account';
$headers = [
'X-API-HOSTED-SECRET' => $this->v4_secret,
'X-Requested-With' => 'XMLHttpRequest',
'Content-Type' => 'application/json',
];
$body = [
'email' => $this->user->email,
];
$response = Request::post($url, $headers, $body);
if (in_array($response->code, [200])) {
$data = $response->body();
$this->migration_token = $data['token'];
} else {
info("getting token failed");
info($response->raw_body);
}
}
public function generateMigrationData(array $data): array
{
set_time_limit(0);
$migrationData = [];
foreach ($data['companies'] as $company) {
$account = Account::where('account_key', $company['id'])->firstOrFail();
$this->account = $account;
$date = date('Y-m-d');
$accountKey = $this->account->account_key;
$output = fopen('php://output', 'w') or Utils::fatalError();
$fileName = "{$accountKey}-{$date}-invoiceninja";
$localMigrationData['data'] = [
'account' => $this->getAccount(),
'company' => $this->getCompany(),
'users' => $this->getUsers(),
'tax_rates' => $this->getTaxRates(),
'payment_terms' => $this->getPaymentTerms(),
'clients' => $this->getClients(),
'company_gateways' => $this->getCompanyGateways(),
'client_gateway_tokens' => $this->getClientGatewayTokens(),
'vendors' => $this->getVendors(),
'projects' => $this->getProjects(),
'products' => $this->getProducts(),
'credits' => $this->getCreditsNotes(),
'invoices' => $this->getInvoices(),
'recurring_invoices' => $this->getRecurringInvoices(),
'quotes' => $this->getQuotes(),
'payments' => array_merge($this->getPayments(), $this->getCredits()),
'documents' => $this->getDocuments(),
'expense_categories' => $this->getExpenseCategories(),
'task_statuses' => $this->getTaskStatuses(),
'expenses' => $this->getExpenses(),
'tasks' => $this->getTasks(),
'documents' => $this->getDocuments(),
'ninja_tokens' => $this->getNinjaToken(),
];
$localMigrationData['force'] = array_key_exists('force', $company);
Storage::makeDirectory('migrations');
$file = Storage::path("migrations/{$fileName}.zip");
//$file = storage_path("migrations/{$fileName}.zip");
ksort($localMigrationData);
$zip = new \ZipArchive();
$zip->open($file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$zip->addFromString('migration.json', json_encode($localMigrationData, JSON_PRETTY_PRINT));
$zip->close();
$localMigrationData['file'] = $file;
$migrationData[] = $localMigrationData;
}
return $migrationData;
}
}

View File

@ -47,4 +47,6 @@ return [
'subscription_key' => env('MSBOT_LUIS_SUBSCRIPTION_KEY'),
],
'ninja_hosted_secret' => env('NINJA_HOSTED_SECRET', false),
];