2018-10-24 05:50:15 +02:00
|
|
|
<?php
|
2020-03-21 06:37:30 +01:00
|
|
|
namespace App\Jobs\Account;
|
|
|
|
|
|
|
|
use App\Events\Account\AccountCreated;
|
|
|
|
use App\Jobs\Company\CreateCompany;
|
2020-05-27 01:49:06 +02:00
|
|
|
use App\Jobs\Company\CreateCompanyPaymentTerms;
|
2020-03-21 06:37:30 +01:00
|
|
|
use App\Jobs\Company\CreateCompanyToken;
|
|
|
|
use App\Jobs\User\CreateUser;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Notifications\Ninja\NewAccountCreated;
|
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
2020-03-25 03:50:08 +01:00
|
|
|
use Illuminate\Support\Str;
|
2020-03-21 06:37:30 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
class CreateAccount
|
|
|
|
{
|
|
|
|
use Dispatchable;
|
2020-03-30 08:40:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
protected $request;
|
2020-03-30 08:40:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function __construct(array $sp660339)
|
|
|
|
{
|
|
|
|
$this->request = $sp660339;
|
|
|
|
}
|
2020-03-30 08:40:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function handle()
|
|
|
|
{
|
2020-04-04 12:32:42 +02:00
|
|
|
if (config('ninja.environment') == 'selfhost' && Account::all()->count() == 0) {
|
2020-03-31 13:52:21 +02:00
|
|
|
return $this->create();
|
2020-04-04 12:32:42 +02:00
|
|
|
} elseif (config('ninja.environment') == 'selfhost' && Account::all()->count() > 1) {
|
|
|
|
return response()->json(array('message' => Ninja::selfHostedMessage()), 400);
|
2020-03-28 12:34:04 +01:00
|
|
|
} elseif (!Ninja::boot()) {
|
2020-04-04 12:32:42 +02:00
|
|
|
return response()->json(array('message' => Ninja::parse()), 401);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-30 08:40:21 +02:00
|
|
|
|
2020-03-31 13:52:21 +02:00
|
|
|
return $this->create();
|
2020-03-30 08:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function create()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$sp794f3f = Account::create($this->request);
|
2020-03-25 03:50:08 +01:00
|
|
|
$sp794f3f->referral_code = Str::random(32);
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
if (!$sp794f3f->key) {
|
2020-03-29 14:22:14 +02:00
|
|
|
$sp794f3f->key = Str::random(32);
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-25 03:50:08 +01:00
|
|
|
$sp794f3f->save();
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$sp035a66 = CreateCompany::dispatchNow($this->request, $sp794f3f);
|
|
|
|
$sp035a66->load('account');
|
|
|
|
$sp794f3f->default_company_id = $sp035a66->id;
|
|
|
|
$sp794f3f->save();
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spaa9f78 = CreateUser::dispatchNow($this->request, $sp794f3f, $sp035a66, true);
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-05-27 01:49:06 +02:00
|
|
|
CreateCompanyPaymentTerms::dispatchNow($sp035a66, $spaa9f78);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($spaa9f78) {
|
|
|
|
auth()->login($spaa9f78, false);
|
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spaa9f78->setCompany($sp035a66);
|
|
|
|
$spafe62e = isset($this->request['token_name']) ? $this->request['token_name'] : request()->server('HTTP_USER_AGENT');
|
|
|
|
$sp2d97e8 = CreateCompanyToken::dispatchNow($sp035a66, $spaa9f78, $spafe62e);
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($spaa9f78) {
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new AccountCreated($spaa9f78, $sp035a66, Ninja::eventVars()));
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spaa9f78->fresh();
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$sp035a66->notification(new NewAccountCreated($spaa9f78, $sp035a66))->ninja();
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $sp794f3f;
|
|
|
|
}
|
|
|
|
}
|