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

@@ -17,8 +17,7 @@ class WhatsAppController extends BaseController
public function status(Request $request, Response $response) public function status(Request $request, Response $response)
{ {
$companyId = $request->company_id; // Added by AuthMiddleware $companyId = $request->company_id; // Added by AuthMiddleware
$sessionModel = new WhatsAppSession(); $session = WhatsAppSession::findOrCreate($companyId);
$session = $sessionModel->findOrCreate($companyId);
// Strip sensitive/internal data before sending to frontend // Strip sensitive/internal data before sending to frontend
unset($session['phone_hash']); unset($session['phone_hash']);
@@ -35,11 +34,10 @@ class WhatsAppController extends BaseController
public function requestQr(Request $request, Response $response) public function requestQr(Request $request, Response $response)
{ {
$companyId = $request->company_id; $companyId = $request->company_id;
$sessionModel = new WhatsAppSession(); $session = WhatsAppSession::findOrCreate($companyId);
$session = $sessionModel->findOrCreate($companyId);
// Temporarily set to connecting // Temporarily set to connecting
$sessionModel->updateState($session['id'], ['status' => 'connecting']); WhatsAppSession::updateState($session['id'], ['status' => 'connecting']);
// Call Baileys Node.js Service on port 3722 // Call Baileys Node.js Service on port 3722
$nodeUrl = 'http://127.0.0.1:3722/api/sessions/start'; $nodeUrl = 'http://127.0.0.1:3722/api/sessions/start';
@@ -69,7 +67,7 @@ class WhatsAppController extends BaseController
]); ]);
} else { } else {
// Revert state on failure // Revert state on failure
$sessionModel->updateState($session['id'], ['status' => 'disconnected']); WhatsAppSession::updateState($session['id'], ['status' => 'disconnected']);
$response->status(500)->json([ $response->status(500)->json([
'status' => 'error', 'status' => 'error',
'message' => 'Failed to reach WhatsApp Gateway.' 'message' => 'Failed to reach WhatsApp Gateway.'
@@ -83,8 +81,7 @@ class WhatsAppController extends BaseController
public function disconnect(Request $request, Response $response) public function disconnect(Request $request, Response $response)
{ {
$companyId = $request->company_id; $companyId = $request->company_id;
$sessionModel = new WhatsAppSession(); $session = WhatsAppSession::findByCompany($companyId);
$session = $sessionModel->findByCompany($companyId);
if ($session && $session['status'] !== 'disconnected') { if ($session && $session['status'] !== 'disconnected') {
// Call Baileys Node.js Service to disconnect // Call Baileys Node.js Service to disconnect
@@ -103,7 +100,7 @@ class WhatsAppController extends BaseController
curl_exec($ch); curl_exec($ch);
curl_close($ch); curl_close($ch);
$sessionModel->updateState($session['id'], [ WhatsAppSession::updateState($session['id'], [
'status' => 'disconnected', 'status' => 'disconnected',
'qr_code' => null, 'qr_code' => null,
'phone' => null, 'phone' => null,
@@ -132,8 +129,7 @@ class WhatsAppController extends BaseController
return; return;
} }
$sessionModel = new WhatsAppSession(); $session = WhatsAppSession::findBySessionKey($body['session_key']);
$session = $sessionModel->findBySessionKey($body['session_key']);
if (!$session) { if (!$session) {
$response->status(404)->json(['error' => 'Session not found']); $response->status(404)->json(['error' => 'Session not found']);
@@ -155,7 +151,7 @@ class WhatsAppController extends BaseController
$updateData['qr_code'] = null; $updateData['qr_code'] = null;
} }
$sessionModel->updateState($session['id'], $updateData); WhatsAppSession::updateState($session['id'], $updateData);
$response->json(['status' => 'success']); $response->json(['status' => 'success']);
} }

View File

@@ -10,7 +10,7 @@ use App\Core\Security;
*/ */
class Contact extends BaseModel class Contact extends BaseModel
{ {
protected string $table = 'contacts'; protected static string $table = 'contacts';
/** /**
* Create a new contact with encryption * Create a new contact with encryption

View File

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