Bozhidar Slaveykov há 1 ano atrás
pai
commit
9026194cd4

+ 8 - 1
web/app/ApiSDK/PhyreApiSDK.php

@@ -10,7 +10,7 @@ class PhyreApiSDK
     public $password;
     public $username;
 
-    public function __construct($ip, $port, $password, $username)
+    public function __construct($ip, $port, $username, $password)
     {
         $this->ip = $ip;
         $this->port = $port;
@@ -18,6 +18,13 @@ class PhyreApiSDK
         $this->username = $username;
     }
 
+    public function createCustomer($data)
+    {
+        $response = $this->sendRequest('customers', $data, 'POST');
+
+        return $response;
+    }
+
     public function healthCheck()
     {
         $response = $this->sendRequest('health', [], 'GET');

+ 1 - 1
web/app/Filament/Resources/CustomerResource.php

@@ -32,7 +32,7 @@ class CustomerResource extends Resource
 
         $schema = [];
         if ($getPhyreServers->count() > 0) {
-            $schema[] = Forms\Components\Select::make('server')
+            $schema[] = Forms\Components\Select::make('phyre_server_id')
                 ->options($phyreServers)
                 ->default(0)
                 ->columnSpanFull();

+ 1 - 0
web/app/Filament/Resources/PhyreServerResource.php

@@ -40,6 +40,7 @@ class PhyreServerResource extends Resource
 
                 Forms\Components\TextInput::make('port')
                     ->label('Port')
+                    ->default('22')
                     ->placeholder('Enter the port of the server')
                     ->required(),
 

+ 33 - 0
web/app/Models/Customer.php

@@ -2,6 +2,7 @@
 
 namespace App\Models;
 
+use App\ApiSDK\PhyreApiSDK;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 
@@ -10,6 +11,7 @@ class Customer extends Model
     use HasFactory;
 
     protected $fillable = [
+        'phyre_server_id',
         'name',
         'username',
         'password',
@@ -23,6 +25,37 @@ class Customer extends Model
         'company',
     ];
 
+    public static function boot()
+    {
+        parent::boot();
+
+        static::creating(function ($model) {
+            if ($model->phyre_server_id > 0) {
+                $phyreServer = PhyreServer::where('id', $model->phyre_server_id)->first();
+                if ($phyreServer) {
+                    $phyreApiSDK = new PhyreApiSDK($phyreServer->ip, 8443, $phyreServer->username, $phyreServer->password);
+                    $createCustomer = $phyreApiSDK->createCustomer([
+                        'name' => $model->name,
+                        'email' => $model->email
+                    ]);
+                    if (isset($createCustomer['data']['customer']['id'])) {
+                        $model->external_id = $createCustomer['data']['customer']['id'];
+                    } else {
+                        return false;
+                    }
+
+                } else {
+                    return false;
+                }
+            }
+        });
+
+        static::deleting(function ($model) {
+
+        });
+
+    }
+
     public function hostingSubscriptions()
     {
         return $this->hasMany(HostingSubscription::class);

+ 1 - 0
web/app/Models/HostingSubscription.php

@@ -10,6 +10,7 @@ class HostingSubscription extends Model
     use HasFactory;
 
     protected $fillable = [
+        'phyre_server_id',
         'domain',
         'customer_id',
         'hosting_plan_id',

+ 1 - 1
web/app/Models/PhyreServer.php

@@ -32,7 +32,7 @@ class PhyreServer extends Model
     public function healthCheck()
     {
         try {
-            $phyreApiSDK = new PhyreApiSDK($this->ip, 8443, $this->password, $this->username);
+            $phyreApiSDK = new PhyreApiSDK($this->ip, 8443, $this->username, $this->password);
             $response = $phyreApiSDK->healthCheck();
             if (isset($response['status']) && $response['status'] == 'ok') {
                 $this->status = 'Online';

+ 2 - 0
web/database/migrations/2024_04_02_181018_create_customers_table.php

@@ -14,6 +14,8 @@ return new class extends Migration
         Schema::create('customers', function (Blueprint $table) {
             $table->id();
 
+            $table->integer('phyre_server_id')->nullable();
+            $table->integer('external_id')->nullable();
             $table->string('name')->nullable();
             $table->string('email')->nullable()->unique();
             $table->string('username')->nullable()->unique();