message chat section

This commit is contained in:
amazuecos
2024-11-29 22:05:50 +01:00
parent c1aecb43f2
commit 2e767889ef
24 changed files with 615 additions and 105 deletions

View File

@ -29,7 +29,7 @@ class ChatController extends BaseController
protected ClienteModel $clienteModel;
protected ChatUser $chatUserModel;
protected ChatNotification $chatNotificationModel;
protected array $viewData;
protected static $viewPath = 'themes/vuexy/form/mensajes/';
@ -57,6 +57,10 @@ class ChatController extends BaseController
$data = $this->chatDeparmentModel->getChatDepartments();
return $this->response->setJSON($data);
}
public function get_chat_department_select(){
$query = $this->chatDeparmentModel->getChatDepartmentSelect($this->request->getGet("q"));
return $this->response->setJSON($query->get()->getResultObject());
}
public function get_chat_presupuesto(int $chat_department_id, int $presupuesto_id)
{
@ -122,6 +126,62 @@ class ChatController extends BaseController
return view(static::$viewPath . 'messageChat', $this->viewData);
}
public function get_chat_presupuesto_view($chat_id)
{
$chat = $this->chatModel->find($chat_id);
$this->viewData['breadcrumb'] = [
['title' => lang("Chat.chat"), 'route' => route_to("mensajeriaView"), 'active' => false],
['title' => $chat->title, 'route' => 'javascript:void(0);', 'active' => true]
];
$this->viewData["modelId"] = $chat->presupuesto_id;
$this->viewData["type"] = "presupuesto";
$auth_user = auth()->user();
$this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id);
$this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id);
if($chat->chat_department_id){
return view(static::$viewPath . 'messageChatPresupuesto', $this->viewData);
}else{
return view(static::$viewPath . 'messageChatInternal', $this->viewData);
}
}
public function get_chat_pedido_view($chat_id)
{
$chat = $this->chatModel->find($chat_id);
$this->viewData['breadcrumb'] = [
['title' => lang("Chat.chat"), 'route' => route_to("mensajeriaView"), 'active' => false],
['title' => $chat->title, 'route' => 'javascript:void(0);', 'active' => true]
];
$this->viewData["modelId"] = $chat->pedido_id;
$this->viewData["type"] = "pedido";
$auth_user = auth()->user();
$this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id);
$this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id);
if($chat->chat_department_id ){
return view(static::$viewPath . 'messageChatPedido', $this->viewData);
}else{
return view(static::$viewPath . 'messageChatInternal', $this->viewData);
}
}
public function get_chat_factura_view($chat_id)
{
$chat = $this->chatModel->find($chat_id);
$this->viewData['breadcrumb'] = [
['title' => lang("Chat.chat"), 'route' => route_to("mensajeriaView"), 'active' => false],
['title' => $chat->title, 'route' => 'javascript:void(0);', 'active' => true]
];
$this->viewData["modelId"] = $chat->factura_id;
$this->viewData["type"] = "factura";
$auth_user = auth()->user();
$this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id);
$this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id);
if($chat->chat_department_id){
return view(static::$viewPath . 'messageChatFactura', $this->viewData);
}else{
return view(static::$viewPath . 'messageChatInternal', $this->viewData);
}
}
public function get_chat(int $chat_id)
{
@ -144,8 +204,8 @@ class ChatController extends BaseController
$dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]);
foreach ($chatDepartmentUsers as $user) {
if($user->id != auth()->user()->id){
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id"=> $user->id]);
if ($user->id != auth()->user()->id) {
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]);
}
}
return $this->response->setJSON($dataResponse);
@ -165,8 +225,8 @@ class ChatController extends BaseController
$dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]);
foreach ($chatDepartmentUsers as $user) {
if($user->id != auth()->user()->id){
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id"=> $user->id]);
if ($user->id != auth()->user()->id) {
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]);
}
}
return $this->response->setJSON($dataResponse);
@ -186,8 +246,8 @@ class ChatController extends BaseController
$dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]);
foreach ($chatDepartmentUsers as $user) {
if($user->id != auth()->user()->id){
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id"=> $user->id]);
if ($user->id != auth()->user()->id) {
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]);
}
}
return $this->response->setJSON($dataResponse);
@ -338,6 +398,7 @@ class ChatController extends BaseController
}
public function store_hebra_presupuesto()
{
$auth_user = auth()->user();
$bodyData = $this->request->getPost();
$chat_id = $this->chatModel->insert([
"presupuesto_id" => $bodyData["modelId"],
@ -346,9 +407,10 @@ class ChatController extends BaseController
$chatMessageId = $this->chatMessageModel->insert([
"chat_id" => $chat_id,
"message" => $bodyData["message"],
"sender_id" => auth()->user()->id
"sender_id" => $auth_user->id
]);
if (isset($bodyData["users"])) {
$bodyData["users"][] = $auth_user->id;
$chatUserData = array_map(fn($x) => ["user_id" => $x, "chat_id" => $chat_id], $bodyData["users"]);
$this->chatUserModel->insertBatch($chatUserData);
foreach ($bodyData["users"] as $userId) {
@ -494,7 +556,45 @@ class ChatController extends BaseController
->edit('updated_at', fn($q) => Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i"))
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id))
->add("action", fn($q) => $q->id)
->add("action", fn($q) => ["type" => "direct","modelId" => $q->id])
->toJson(true);
}
public function datatable_presupuesto_messages()
{
$auth_user_id = auth()->user()->id;
$query = $this->chatModel->getQueryDatatableMessagePresupuesto($auth_user_id);
return DataTable::of($query)
->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "")
->edit('updated_at', fn($q) => $q->updated_at ? Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i") : "")
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id))
->add("action", fn($q) => ["type" => "presupuesto","modelId" => $q->id])
->toJson(true);
}
public function datatable_pedido_messages()
{
$auth_user_id = auth()->user()->id;
$query = $this->chatModel->getQueryDatatableMessagePedido($auth_user_id);
return DataTable::of($query)
->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "")
->edit('updated_at', fn($q) => $q->updated_at ? Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i") : "")
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id))
->add("action", fn($q) => ["type" => "pedido","modelId" => $q->id])
->toJson(true);
}
public function datatable_factura_messages()
{
$auth_user_id = auth()->user()->id;
$query = $this->chatModel->getQueryDatatableMessageFactura($auth_user_id);
return DataTable::of($query)
->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "")
->edit('updated_at', fn($q) => $q->updated_at ? Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i") : "")
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id))
->add("action", fn($q) => ["type" => "factura","modelId" => $q->id])
->toJson(true);
}
public function store_new_direct_message()
@ -516,6 +616,28 @@ class ChatController extends BaseController
$this->chatModel->createNewDirectChat(...$bodyData);
return $this->response->setJSON(["message" => lang("Chat.new_message_ok"), "status" => true]);
}
public function store_new_direct_message_client()
{
$bodyData = $this->request->getPost();
$rules = [
"title" => "required|string",
"message" => "required|string",
"chat_department_id" => "required",
];
if (!$this->validate($rules)) {
return $this->response->setStatusCode(400)->setJSON([
'message' => lang('App.global_alert_save_error'),
'status' => 'error',
'errors' => $this->validator->getErrors(),
]);
}
$users = $this->chatDeparmentModel->getChatDepartmentUsers($bodyData["chat_department_id"]);
$bodyData["users"] = array_map(fn($q) => $q->id,$users);
dd(1);
$this->chatModel->createNewDirectChat(...$bodyData);
return $this->response->setJSON(["message" => lang("Chat.new_message_ok"), "status" => true]);
}
public function get_chat_direct($chat_id)
{
$chatData = $this->chatModel->getChatDirect($chat_id);
@ -570,9 +692,9 @@ class ChatController extends BaseController
$message = $this->chatMessageModel->get_chat_message($chat_message_id);
return $this->response->setJSON($message);
}
public function update_chat_direct_message_unread($chat_id){
$this->chatModel->setAsUnviewedChatUserNotifications($chat_id,auth()->user()->id);
public function update_chat_direct_message_unread($chat_id)
{
$this->chatModel->setAsUnviewedChatUserNotifications($chat_id, auth()->user()->id);
return $this->response->setJSON(["message" => "ok", "status" => true]);
}
}