2021-07-17 07:58:37 +02:00
< ? php
/**
* Invoice Ninja ( https :// invoiceninja . com ) .
*
* @ 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 )
2021-07-17 07:58:37 +02:00
*
* @ license https :// www . elastic . co / licensing / elastic - license
*/
namespace App\Http\Controllers ;
use App\Jobs\Account\CreateAccount ;
use App\Libraries\MultiDB ;
2021-11-08 03:05:54 +01:00
use App\Models\Company ;
2021-07-17 07:58:37 +02:00
use App\Models\CompanyToken ;
use Illuminate\Http\Request ;
class HostedMigrationController extends Controller
{
2024-01-14 05:17:15 +01:00
public function checkStatus ( Request $request )
{
2024-02-13 05:25:18 +01:00
2024-01-14 05:17:15 +01:00
if ( $request -> header ( 'X-API-HOSTED-SECRET' ) != config ( 'ninja.ninja_hosted_secret' )) {
return ;
}
MultiDB :: findAndSetDbByCompanyKey ( $request -> company_key );
$c = Company :: where ( 'company_key' , $request -> company_key ) -> first ();
2024-02-13 05:25:18 +01:00
if ( ! $c || $c -> is_disabled ) {
2024-01-14 05:17:15 +01:00
return response () -> json ([ 'message' => 'ok' ], 200 );
2024-02-13 05:25:18 +01:00
}
2024-01-14 05:17:15 +01:00
2024-01-17 01:43:45 +01:00
// if(\App\Models\Invoice::query()->where('company_id', $c->id)->where('created_at', '>', now()->subMonths(2))->first())
// return response()->json(['message' => 'New data exists, are you sure? Please log in here https://app.invoicing.co and delete the company if you really need to migrate again.'], 400);
2024-02-13 05:25:18 +01:00
// if(\App\Models\Client::query()->where('company_id', $c->id)->where('created_at', '>', now()->subMonths(2))->first())
2024-01-17 01:43:45 +01:00
// return response()->json(['message' => 'New data exists, are you sure? Please log in here https://app.invoicing.co and delete the company if you really need to migrate again.'], 400);
2024-02-13 05:25:18 +01:00
// if(\App\Models\Quote::query()->where('company_id', $c->id)->where('created_at', '>', now()->subMonths(2)))
2024-01-17 01:43:45 +01:00
// return response()->json(['message' => 'New data exists, are you sure? Please log in here https://app.invoicing.co and delete the company if you really need to migrate again.'], 400);
2024-01-14 05:17:15 +01:00
2024-02-13 05:25:18 +01:00
// if(\App\Models\RecurringInvoice::query()->where('company_id', $c->id)->where('created_at', '>', now()->subMonths(2)))
2024-01-17 01:43:45 +01:00
// return response()->json(['message' => 'New data exists, are you sure? Please log in here https://app.invoicing.co and delete the company if you really need to migrate again.'], 400);
2024-01-14 05:17:15 +01:00
2024-01-17 01:43:45 +01:00
return response () -> json ([ 'message' => 'You have already activated this company on v5!!!!!! This migration may be a BAD idea. Contact us contact@invoiceninja.com to confirm this action.' ], 400 );
2024-01-14 05:17:15 +01:00
}
2021-07-17 07:58:37 +02:00
public function getAccount ( Request $request )
{
2022-06-21 11:57:17 +02:00
if ( $request -> header ( 'X-API-HOSTED-SECRET' ) != config ( 'ninja.ninja_hosted_secret' )) {
2021-07-17 07:58:37 +02:00
return ;
2022-06-21 11:57:17 +02:00
}
2021-07-17 07:58:37 +02:00
2022-06-21 11:57:17 +02:00
if ( $user = MultiDB :: hasUser ([ 'email' => $request -> input ( 'email' )])) {
if ( $user -> account -> owner () && $user -> account -> companies () -> count () >= 1 ) {
return response () -> json ([ 'token' => $user -> account -> companies -> first () -> tokens -> first () -> token ], 200 );
2021-07-17 07:58:37 +02:00
}
2021-07-17 09:38:59 +02:00
return response () -> json ([ 'error' => 'This user is not able to perform a migration. Please contact us at contact@invoiceninja.com to discuss.' ], 401 );
2021-07-17 07:58:37 +02:00
}
2022-06-24 13:15:14 +02:00
$account = ( new CreateAccount ( $request -> all (), $request -> getClientIp ())) -> handle ();
2022-01-28 06:30:40 +01:00
$account -> hosted_client_count = 100 ;
$account -> hosted_company_count = 10 ;
2023-05-05 05:16:34 +02:00
$account -> created_at = now () -> subYears ( 2 );
2022-01-28 06:30:40 +01:00
$account -> save ();
2022-06-21 11:57:17 +02:00
2023-04-02 13:42:14 +02:00
MultiDB :: findAndSetDbByAccountKey ( $account -> key );
2021-07-17 07:58:37 +02:00
$company = $account -> companies -> first ();
2023-08-04 09:12:21 +02:00
/** @var \App\Models\CompanyToken $company_token **/
2021-07-17 07:58:37 +02:00
$company_token = CompanyToken :: where ( 'user_id' , auth () -> user () -> id )
2022-06-24 13:15:14 +02:00
-> where ( 'company_id' , $company -> id )
-> first ();
2021-07-17 07:58:37 +02:00
return response () -> json ([ 'token' => $company_token -> token ], 200 );
}
2021-11-08 00:17:49 +01:00
public function confirmForwarding ( Request $request )
{
2022-06-21 11:57:17 +02:00
if ( $request -> header ( 'X-API-HOSTED-SECRET' ) != config ( 'ninja.ninja_hosted_secret' )) {
2021-11-08 00:17:49 +01:00
return ;
2022-06-21 11:57:17 +02:00
}
2021-11-08 00:17:49 +01:00
2021-11-08 03:05:54 +01:00
$input = $request -> all ();
2021-11-21 03:49:52 +01:00
2021-11-08 03:05:54 +01:00
MultiDB :: findAndSetDbByCompanyKey ( $input [ 'account_key' ]);
2023-08-04 09:12:21 +02:00
/** @var \App\Models\Company $company **/
2021-11-08 03:05:54 +01:00
$company = Company :: with ( 'account' ) -> where ( 'company_key' , $input [ 'account_key' ]) -> first ();
2021-11-21 02:57:27 +01:00
$forward_url = $company -> domain ();
2022-06-21 11:57:17 +02:00
2022-06-24 13:15:14 +02:00
$billing_transferred = ( new \Modules\Admin\Jobs\Account\TransferAccountPlan ( $input )) -> handle ();
2021-11-08 03:05:54 +01:00
2021-11-21 02:57:27 +01:00
return response () -> json ([ 'forward_url' => $forward_url , 'billing_transferred' => $billing_transferred ], 200 );
2021-11-08 00:17:49 +01:00
}
2022-06-21 11:57:17 +02:00
}