Files
safekat/ci4/app/Services/MessageService.php
2025-02-16 08:10:07 +00:00

63 lines
2.6 KiB
PHP

<?php
namespace App\Services;
use App\Controllers\Configuracion\ConfigVariables;
use App\Models\Chat\ChatDeparmentModel;
use App\Models\Chat\ChatMessageModel;
use App\Models\Chat\ChatModel;
use App\Models\ChatNotification;
use App\Models\ChatUser;
use App\Models\Configuracion\ConfigVariableModel;
use App\Models\Presupuestos\PresupuestoModel;
use CodeIgniter\Config\BaseService;
class MessageService extends BaseService
{
protected ChatModel $chatModel;
protected ChatMessageModel $chatMessageModel;
protected ChatUser $chatUserModel;
protected ChatNotification $chatNotificationModel;
protected PresupuestoModel $presupuestoModel;
protected ChatDeparmentModel $chatDepartmentModel;
protected ConfigVariableModel $configVariables;
public function __construct()
{
$this->chatModel = model(ChatModel::class);
$this->chatMessageModel = model(ChatMessageModel::class);
$this->chatNotificationModel = model(ChatNotification::class);
$this->chatUserModel = model(ChatUser::class);
$this->presupuestoModel = model(PresupuestoModel::class);
$this->chatDepartmentModel = model(ChatDeparmentModel::class);
$this->configVariables = model(ConfigVariableModel::class);
}
public function createErrorMessagePresupuesto(string $error, int $presupuesto_id)
{
$dataResponse = null;
$chat_department_name = $this->configVariables->getVariable('send_error_to_chat_department_name')?->value;
$chat_department_id = $this->chatDepartmentModel->where('name', $chat_department_name)->first()?->id;
if ($chat_department_id) {
$existChat = $this->chatModel->existChatPresupuesto($chat_department_id, $presupuesto_id);
if ($existChat == false) {
$chatId = $this->chatModel->createChatPresupuesto($chat_department_id, $presupuesto_id);
} else {
$chat = $this->chatModel->getChatPresupuesto($chat_department_id, $presupuesto_id);
$chatId = $chat->id;
}
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $error]);
$dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDepartmentModel->getChatDepartmentUsers($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]);
}
}
}
return $dataResponse;
}
}