2019-11-20 06:41:49 +01:00
|
|
|
<?php
|
2020-11-11 18:26:17 +01:00
|
|
|
|
2019-11-20 06:41:49 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-11-20 06:41:49 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-11-20 06:41:49 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-11-20 06:41:49 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
use App\Console\Commands\ImportMigrations;
|
2020-03-12 21:38:22 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
2021-02-18 21:57:10 +01:00
|
|
|
use App\Jobs\Mail\NinjaMailerJob;
|
|
|
|
use App\Jobs\Mail\NinjaMailerObject;
|
2020-01-23 21:35:00 +01:00
|
|
|
use App\Jobs\Util\StartMigration;
|
2020-04-14 00:20:54 +02:00
|
|
|
use App\Mail\ExistingMigration;
|
2021-05-04 04:40:28 +02:00
|
|
|
use App\Mail\Migration\MaxCompanies;
|
2020-09-06 11:38:10 +02:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CompanyToken;
|
2021-05-10 01:52:58 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-09-06 11:38:10 +02:00
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
2019-11-20 06:41:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
2021-09-29 13:06:42 +02:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2021-08-25 12:53:13 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
2022-06-21 11:57:17 +02:00
|
|
|
use Illuminate\Support\Str;
|
2019-11-20 06:41:49 +01:00
|
|
|
|
|
|
|
class MigrationController extends BaseController
|
|
|
|
{
|
|
|
|
use DispatchesJobs;
|
|
|
|
|
2023-07-10 04:55:21 +02:00
|
|
|
public bool $silent_migration = false;
|
|
|
|
|
2019-11-20 06:41:49 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Purge Company.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/migration/purge/{company}",
|
|
|
|
* operationId="postPurgeCompany",
|
|
|
|
* tags={"migration"},
|
|
|
|
* summary="Attempts to purge a company record and all its child records",
|
|
|
|
* description="Attempts to purge a company record and all its child records",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="company",
|
|
|
|
* in="path",
|
|
|
|
* description="The Company Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-11-20 06:41:49 +01:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Company $company
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
* @throws \Exception
|
2019-11-20 06:41:49 +01:00
|
|
|
*/
|
|
|
|
public function purgeCompany(Company $company)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted() && config('ninja.ninja_default_company_id') == $company->id) {
|
2021-07-11 02:16:27 +02:00
|
|
|
return response()->json(['message' => 'Cannot purge this company'], 400);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-07-11 02:16:27 +02:00
|
|
|
|
2020-08-13 23:15:15 +02:00
|
|
|
$account = $company->account;
|
|
|
|
$company_id = $company->id;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$company->delete();
|
2019-11-20 06:41:49 +01:00
|
|
|
|
2020-08-13 23:15:15 +02:00
|
|
|
/*Update the new default company if necessary*/
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($company_id == $account->default_company_id && $account->companies->count() >= 1) {
|
2020-08-13 23:15:15 +02:00
|
|
|
$new_default_company = $account->companies->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($new_default_company) {
|
2020-08-13 23:15:15 +02:00
|
|
|
$account->default_company_id = $new_default_company->id;
|
|
|
|
$account->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:44:42 +01:00
|
|
|
return response()->json(['message' => 'Company purged'], 200);
|
2019-11-20 06:41:49 +01:00
|
|
|
}
|
|
|
|
|
2020-11-22 22:25:29 +01:00
|
|
|
private function purgeCompanyWithForceFlag(Company $company)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted() && config('ninja.ninja_default_company_id') == $company->id) {
|
2021-07-11 02:16:27 +02:00
|
|
|
return response()->json(['message' => 'Cannot purge this company'], 400);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-07-11 02:16:27 +02:00
|
|
|
|
2020-11-22 22:25:29 +01:00
|
|
|
$account = $company->account;
|
|
|
|
$company_id = $company->id;
|
|
|
|
|
|
|
|
$company->delete();
|
|
|
|
|
|
|
|
/*Update the new default company if necessary*/
|
|
|
|
if ($company_id == $account->default_company_id && $account->companies->count() >= 1) {
|
|
|
|
$new_default_company = $account->companies->first();
|
|
|
|
|
|
|
|
if ($new_default_company) {
|
|
|
|
$account->default_company_id = $new_default_company->id;
|
|
|
|
$account->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-23 02:46:16 +01:00
|
|
|
|
2019-11-20 06:41:49 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Purge Company but save settings.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/migration/purge_save_settings/{company}",
|
|
|
|
* operationId="postPurgeCompanySaveSettings",
|
|
|
|
* tags={"migration"},
|
|
|
|
* summary="Attempts to purge a companies child records but save the company record and its settings",
|
|
|
|
* description="Attempts to purge a companies child records but save the company record and its settings",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="company",
|
|
|
|
* in="path",
|
|
|
|
* description="The Company Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-11-20 06:41:49 +01:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Company $company
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2019-11-20 06:41:49 +01:00
|
|
|
*/
|
2020-08-14 02:40:59 +02:00
|
|
|
public function purgeCompanySaveSettings(Request $request, Company $company)
|
2019-11-20 06:41:49 +01:00
|
|
|
{
|
2020-08-24 00:20:57 +02:00
|
|
|
$company->clients()->forceDelete();
|
|
|
|
$company->products()->forceDelete();
|
2021-04-20 23:35:54 +02:00
|
|
|
$company->projects()->forceDelete();
|
|
|
|
$company->tasks()->forceDelete();
|
|
|
|
$company->vendors()->forceDelete();
|
|
|
|
$company->expenses()->forceDelete();
|
2023-02-05 05:22:44 +01:00
|
|
|
$company->purchase_orders()->forceDelete();
|
2022-11-24 04:33:18 +01:00
|
|
|
$company->bank_transaction_rules()->forceDelete();
|
2022-11-23 12:28:22 +01:00
|
|
|
$company->bank_transactions()->forceDelete();
|
2022-12-01 20:59:16 +01:00
|
|
|
// $company->bank_integrations()->forceDelete();
|
2022-11-23 12:28:22 +01:00
|
|
|
|
2022-07-21 00:28:34 +02:00
|
|
|
$company->all_activities()->forceDelete();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-10-20 01:52:51 +02:00
|
|
|
$settings = $company->settings;
|
|
|
|
|
|
|
|
/* Reset all counters to 1 after a purge */
|
|
|
|
$settings->recurring_invoice_number_counter = 1;
|
|
|
|
$settings->invoice_number_counter = 1;
|
|
|
|
$settings->quote_number_counter = 1;
|
|
|
|
$settings->client_number_counter = 1;
|
|
|
|
$settings->credit_number_counter = 1;
|
|
|
|
$settings->task_number_counter = 1;
|
|
|
|
$settings->expense_number_counter = 1;
|
|
|
|
$settings->recurring_expense_number_counter = 1;
|
|
|
|
$settings->recurring_quote_number_counter = 1;
|
|
|
|
$settings->vendor_number_counter = 1;
|
|
|
|
$settings->ticket_number_counter = 1;
|
|
|
|
$settings->payment_number_counter = 1;
|
|
|
|
$settings->project_number_counter = 1;
|
2022-07-19 01:39:54 +02:00
|
|
|
$settings->purchase_order_number_counter = 1;
|
2021-10-20 01:52:51 +02:00
|
|
|
|
|
|
|
$company->settings = $settings;
|
|
|
|
|
2019-11-20 21:55:16 +01:00
|
|
|
$company->save();
|
2019-11-20 06:41:49 +01:00
|
|
|
|
2020-03-03 23:44:42 +01:00
|
|
|
return response()->json(['message' => 'Settings preserved'], 200);
|
2019-11-20 06:41:49 +01:00
|
|
|
}
|
2020-01-23 21:35:00 +01:00
|
|
|
|
2020-01-27 21:56:48 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Start the migration from V1.
|
2020-01-27 21:56:48 +01:00
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/migration/start",
|
|
|
|
* operationId="postStartMigration",
|
|
|
|
* tags={"migration"},
|
|
|
|
* summary="Starts the migration from previous version of Invoice Ninja",
|
|
|
|
* description="Starts the migration from previous version of Invoice Ninja",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-01-27 21:56:48 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-PASSWORD"),
|
2020-01-27 21:56:48 +01:00
|
|
|
* @OA\Parameter(
|
|
|
|
* name="migration",
|
2020-07-30 05:27:00 +02:00
|
|
|
* in="query",
|
2020-01-27 21:56:48 +01:00
|
|
|
* description="The migraton file",
|
|
|
|
* example="migration.zip",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
2020-02-12 10:18:56 +01:00
|
|
|
* type="object",
|
2020-01-27 21:56:48 +01:00
|
|
|
* format="file",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-27 21:56:48 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse|void
|
2020-01-27 21:56:48 +01:00
|
|
|
*/
|
2020-11-11 18:26:17 +01:00
|
|
|
public function startMigration(Request $request)
|
2022-06-21 11:57:17 +02:00
|
|
|
{
|
|
|
|
nlog('Starting Migration');
|
2021-09-29 13:06:42 +02:00
|
|
|
|
2023-07-10 04:55:21 +02:00
|
|
|
if($request->has('silent_migration'))
|
|
|
|
$this->silent_migration = true;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($request->companies) {
|
2021-10-06 00:51:03 +02:00
|
|
|
//handle Laravel 5.5 UniHTTP
|
2022-06-21 11:57:17 +02:00
|
|
|
$companies = json_decode($request->companies, 1);
|
|
|
|
} else {
|
2021-10-06 00:51:03 +02:00
|
|
|
//handle Laravel 6 Guzzle
|
|
|
|
$companies = [];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach ($request->all() as $input) {
|
|
|
|
if ($input instanceof UploadedFile) {
|
|
|
|
} else {
|
|
|
|
$companies[] = json_decode($input, 1);
|
2022-02-19 06:11:30 +01:00
|
|
|
}
|
2021-10-06 00:51:03 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 12:29:58 +02:00
|
|
|
|
2020-04-29 15:24:52 +02:00
|
|
|
if (app()->environment() === 'local') {
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
|
|
|
return response()->json([
|
|
|
|
'_id' => Str::uuid(),
|
|
|
|
'method' => config('queue.default'),
|
|
|
|
'started_at' => now(),
|
|
|
|
], 200);
|
|
|
|
} finally {
|
|
|
|
// Controller logic here
|
|
|
|
|
|
|
|
foreach ($companies as $company) {
|
|
|
|
if (! is_array($company)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-04 04:40:28 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$company = (array) $company;
|
2021-05-04 04:40:28 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$user = auth()->user();
|
2020-04-14 00:20:54 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$company_count = $user->account->companies()->count();
|
2023-08-11 06:18:58 +02:00
|
|
|
$fresh_company = false;
|
2021-09-29 13:06:42 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
// Look for possible existing company (based on company keys).
|
|
|
|
$existing_company = Company::whereRaw('BINARY `company_key` = ?', [$company['company_key']])->first();
|
2020-04-14 00:20:54 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
$t = app('translator');
|
|
|
|
$t->replace(Ninja::transformTranslations($user->account->companies()->first()->settings));
|
|
|
|
App::setLocale($user->account->companies()->first()->getLocale());
|
2020-04-30 00:01:44 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $existing_company && $company_count >= 10) {
|
|
|
|
$nmo = new NinjaMailerObject;
|
|
|
|
$nmo->mailable = new MaxCompanies($user->account->companies()->first());
|
|
|
|
$nmo->company = $user->account->companies()->first();
|
|
|
|
$nmo->settings = $user->account->companies()->first()->settings;
|
|
|
|
$nmo->to_user = $user;
|
2023-07-10 04:55:21 +02:00
|
|
|
|
|
|
|
if(!$this->silent_migration)
|
|
|
|
NinjaMailerJob::dispatch($nmo, true);
|
2021-05-04 04:40:28 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return;
|
|
|
|
} elseif ($existing_company && $company_count > 10) {
|
|
|
|
$nmo = new NinjaMailerObject;
|
|
|
|
$nmo->mailable = new MaxCompanies($user->account->companies()->first());
|
|
|
|
$nmo->company = $user->account->companies()->first();
|
|
|
|
$nmo->settings = $user->account->companies()->first()->settings;
|
|
|
|
$nmo->to_user = $user;
|
2023-07-10 04:55:21 +02:00
|
|
|
|
|
|
|
if(!$this->silent_migration)
|
|
|
|
NinjaMailerJob::dispatch($nmo, true);
|
2020-04-30 00:01:44 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-08-25 12:53:13 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$checks = [
|
|
|
|
'existing_company' => $existing_company ? (bool) 1 : false,
|
|
|
|
'force' => array_key_exists('force', $company) ? (bool) $company['force'] : false,
|
|
|
|
];
|
2021-05-04 04:40:28 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
// If there's existing company and ** no ** force is provided - skip migration.
|
|
|
|
if ($checks['existing_company'] == true && $checks['force'] == false) {
|
|
|
|
nlog('Migrating: Existing company without force. (CASE_01)');
|
2020-01-27 21:56:48 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$nmo = new NinjaMailerObject;
|
|
|
|
$nmo->mailable = new ExistingMigration($existing_company);
|
|
|
|
$nmo->company = $user->account->companies()->first();
|
|
|
|
$nmo->settings = $user->account->companies()->first();
|
|
|
|
$nmo->to_user = $user;
|
2020-03-12 21:38:22 +01:00
|
|
|
|
2023-07-10 04:55:21 +02:00
|
|
|
if(!$this->silent_migration)
|
|
|
|
NinjaMailerJob::dispatch($nmo, true);
|
2020-04-14 00:20:54 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return response()->json([
|
|
|
|
'_id' => Str::uuid(),
|
|
|
|
'method' => config('queue.default'),
|
|
|
|
'started_at' => now(),
|
|
|
|
], 200);
|
|
|
|
}
|
2020-11-11 18:26:17 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
// If there's existing company and force ** is provided ** - purge the company and migrate again.
|
|
|
|
if ($checks['existing_company'] == true && $checks['force'] == true) {
|
|
|
|
nlog('purging the existing company here');
|
|
|
|
$this->purgeCompanyWithForceFlag($existing_company);
|
|
|
|
|
|
|
|
$account = auth()->user()->account;
|
|
|
|
$fresh_company = (new ImportMigrations())->getCompany($account);
|
|
|
|
$fresh_company->is_disabled = true;
|
|
|
|
$fresh_company->save();
|
|
|
|
|
|
|
|
$account->default_company_id = $fresh_company->id;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
$fresh_company_token = new CompanyToken();
|
|
|
|
$fresh_company_token->user_id = $user->id;
|
|
|
|
$fresh_company_token->company_id = $fresh_company->id;
|
|
|
|
$fresh_company_token->account_id = $account->id;
|
|
|
|
$fresh_company_token->name = $request->token_name ?? Str::random(12);
|
|
|
|
$fresh_company_token->token = $request->token ?? Str::random(64);
|
|
|
|
$fresh_company_token->is_system = true;
|
|
|
|
$fresh_company_token->save();
|
|
|
|
|
|
|
|
$user->companies()->attach($fresh_company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'is_locked' => 0,
|
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
|
|
|
'permissions' => '',
|
|
|
|
'settings' => null,
|
|
|
|
]);
|
|
|
|
}
|
2020-04-14 00:20:54 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
// If there's no existing company migrate just normally.
|
|
|
|
if ($checks['existing_company'] == false) {
|
|
|
|
nlog('creating fresh company');
|
|
|
|
|
|
|
|
$account = auth()->user()->account;
|
|
|
|
$fresh_company = (new ImportMigrations())->getCompany($account);
|
|
|
|
|
|
|
|
$fresh_company->is_disabled = true;
|
|
|
|
$fresh_company->save();
|
|
|
|
|
|
|
|
$fresh_company_token = new CompanyToken();
|
|
|
|
$fresh_company_token->user_id = $user->id;
|
|
|
|
$fresh_company_token->company_id = $fresh_company->id;
|
|
|
|
$fresh_company_token->account_id = $account->id;
|
|
|
|
$fresh_company_token->name = $request->token_name ?? Str::random(12);
|
|
|
|
$fresh_company_token->token = $request->token ?? Str::random(64);
|
|
|
|
$fresh_company_token->is_system = true;
|
|
|
|
|
|
|
|
$fresh_company_token->save();
|
|
|
|
|
|
|
|
$user->companies()->attach($fresh_company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'is_locked' => 0,
|
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
|
|
|
'permissions' => '',
|
|
|
|
'settings' => null,
|
|
|
|
]);
|
|
|
|
}
|
2020-04-30 00:01:44 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$migration_file = $request->file($company['company_index'])
|
2020-11-11 18:26:17 +01:00
|
|
|
->storeAs(
|
|
|
|
'migrations',
|
2021-09-29 13:06:42 +02:00
|
|
|
$request->file($company['company_index'])->getClientOriginalName(),
|
2020-11-29 23:58:31 +01:00
|
|
|
'public'
|
2020-11-11 18:26:17 +01:00
|
|
|
);
|
2020-04-14 00:20:54 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (app()->environment() == 'testing') {
|
|
|
|
nlog('environment is testing = bailing out now');
|
2020-03-03 23:44:42 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nlog('starting migration job');
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog($migration_file);
|
2021-05-10 01:52:58 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted()) {
|
2023-07-10 04:55:21 +02:00
|
|
|
StartMigration::dispatch($migration_file, $user, $fresh_company, $this->silent_migration)->onQueue('migration');
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2023-07-10 04:55:21 +02:00
|
|
|
StartMigration::dispatch($migration_file, $user, $fresh_company, $this->silent_migration);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-08 01:37:21 +02:00
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'_id' => Str::uuid(),
|
|
|
|
'method' => config('queue.default'),
|
|
|
|
'started_at' => now(),
|
|
|
|
], 200);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2023-06-08 01:37:21 +02:00
|
|
|
|
2020-01-23 21:35:00 +01:00
|
|
|
}
|
2019-11-20 06:41:49 +01:00
|
|
|
}
|