Deploy: 2026-05-21 18:21:40

This commit is contained in:
Hamza-Ayed
2026-05-21 18:21:40 +03:00
parent a411acbdf6
commit 0e0a3f77c0
3 changed files with 26 additions and 28 deletions

View File

@@ -4,23 +4,25 @@ namespace App\Models;
use App\Core\Security;
use App\Core\Database;
/**
* WhatsAppSession Model
* Handles the whatsapp_sessions table with encryption for phone and QR code.
*/
class WhatsAppSession extends BaseModel
{
protected string $table = 'whatsapp_sessions';
protected static string $table = 'whatsapp_sessions';
/**
* Get the session for a specific company
*/
public function findByCompany(int $companyId)
public static function findByCompany(int $companyId)
{
$session = $this->db->query(
"SELECT * FROM {$this->table} WHERE company_id = ? LIMIT 1",
$session = Database::selectOne(
"SELECT * FROM " . static::$table . " WHERE company_id = ? LIMIT 1",
[$companyId]
)->fetch();
);
if ($session) {
$session['phone'] = $session['phone'] ? Security::decrypt($session['phone']) : null;
@@ -33,12 +35,12 @@ class WhatsAppSession extends BaseModel
/**
* Get a session by session_key (used by webhooks)
*/
public function findBySessionKey(string $sessionKey)
public static function findBySessionKey(string $sessionKey)
{
$session = $this->db->query(
"SELECT * FROM {$this->table} WHERE session_key = ? LIMIT 1",
$session = Database::selectOne(
"SELECT * FROM " . static::$table . " WHERE session_key = ? LIMIT 1",
[$sessionKey]
)->fetch();
);
if ($session) {
$session['phone'] = $session['phone'] ? Security::decrypt($session['phone']) : null;
@@ -51,29 +53,29 @@ class WhatsAppSession extends BaseModel
/**
* Create or retrieve a new session for a company
*/
public function findOrCreate(int $companyId, string $name = 'Main WhatsApp')
public static function findOrCreate(int $companyId, string $name = 'Main WhatsApp')
{
$session = $this->findByCompany($companyId);
$session = static::findByCompany($companyId);
if ($session) {
return $session;
}
$sessionKey = 'cmp_' . $companyId . '_' . bin2hex(random_bytes(4));
$id = $this->create([
$id = static::create([
'company_id' => $companyId,
'name' => $name,
'session_key' => $sessionKey,
'status' => 'disconnected'
]);
return $this->findByCompany($companyId);
return static::findByCompany($companyId);
}
/**
* Update session state securely
*/
public function updateState(int $id, array $data)
public static function updateState(int $id, array $data)
{
if (isset($data['phone'])) {
$data['phone_hash'] = Security::blindIndex($data['phone']);
@@ -84,6 +86,6 @@ class WhatsAppSession extends BaseModel
$data['qr_code'] = Security::encrypt($data['qr_code']);
}
return $this->update($id, $data);
return static::update($id, $data);
}
}