- добавил авто-создание токенов в табл bx_id

- убрал политику на создание организации
This commit is contained in:
Thekindbull 2025-11-12 06:43:00 +08:00
parent 14675465c4
commit 4a9f232723
6 changed files with 67 additions and 42 deletions

View File

@ -27,10 +27,6 @@ public function complex()
{ {
return $this->belongsTo(Complex::class); return $this->belongsTo(Complex::class);
} }
public function user()
{
return $this->belongsTo(User::class, 'client_id');
}
public function agent() public function agent()
{ {
return $this->belongsTo(Agent::class, 'agent_id'); return $this->belongsTo(Agent::class, 'agent_id');

View File

@ -0,0 +1,27 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('bx_ids', function (Blueprint $table)
{
$table->string('token')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('bx_ids');
}
};

View File

@ -13,6 +13,11 @@ class ContractUpdateController
{ {
public function __invoke(Deal $deal, Request $request) public function __invoke(Deal $deal, Request $request)
{ {
$companyToken = $deal->agent->company->secret;
if ($request->token != $companyToken)
{
return false;
}
$status = $status =
Contract::updateOrCreate( Contract::updateOrCreate(
['deal_id' => $deal->id], ['deal_id' => $deal->id],

View File

@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Hash;
use Modules\Main\Models\Deal\Deal; use Modules\Main\Models\Deal\Deal;
use Modules\Main\Models\Deal\Client; use Modules\Main\Models\Deal\Client;
@ -35,56 +36,49 @@ protected static function booted()
{ {
return; return;
} }
$object = false;
$sender = new BitrixSender();
switch ( $bitrixId->bitrixable_type ) switch ( $bitrixId->bitrixable_type )
{ {
case Deal::class: case Deal::class:
$deal = Deal::findOrFail($bitrixId->bitrixable_id); $deal = Deal::findOrFail($bitrixId->bitrixable_id);
$sender = new SendDeal($deal); $sender = new SendDeal($deal);
$result = $sender->send(); $object = $deal;
if ($result == true)
{
$id = $sender->resultData['id'];
$bitrixId->bx_id = $id;
return;
}
break; break;
case Agent::class: case Agent::class:
$agent = Agent::findOrFail($bitrixId->bitrixable_id); $agent = Agent::findOrFail($bitrixId->bitrixable_id);
$sender = new SendAgent($agent); $sender = new SendAgent($agent);
$result = $sender->send();
if ($result == true)
{
$id = $sender->resultData['id'];
$bitrixId->bx_id = $id;
return;
};
break; break;
case DealClients::class: case DealClients::class:
$dealClient = DealClients::findOrFail($bitrixId->bitrixable_id); $dealClient = DealClients::findOrFail($bitrixId->bitrixable_id);
$client = $dealClient->client; $client = $dealClient->client;
$sender = new SendClient($client); $sender = new SendClient($client);
$result = $sender->send(); $bitrixId->bitrixable_type = Client::class;
if ($result) $bitrixId->bitrixable_id = $client->id;
{ $object = $client;
$id = $sender->resultData['id'];
$bitrixId->bx_id = $id;
$bitrixId->bitrixable_type = Client::class;
$bitrixId->bitrixable_id = $client->id;
return;
};
break; break;
case Company::class: case Company::class:
$company = Company::findOrFail($bitrixId->bitrixable_id); $company = Company::findOrFail($bitrixId->bitrixable_id);
$sender = new SendCompany($company); $sender = new SendCompany($company);
$result = $sender->send(); $object = $company;
if ($result == true)
{
$id = $sender->resultData['id'];
$bitrixId->bx_id = $id;
return;
};
break; break;
} }
if ($sender)
{
$bitrixId->token = Hash::make(json_encode([$object]));
$sender->setCallbackUrl(
route('api.company.confirm', [
'token' => $bitrixId->token
])
);
$result = $sender->send();
if ($result)
{
$id = $sender->resultData['id'];
$bitrixId->bx_id = $id;
return;
};
}
throw new \Exception('Error of bitrix identifier getter for ' . $bitrixId->bitrixable_type . ' with id ' . $bitrixId->bitrixable_id); throw new \Exception('Error of bitrix identifier getter for ' . $bitrixId->bitrixable_type . ' with id ' . $bitrixId->bitrixable_id);
}); });
static::created(function (BitrixId $bitrixId) static::created(function (BitrixId $bitrixId)

View File

@ -8,13 +8,17 @@
class BitrixSender class BitrixSender
{ {
private $callbackUrl = false;
protected $data; protected $data;
public $resultData; public $resultData;
public function __construct(array $data) public function __construct()
{ {
$this->data = $data;
}
}
public function setCallbackUrl($callbackUrl)
{
$this->callbackUrl = $callbackUrl;
}
protected function getUrl() protected function getUrl()
{ {
$webhookEnumItem = false; $webhookEnumItem = false;
@ -51,6 +55,10 @@ protected function getUrl()
} }
public function send() public function send()
{ {
if ($this->callbackUrl)
{
$this->data['callbackUrl'] = $this->callbackUrl;
}
$postdata = http_build_query( $postdata = http_build_query(
$this->data $this->data
); );

View File

@ -16,11 +16,6 @@ class CreateCompanyController extends Controller
*/ */
public function __invoke(Request $request) public function __invoke(Request $request)
{ {
if ($request->user()->cannot('create', Company::class))
{
abort(403, 'Unauthorized action');
}
$company = false; $company = false;
$request->enum('type', CompanyType::class); $request->enum('type', CompanyType::class);
$validated = $request->validate([ $validated = $request->validate([