Deploy: 2026-05-21 18:21:40
This commit is contained in:
@@ -10,7 +10,7 @@ use App\Core\Security;
|
||||
*/
|
||||
class Contact extends BaseModel
|
||||
{
|
||||
protected string $table = 'contacts';
|
||||
protected static string $table = 'contacts';
|
||||
|
||||
/**
|
||||
* Create a new contact with encryption
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user