send email

This commit is contained in:
amazuecos
2025-03-24 22:45:02 +01:00
parent c0d54e28b7
commit 74867ca1ac
7 changed files with 152 additions and 25 deletions

View File

@ -17,12 +17,13 @@ use App\Models\Configuracion\ConfigVariableModel;
use App\Models\Presupuestos\PresupuestoModel;
use App\Models\Usuarios\UserModel;
use CodeIgniter\Config\BaseService;
use CodeIgniter\Email\Email;
class ChatService extends BaseService
{
protected ?ChatEntity $chatEntity;
protected Email $emailService;
protected UserModel $userModel;
protected ChatModel $chatModel;
protected ChatMessageModel $chatMessageModel;
protected ChatUser $chatUserModel;
@ -39,6 +40,8 @@ class ChatService extends BaseService
protected array $modelClassMap;
public function __construct()
{
$this->emailService = service('email');
$this->userModel = model(UserModel::class);
$this->chatModel = model(ChatModel::class);
$this->chatMessageModel = model(ChatMessageModel::class);
$this->chatNotificationModel = model(ChatNotification::class);
@ -87,6 +90,12 @@ class ChatService extends BaseService
}
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $this->chatEntity->id, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
$chatMessageEntity = $this->chatMessageModel->find($chat_message_id);
if ($data["client"]) {
$userClient = $this->userModel->find($data['client']);
if ($userClient) {
$this->sendMessageNotificationEmail($chatMessageEntity, $userClient);
}
}
return $chatMessageEntity;
}
public function createChat(int $chatDepartmentId, string $model, int $modelId): bool|int|string
@ -112,9 +121,9 @@ class ChatService extends BaseService
{
$chatDepartments = $this->chatDepartmentModel->getModelChatDepartments($this->modelFkMap[$model], $modelId);
$departmentWithChat = array_map(fn($q) => $q["name"], $chatDepartments);
if(count($departmentWithChat) > 0){
if (count($departmentWithChat) > 0) {
$departmentWithoutChat = $this->chatDepartmentModel->whereNotIn('name', $departmentWithChat)->findAll();
}else{
} else {
$departmentWithoutChat = $this->chatDepartmentModel->findAll();
}
foreach ($departmentWithoutChat as $value) {
@ -153,18 +162,18 @@ class ChatService extends BaseService
->where($this->modelFkMap[$model], $modelId)
->findAll();
foreach ($chats as $c) {
$this->chatModel->setAsViewedChatUserNotifications($c->id,auth()->user()->id);
$this->chatModel->setAsViewedChatUserNotifications($c->id, auth()->user()->id);
}
$chatWithHebraData = array_map(fn(ChatEntity $c) => $c->withHebra(), $chats);
return $chatWithHebraData ?? [];
}
public function getAuthUserNotifications(): array
public function getAuthUserNotifications(): array
{
$departmentNotifications = $this->chatModel->getChatDepartmentNotifications();
$internalNotifications = $this->chatModel->getChatInternalNotifications();
$totalMessages = 0;
$countNotificiationDepartments = array_map(fn($n) => $n->unreadMessages,$departmentNotifications);
$countNotificationInternal = array_map(fn($n) => $n->unreadMessages,$internalNotifications);
$countNotificiationDepartments = array_map(fn($n) => $n->unreadMessages, $departmentNotifications);
$countNotificationInternal = array_map(fn($n) => $n->unreadMessages, $internalNotifications);
$totalMessages = array_sum($countNotificiationDepartments) + array_sum($countNotificationInternal);
return [
@ -173,4 +182,14 @@ class ChatService extends BaseService
"totalMessages" => $totalMessages
];
}
public function sendMessageNotificationEmail(ChatMessageEntity $chatMessageEntity, UserEntity $user): bool
{
$users = auth()->getProvider();
$toEmail = $users->find($user->id)->getEmail();
$this->emailService->setTo($toEmail);
$subject = '[Safekat]' . lang('Chat.mail.mail_subject') . '-' . $chatMessageEntity->chat()->title;
$this->emailService->setSubject($subject);
$this->emailService->setMessage(view('themes/vuexy/mail/messageNotification', ["header" => lang('Chat.mail.mail_subject'), "data" => $chatMessageEntity->withUser()]));
return $this->emailService->send();
}
}