From 391df15a965a655623e7e37509b35ede7fe28a43 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 17 Jul 2021 18:58:42 +1000 Subject: [PATCH] Refactor Migration codebase --- .../Controllers/Migration/StepsController.php | 43 +++++++++---------- app/Jobs/HostedMigration.php | 19 ++++---- app/Services/Migration/CompleteService.php | 1 + app/Traits/GenerateMigrationResources.php | 12 ++---- 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/app/Http/Controllers/Migration/StepsController.php b/app/Http/Controllers/Migration/StepsController.php index cba445154ad9..c6bc6cb3110d 100644 --- a/app/Http/Controllers/Migration/StepsController.php +++ b/app/Http/Controllers/Migration/StepsController.php @@ -8,16 +8,17 @@ use App\Http\Requests\MigrationCompaniesRequest; use App\Http\Requests\MigrationEndpointRequest; use App\Http\Requests\MigrationForwardRequest; use App\Http\Requests\MigrationTypeRequest; +use App\Jobs\HostedMigration; use App\Libraries\Utils; use App\Models\Account; use App\Services\Migration\AuthService; use App\Services\Migration\CompanyService; use App\Services\Migration\CompleteService; use App\Traits\GenerateMigrationResources; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; use Validator; -use Illuminate\Http\Request; class StepsController extends BaseController { @@ -48,6 +49,17 @@ class StepsController extends BaseController */ public function start() { + if(Utils::isNinja()){ + + session()->put('MIGRATION_ENDPOINT', 'https://v5-app1.invoicing.co'); + // session()->put('MIGRATION_ENDPOINT', 'http://ninja.test:8000'); + session()->put('MIGRATION_ACCOUNT_TOKEN',''); + session()->put('MIGRAITON_API_SECRET', null); + + return $this->companies(); + + } + return view('migration.start'); } @@ -68,16 +80,13 @@ class StepsController extends BaseController { session()->put('MIGRATION_TYPE', $request->option); - if ($request->option == 0) { + if ($request->option == 0 || $request->option == '0') { - session()->put('MIGRATION_ENDPOINT', 'https://v5-app1.invoicing.co'); - - //refactor here to make this a little more magical - // return redirect( url('/migration/companies?hosted=true') ); + //old // return redirect( // url('/migration/auth') // ); @@ -124,6 +133,7 @@ class StepsController extends BaseController public function endpoint() { + if ($this->shouldGoBack('endpoint')) { return redirect( url($this->access['endpoint']['redirect']) @@ -214,27 +224,14 @@ class StepsController extends BaseController url($this->access['companies']['redirect']) ); } + $bool = true; - if($request->has('hosted') && $request->input('hosted') == 'true') + if(Utils::isNinja()) { - //push a job with $request->all() and the auth()->user() reference; - // - //In that job we will - // - //Create data file - // - //Send along a custom protected route - // - //auth as the end user - //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')); + $this->dispatch(new HostedMigration(auth()->user(), $request->all(), config('database.default'))); - if ($completeService->isSuccessful()) { - return view('migration.completed'); - } + return view('migration.completed'); } diff --git a/app/Jobs/HostedMigration.php b/app/Jobs/HostedMigration.php index 3f14567966a9..e0725dbdf684 100644 --- a/app/Jobs/HostedMigration.php +++ b/app/Jobs/HostedMigration.php @@ -23,9 +23,7 @@ class HostedMigration extends Job private $v4_secret; - private $migration_token; - - public $account; + public $migration_token; public function __construct(User $user, array $data, $db) { @@ -40,18 +38,19 @@ class HostedMigration extends 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); + $migrationData = $this->generateMigrationData($this->data); $completeService->data($migrationData) ->endpoint('https://v5-app1.invoicing.co') + // ->endpoint('http://ninja.test:8000') ->start(); } @@ -59,6 +58,7 @@ class HostedMigration extends Job private function getToken() { $url = 'https://invoicing.co/api/v1/get_migration_account'; + // $url = 'http://ninja.test:8000/api/v1/get_migration_account'; $headers = [ 'X-API-HOSTED-SECRET' => $this->v4_secret, @@ -75,13 +75,15 @@ class HostedMigration extends Job 'password' => '', ]; + $body = \Unirest\Request\Body::json($body); + $response = Request::post($url, $headers, $body); if (in_array($response->code, [200])) { - $data = $response->body(); - - $this->migration_token = $data['token']; + $data = $response->body; + info(print_r($data,1)); + $this->migration_token = $data->token; } else { info("getting token failed"); @@ -89,6 +91,7 @@ class HostedMigration extends Job } + return $this; } diff --git a/app/Services/Migration/CompleteService.php b/app/Services/Migration/CompleteService.php index 140431388aff..171576e66458 100644 --- a/app/Services/Migration/CompleteService.php +++ b/app/Services/Migration/CompleteService.php @@ -40,6 +40,7 @@ class CompleteService public function start() { + $files = []; foreach ($this->data as $companyKey => $companyData) { diff --git a/app/Traits/GenerateMigrationResources.php b/app/Traits/GenerateMigrationResources.php index fcdb04c90bfc..6943c449096d 100644 --- a/app/Traits/GenerateMigrationResources.php +++ b/app/Traits/GenerateMigrationResources.php @@ -363,8 +363,6 @@ info("get company"); private function getClientSettings($client) { - info("get client settings"); - $settings = new \stdClass(); $settings->currency_id = $client->currency_id ? (string) $client->currency_id : (string) $client->account->currency_id; @@ -381,8 +379,7 @@ info("get company"); protected function getClientContacts($client) { - info("get client contacts"); - + $contacts = Contact::where('client_id', $client->id)->withTrashed()->get(); $transformed = []; @@ -995,7 +992,7 @@ info("get company"); public function getResourceInvitations($items, $resourceKeyId) { - info("get resource {$resourceKeyId} invitations"); + // info("get resource {$resourceKeyId} invitations"); $transformed = []; @@ -1068,7 +1065,7 @@ info("get company"); public function getInvoiceItems($items) { - info("get invoice items"); + // info("get invoice items"); $transformed = []; @@ -1394,12 +1391,9 @@ info("get company"); $fees_and_limits = $this->transformFeesAndLimits($gateway_type); -info("generated fees and limits = "); -info(print_r($fees_and_limits,1)); $translated_gateway_type = $this->translateGatewayTypeId($gateway_type); -info("translated gateway_type = {$translated_gateway_type}"); $fees->{$translated_gateway_type} = $fees_and_limits; }