From 3c695e88fda656584ecae3dd543e269ca480a708 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Thu, 21 May 2026 18:42:33 +0300 Subject: [PATCH] Fix models database calls and structural updates --- backend/app/Models/Campaign.php | 8 ++++---- backend/app/Models/Contact.php | 30 ++++++++++++++--------------- backend/app/Models/ContactGroup.php | 20 +++++++++---------- backend/app/Models/MessageLog.php | 6 +++--- backend/app/Models/Template.php | 22 ++++++++++----------- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/backend/app/Models/Campaign.php b/backend/app/Models/Campaign.php index ae52186..9406fe2 100644 --- a/backend/app/Models/Campaign.php +++ b/backend/app/Models/Campaign.php @@ -13,15 +13,15 @@ class Campaign extends BaseModel /** * Get all campaigns for a company */ - public function findAllByCompany(int $companyId) + public static function findAllByCompany(int $companyId) { - return $this->db->query( + return \App\Core\Database::select( "SELECT c.*, g.name as group_name, t.name as template_name - FROM {$this->table} c + FROM " . static::$table . " c LEFT JOIN contact_groups g ON c.group_id = g.id LEFT JOIN templates t ON c.template_id = t.id WHERE c.company_id = ? ORDER BY c.id DESC", [$companyId] - )->fetchAll(); + ); } } diff --git a/backend/app/Models/Contact.php b/backend/app/Models/Contact.php index 1049e54..e499e01 100644 --- a/backend/app/Models/Contact.php +++ b/backend/app/Models/Contact.php @@ -15,7 +15,7 @@ class Contact extends BaseModel /** * Create a new contact with encryption */ - public function createSecure(array $data) + public static function createSecure(array $data) { if (!empty($data['phone'])) { $data['phone_hash'] = Security::blindIndex($data['phone']); @@ -31,13 +31,13 @@ class Contact extends BaseModel $data['notes'] = Security::encrypt($data['notes']); } - return $this->create($data); + return self::create($data); } /** * Update an existing contact with encryption */ - public function updateSecure(int $id, array $data) + public static function updateSecure(int $id, array $data) { if (isset($data['phone'])) { $data['phone_hash'] = Security::blindIndex($data['phone']); @@ -53,35 +53,35 @@ class Contact extends BaseModel $data['notes'] = Security::encrypt($data['notes']); } - return $this->update($id, $data); + return self::update($id, $data); } /** * Find a contact by decrypted phone number within a company */ - public function findByPhone(int $companyId, string $phone) + public static function findByPhone(int $companyId, string $phone) { $hash = Security::blindIndex($phone); - $contact = $this->db->query( - "SELECT * FROM {$this->table} WHERE company_id = ? AND phone_hash = ? LIMIT 1", + $contact = \App\Core\Database::selectOne( + "SELECT * FROM " . static::$table . " WHERE company_id = ? AND phone_hash = ? LIMIT 1", [$companyId, $hash] - )->fetch(); + ); - return $this->decryptContact($contact); + return self::decryptContact($contact); } /** * Retrieve all contacts for a company */ - public function findAllByCompany(int $companyId) + public static function findAllByCompany(int $companyId) { - $contacts = $this->db->query( - "SELECT * FROM {$this->table} WHERE company_id = ? ORDER BY id DESC", + $contacts = \App\Core\Database::select( + "SELECT * FROM " . static::$table . " WHERE company_id = ? ORDER BY id DESC", [$companyId] - )->fetchAll(); + ); foreach ($contacts as &$contact) { - $contact = $this->decryptContact($contact); + $contact = self::decryptContact($contact); } return $contacts; @@ -90,7 +90,7 @@ class Contact extends BaseModel /** * Helper to decrypt sensitive fields */ - private function decryptContact($contact) + private static function decryptContact($contact) { if ($contact) { $contact['phone'] = !empty($contact['phone']) ? Security::decrypt($contact['phone']) : null; diff --git a/backend/app/Models/ContactGroup.php b/backend/app/Models/ContactGroup.php index 67167f3..6cbe89f 100644 --- a/backend/app/Models/ContactGroup.php +++ b/backend/app/Models/ContactGroup.php @@ -8,20 +8,20 @@ namespace App\Models; */ class ContactGroup extends BaseModel { - protected string $table = 'contact_groups'; + protected static string $table = 'contact_groups'; /** * Attach a contact to this group */ - public function attachContact(int $groupId, int $contactId) + public static function attachContact(int $groupId, int $contactId) { - $exists = $this->db->query( + $exists = \App\Core\Database::selectOne( "SELECT 1 FROM contact_group_relations WHERE group_id = ? AND contact_id = ?", [$groupId, $contactId] - )->fetch(); + ); if (!$exists) { - $this->db->query( + \App\Core\Database::execute( "INSERT INTO contact_group_relations (group_id, contact_id) VALUES (?, ?)", [$groupId, $contactId] ); @@ -32,9 +32,9 @@ class ContactGroup extends BaseModel /** * Remove a contact from this group */ - public function detachContact(int $groupId, int $contactId) + public static function detachContact(int $groupId, int $contactId) { - $this->db->query( + \App\Core\Database::execute( "DELETE FROM contact_group_relations WHERE group_id = ? AND contact_id = ?", [$groupId, $contactId] ); @@ -44,13 +44,13 @@ class ContactGroup extends BaseModel /** * Get all raw contact records for a group (Decryption needed after fetch) */ - public function getRawContacts(int $groupId) + public static function getRawContacts(int $groupId) { - return $this->db->query( + return \App\Core\Database::select( "SELECT c.* FROM contacts c JOIN contact_group_relations cgr ON c.id = cgr.contact_id WHERE cgr.group_id = ?", [$groupId] - )->fetchAll(); + ); } } diff --git a/backend/app/Models/MessageLog.php b/backend/app/Models/MessageLog.php index b35fed1..a0103e1 100644 --- a/backend/app/Models/MessageLog.php +++ b/backend/app/Models/MessageLog.php @@ -10,12 +10,12 @@ use App\Core\Security; */ class MessageLog extends BaseModel { - protected string $table = 'messages_log'; + protected static string $table = 'messages_log'; /** * Securely log a new message */ - public function logMessage(array $data) + public static function logMessage(array $data) { if (!empty($data['contact_phone'])) { $data['contact_phone_hash'] = Security::blindIndex($data['contact_phone']); @@ -30,6 +30,6 @@ class MessageLog extends BaseModel $data['media_url'] = Security::encrypt($data['media_url']); } - return $this->create($data); + return self::create($data); } } diff --git a/backend/app/Models/Template.php b/backend/app/Models/Template.php index a9f84fd..93161c3 100644 --- a/backend/app/Models/Template.php +++ b/backend/app/Models/Template.php @@ -10,28 +10,28 @@ use App\Core\Security; */ class Template extends BaseModel { - protected string $table = 'templates'; + protected static string $table = 'templates'; /** * Create template with encrypted media URL if exists */ - public function createSecure(array $data) + public static function createSecure(array $data) { if (!empty($data['media_url'])) { $data['media_url'] = Security::encrypt($data['media_url']); } - return $this->create($data); + return self::create($data); } /** * Retrieve and decrypt templates */ - public function findAllByCompany(int $companyId) + public static function findAllByCompany(int $companyId) { - $templates = $this->db->query( - "SELECT * FROM {$this->table} WHERE company_id = ? ORDER BY id DESC", + $templates = \App\Core\Database::select( + "SELECT * FROM " . static::$table . " WHERE company_id = ? ORDER BY id DESC", [$companyId] - )->fetchAll(); + ); foreach ($templates as &$template) { if (!empty($template['media_url'])) { @@ -45,12 +45,12 @@ class Template extends BaseModel /** * Get single template and decrypt */ - public function findByIdAndCompany(int $id, int $companyId) + public static function findByIdAndCompany(int $id, int $companyId) { - $template = $this->db->query( - "SELECT * FROM {$this->table} WHERE id = ? AND company_id = ? LIMIT 1", + $template = \App\Core\Database::selectOne( + "SELECT * FROM " . static::$table . " WHERE id = ? AND company_id = ? LIMIT 1", [$id, $companyId] - )->fetch(); + ); if ($template && !empty($template['media_url'])) { $template['media_url'] = Security::decrypt($template['media_url']);