mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2026-04-18 12:10:50 +00:00
Add back migration controller for purge routes
This commit is contained in:
194
app/Http/Controllers/MigrationController.php
Normal file
194
app/Http/Controllers/MigrationController.php
Normal file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2026. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Console\Commands\ImportMigrations;
|
||||
use App\DataMapper\CompanySettings;
|
||||
use App\Jobs\Mail\NinjaMailerJob;
|
||||
use App\Jobs\Mail\NinjaMailerObject;
|
||||
use App\Jobs\Util\StartMigration;
|
||||
use App\Mail\ExistingMigration;
|
||||
use App\Mail\Migration\MaxCompanies;
|
||||
use App\Models\Company;
|
||||
use App\Models\CompanyToken;
|
||||
use App\Utils\Ninja;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class MigrationController extends BaseController
|
||||
{
|
||||
use DispatchesJobs;
|
||||
|
||||
public bool $silent_migration = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge Company.
|
||||
*
|
||||
* @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",
|
||||
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
||||
* @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",
|
||||
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
||||
* @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"),
|
||||
* ),
|
||||
* )
|
||||
* @param Company $company
|
||||
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function purgeCompany(Company $company)
|
||||
{
|
||||
if (Ninja::isHosted() && config('ninja.ninja_default_company_id') == $company->id) {
|
||||
return response()->json(['message' => 'Cannot purge this company'], 400);
|
||||
}
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Company purged'], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge Company but save settings.
|
||||
*
|
||||
* @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",
|
||||
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
||||
* @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",
|
||||
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
||||
* @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"),
|
||||
* ),
|
||||
* )
|
||||
* @param Request $request
|
||||
* @param Company $company
|
||||
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
|
||||
*/
|
||||
public function purgeCompanySaveSettings(Request $request, Company $company)
|
||||
{
|
||||
$company->clients()->forceDelete();
|
||||
$company->products()->forceDelete();
|
||||
$company->projects()->forceDelete();
|
||||
$company->tasks()->forceDelete();
|
||||
$company->vendors()->forceDelete();
|
||||
$company->expenses()->forceDelete();
|
||||
$company->purchase_orders()->forceDelete();
|
||||
$company->bank_transaction_rules()->forceDelete();
|
||||
$company->bank_transactions()->forceDelete();
|
||||
// $company->bank_integrations()->forceDelete();
|
||||
|
||||
$company->all_activities()->forceDelete();
|
||||
|
||||
$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;
|
||||
$settings->purchase_order_number_counter = 1;
|
||||
|
||||
$company->settings = $settings;
|
||||
|
||||
$company->save();
|
||||
|
||||
return response()->json(['message' => 'Settings preserved'], 200);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,6 +55,7 @@ use App\Http\Controllers\LicenseController;
|
||||
use App\Http\Controllers\LocationController;
|
||||
use App\Http\Controllers\LogoutController;
|
||||
use App\Http\Controllers\MailgunController;
|
||||
use App\Http\Controllers\MigrationController;
|
||||
use App\Http\Controllers\OneTimeTokenController;
|
||||
use App\Http\Controllers\PasskeyController;
|
||||
use App\Http\Controllers\PaymentController;
|
||||
|
||||
Reference in New Issue
Block a user