Files
portal-api/app/Jobs/Hosting/HostingJobs.php
2025-07-24 16:18:27 +07:00

116 lines
3.2 KiB
PHP

<?php
namespace App\Jobs\Hosting;
use App\Http\Facade\SignRSA;
use App\Http\Services\Hosting;
use App\Models\ModuleHosting;
use App\Models\ModuleHostingServer;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Throwable;
class HostingJobs implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected int $product;
protected string $action;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($product = 0, $action = '')
{
$this->product = $product;
$this->action = $action;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Hosting $hosting)
{
trans();
$product = ModuleHosting::with('Server', 'Customer')->find($this->product);
if (!$product) {
throw new \Exception("Can't find product");
}
if (!$product->Server) {
throw new \Exception('Invalid request.');
}
$hosting->url = $product->Server->name;
if ($product->Server->apikey) {
$hosting->apiKey = $product->Server->apikey;
}
if ($product->Server->apikey) {
$hosting->apiKey = $product->Server->apikey;
}
$ClientUser = $hosting->CheckUserClient($product->Customer);
if ($ClientUser === "Login_Again") {
$hosting->username = $product->Server->user;
$hosting->password = SignRSA::DecryptFunc($product->Server->password);
$AuthLogin = $hosting->LoginServer();
if ($AuthLogin['error']) {
throw new \Exception("You have entered incorrect username or password.");
}
$hosting->apiKey = $AuthLogin['data']['key'];
ModuleHostingServer::where('id', $product->Server->id)->update(['apikey' => $AuthLogin['data']['key']]);
$ClientUser = $hosting->CheckUserClient($product->Customer);
}
if ($ClientUser['error'] === true) {
throw new \Exception('Invalid request.');
}
$statusAction = null;
switch ($this->action) {
case "Suspend":
$statusAction = $hosting->SuspendAccount($ClientUser['data']['0']['id']);
break;
case "Activate":
$statusAction = $hosting->ActivateAccount($ClientUser['data']['0']['id']);
break;
default:
break;
}
if (isset($statusAction['error'])) {
throw new \Exception($statusAction['data']);
}
}
public function failed(Throwable $exception): void
{
Log::channel('Custom')->info("\r\n");
Log::channel('Custom')->info("=================================");
Log::channel('Custom')->info("Thông báo lỗi lúc: " .Carbon::now());
Log::channel('Custom')->info($exception->getMessage());
Log::channel('Custom')->info("=================================");
}
}