Initial baseline WACloud CI3 production
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Chat_model extends CI_Model {
|
||||
|
||||
// 🔹 Ambil semua session aktif beserta pesan terakhir
|
||||
public function get_all_sessions()
|
||||
{
|
||||
$sql = "
|
||||
SELECT
|
||||
s.id,
|
||||
s.customer_name,
|
||||
s.customer_number,
|
||||
s.last_message_at,
|
||||
s.status,
|
||||
(
|
||||
SELECT m.message_body
|
||||
FROM messages m
|
||||
WHERE m.session_id = s.id
|
||||
ORDER BY m.id DESC
|
||||
LIMIT 1
|
||||
) AS last_message
|
||||
FROM sessions s
|
||||
ORDER BY s.last_message_at DESC LIMIT 200
|
||||
";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
// 🔹 Ambil semua semua chat per session
|
||||
public function get_messages_by_session($session_id)
|
||||
{
|
||||
$this->db->select('
|
||||
m.id,
|
||||
m.session_id,
|
||||
m.direction,
|
||||
m.message_type,
|
||||
m.message_body,
|
||||
m.content,
|
||||
m.status,
|
||||
m.timestamp,
|
||||
f.file_name,
|
||||
f.file_url,
|
||||
f.mime_type,
|
||||
f.file_size,
|
||||
g.name
|
||||
');
|
||||
$this->db->from('messages m');
|
||||
$this->db->join('message_files f', 'f.message_id = m.id', 'left');
|
||||
$this->db->join('users g', 'g.id = m.user_id', 'left');
|
||||
$this->db->where('m.session_id', $session_id);
|
||||
$this->db->order_by('m.timestamp', 'ASC');
|
||||
|
||||
$query = $this->db->get();
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
// 🔹 Ambil semua semua chat per session
|
||||
public function get_messages_by_id($messages_id)
|
||||
{
|
||||
$this->db->select('
|
||||
m.id,
|
||||
m.session_id,
|
||||
m.direction,
|
||||
m.message_type,
|
||||
m.message_body,
|
||||
m.content,
|
||||
m.status,
|
||||
m.timestamp,
|
||||
f.file_name,
|
||||
f.file_url,
|
||||
f.mime_type,
|
||||
f.file_size,
|
||||
g.name
|
||||
');
|
||||
$this->db->from('messages m');
|
||||
$this->db->join('message_files f', 'f.message_id = m.id', 'left');
|
||||
$this->db->join('users g', 'g.id = m.user_id', 'left');
|
||||
$this->db->where('m.id', $messages_id);
|
||||
|
||||
$query = $this->db->get();
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
// 🔹 Update status session jadi 'closed'
|
||||
public function updateStatusSession($session_id)
|
||||
{
|
||||
$this->db->where('id', $session_id);
|
||||
return $this->db->update('sessions', ['status' => 'closed']);
|
||||
}
|
||||
|
||||
// 🔹 Update user_id message white session active
|
||||
public function updateUserMessage($session_id, $user_id)
|
||||
{
|
||||
// 🔹 Gunakan array tunggal atau beberapa where terpisah
|
||||
$this->db->where([
|
||||
'session_id' => $session_id,
|
||||
'user_id' => 0,
|
||||
'direction' => 'in',
|
||||
]);
|
||||
|
||||
return $this->db->update('messages', ['user_id' => $user_id]);
|
||||
}
|
||||
|
||||
|
||||
public function get_template_chat($whatsapp_number_id, $template_name)
|
||||
{
|
||||
return $this->db
|
||||
->where('whatsapp_number_id', $whatsapp_number_id)
|
||||
->where('template_name', $template_name)
|
||||
->get('whatsapp_templates')
|
||||
->row();
|
||||
}
|
||||
|
||||
public function get_whatsapp_number_by_phone($phoneNumber)
|
||||
{
|
||||
return $this->db
|
||||
->where('customer_number', $phoneNumber)
|
||||
->get('sessions')
|
||||
->row();
|
||||
}
|
||||
|
||||
public function get_session_by_id($session_id)
|
||||
{
|
||||
return $this->db
|
||||
->where('id', $session_id)
|
||||
->get('sessions')
|
||||
->row();
|
||||
}
|
||||
|
||||
public function get_session_detail($session_id)
|
||||
{
|
||||
$this->db->select('
|
||||
m.customer_number,
|
||||
f.phone_number_id,
|
||||
f.access_token
|
||||
');
|
||||
$this->db->from('sessions m');
|
||||
$this->db->join('whatsapp_numbers f', 'f.id = m.whatsapp_number_id', 'left');
|
||||
$this->db->where('m.id', $session_id);
|
||||
|
||||
$query = $this->db->get();
|
||||
return $query->row_array();
|
||||
}
|
||||
|
||||
public function get_template($whatsapp_number_id, $template_name, $language = 'id')
|
||||
{
|
||||
$query = $this->db
|
||||
->select('*')
|
||||
->from('whatsapp_templates')
|
||||
->where('whatsapp_number_id', $whatsapp_number_id)
|
||||
->where('template_name', $template_name)
|
||||
->where('language', $language)
|
||||
->limit(1)
|
||||
->get();
|
||||
|
||||
$result = $query->row();
|
||||
|
||||
if ($result && isset($result->components)) {
|
||||
// Karena kolom JSON, biarkan seperti apa adanya
|
||||
$result->components = $result->components;
|
||||
}
|
||||
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function get_sessions_paginated($whatsapp_number_id, $limit = 15, $offset = 0, $search = null, $filter = 'all')
|
||||
{
|
||||
if (empty($whatsapp_number_id)) {
|
||||
// ⚠️ Kalau tidak ada ID, jangan ambil data apa pun
|
||||
return [];
|
||||
}
|
||||
|
||||
$this->db->select('*');
|
||||
$this->db->from('sessions');
|
||||
$this->db->where('whatsapp_number_id', $whatsapp_number_id);
|
||||
|
||||
// ✅ Filter unread session
|
||||
if ($filter === 'unread') {
|
||||
$this->db->where('status', 'open');
|
||||
}
|
||||
|
||||
// ✅ Pencarian nama / nomor pelanggan
|
||||
if (!empty($search)) {
|
||||
$this->db->group_start();
|
||||
$this->db->like('customer_name', $search);
|
||||
$this->db->or_like('customer_number', $search);
|
||||
$this->db->group_end();
|
||||
}
|
||||
|
||||
$this->db->order_by('last_message_at', 'DESC');
|
||||
$this->db->limit($limit, $offset);
|
||||
|
||||
return $this->db->get()->result_array();
|
||||
}
|
||||
|
||||
|
||||
public function count_sessions($search = null, $filter = 'all')
|
||||
{
|
||||
$this->db->select('COUNT(*) AS total');
|
||||
$this->db->from('sessions');
|
||||
|
||||
if ($filter === 'unread') {
|
||||
$this->db->where('status', 'open');
|
||||
}
|
||||
|
||||
if (!empty($search)) {
|
||||
$this->db->group_start();
|
||||
$this->db->like('customer_name', $search);
|
||||
$this->db->or_like('customer_number', $search);
|
||||
$this->db->group_end();
|
||||
}
|
||||
|
||||
return (int) $this->db->get()->row()->total;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user