diff --git a/app/Http/Controllers/Migration/StepsController.php b/app/Http/Controllers/Migration/StepsController.php index fe0541fd9180..cba445154ad9 100644 --- a/app/Http/Controllers/Migration/StepsController.php +++ b/app/Http/Controllers/Migration/StepsController.php @@ -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'); } diff --git a/app/Jobs/HostedMigration.php b/app/Jobs/HostedMigration.php new file mode 100644 index 000000000000..eed4521a3177 --- /dev/null +++ b/app/Jobs/HostedMigration.php @@ -0,0 +1,156 @@ +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; + + } +} \ No newline at end of file diff --git a/config/ninja.php b/config/ninja.php index a585e50b7314..aeeece9de470 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -47,4 +47,6 @@ return [ 'subscription_key' => env('MSBOT_LUIS_SUBSCRIPTION_KEY'), ], + 'ninja_hosted_secret' => env('NINJA_HOSTED_SECRET', false), + ];