Fix models database calls and structural updates

This commit is contained in:
Hamza-Ayed
2026-05-21 18:42:33 +03:00
parent 92755472e1
commit 3c695e88fd
5 changed files with 43 additions and 43 deletions

View File

@@ -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();
);
}
}

View File

@@ -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;

View File

@@ -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();
);
}
}

View File

@@ -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);
}
}

View File

@@ -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']);