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