Skip to content

Commit 5d1b5e3

Browse files
committed
updated api
1 parent ab8449a commit 5d1b5e3

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

Helper/Data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function getCoreRegistry()
202202
public function getChatId(\Lof\ChatSystem\Model\ChatFactory $chatFactory) {
203203
if (!$this->_current_chat_id) {
204204
if($this->isLoggedIn()) {
205-
$chat = $chatFactory->create()->getCollection()->addFieldToFilter('customer_email',$this->getCustomerEmail());
205+
$chat = $chatFactory->create()->getCollection()->addFieldToFilter('customer_email', $this->getCustomerEmail());
206206
if($chat->getSize() > 0) {
207207
$chat_id = $chat->getFirstItem()->getData('chat_id');
208208
}else {

Model/ChatRepository.php

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class ChatRepository implements ChatRepositoryInterface
5454
protected $searchMessageResultsFactory;
5555
protected $sender;
5656

57+
/**
58+
* @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
59+
*/
60+
protected $customerRepository;
61+
5762
/**
5863
* @param ResourceChat $resource
5964
* @param ChatFactory $chatFactory
@@ -72,6 +77,7 @@ class ChatRepository implements ChatRepositoryInterface
7277
* @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
7378
* @param MessageSearchResultsInterfaceFactory $searchMessageResultsFactory
7479
* @param \Lof\ChatSystem\Model\Sender $sender
80+
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
7581
*/
7682
public function __construct(
7783
ResourceChat $resource,
@@ -90,7 +96,8 @@ public function __construct(
9096
\Lof\ChatSystem\Model\BlacklistFactory $blacklistFactory,
9197
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
9298
MessageSearchResultsInterfaceFactory $searchMessageResultsFactory,
93-
\Lof\ChatSystem\Model\Sender $sender
99+
\Lof\ChatSystem\Model\Sender $sender,
100+
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
94101
) {
95102
$this->resource = $resource;
96103
$this->chatFactory = $chatFactory;
@@ -109,6 +116,7 @@ public function __construct(
109116
$this->remoteAddress = $remoteAddress;
110117
$this->searchMessageResultsFactory = $searchMessageResultsFactory;
111118
$this->sender = $sender;
119+
$this->customerRepository = $customerRepository;
112120
}
113121

114122
/**
@@ -279,7 +287,7 @@ public function getMyChat($customerId){
279287
->addFieldToFilter('status', 1)
280288
->getFirstItem();
281289
$searchResults = $this->searchMessageResultsFactory->create();
282-
$collection = $this->messageFactory->create()->getCollection()->addFieldToFilter('customer_id',$customerId);
290+
$collection = $this->messageFactory->create()->getCollection()->addFieldToFilter('customer_id', $customerId);
283291

284292
$items = [];
285293
foreach ($collection as $model) {
@@ -296,6 +304,11 @@ public function getMyChat($customerId){
296304
}
297305

298306
/**
307+
* $message object only accept data
308+
* {
309+
* body_msg,
310+
* current_url
311+
* }
299312
* {@inheritdoc}
300313
*/
301314
public function sendCustomerChatMessage($customerId, \Lof\ChatSystem\Api\Data\MessageInterface $message){
@@ -304,12 +317,12 @@ public function sendCustomerChatMessage($customerId, \Lof\ChatSystem\Api\Data\Me
304317
'body_msg is required.'
305318
));
306319
}
307-
if(!$message->getCustomerId()) {
308-
$message->setCustomerId((int)$customerId);
309-
}
320+
$message->setCustomerId((int)$customerId);
321+
$client_ip = $this->remoteAddress->getRemoteAddress();
322+
$message->setIp($client_ip);
323+
310324
$enable_blacklist = $this->_helper->getConfig('chat/enable_blacklist');
311325
if ($enable_blacklist) {
312-
$client_ip = $this->remoteAddress->getRemoteAddress();
313326
$blacklist_model = $this->blacklistFactory->create();
314327
if ($client_ip) {
315328
$blacklist_model->loadByIp($client_ip);
@@ -327,21 +340,47 @@ public function sendCustomerChatMessage($customerId, \Lof\ChatSystem\Api\Data\Me
327340
}
328341
}
329342
//get chat id by logged in customer email
330-
$messageChatId = $message->getChatId();
343+
$messageChatId = 0;
344+
$chatCollection = $this->chatFactory->create()->getCollection()
345+
->addFieldToFilter('customer_id', (int)$customerId)
346+
->addFieldToFilter('status', 1);
347+
348+
if ($chatCollection->getSize() > 0) {
349+
$messageChatId = $chatCollection->getFirstItem()->getData('chat_id');
350+
$chat = $this->chatFactory->create()->load((int)$messageChatId);
351+
} else {
352+
$chat = $this->chatFactory->create();
353+
$customer = $this->customerRepository->getById($customerId);
354+
$chat
355+
->setCustomerId($customerId)
356+
->setCustomerName($customer->getFirstname(). " ". $customer->getLastname())
357+
->setCustomerEmail($customer->getEmail());
358+
359+
$chat->save();
360+
$messageChatId = (int)$chat->getData('chat_id');
361+
}
331362
// check at here
332-
363+
333364
$message->setIsRead(1);
334365
$body_msg = $message->getBodyMsg();
335366
$body_msg = $this->_helper->xss_clean($body_msg);
336367
$message->setBodyMsg($body_msg);
368+
$message->setChatId((int)$messageChatId);
369+
$message->setCustomerEmail($chat->getCustomerEmail());
370+
$message->setCustomerName($chat->getCustomerName());
337371

338372
$data = $message->__toArray();
373+
unset($data["message_id"]);
374+
unset($data["user_name"]);
375+
unset($data["name"]);
376+
unset($data["created_at"]);
377+
unset($data["updated_at"]);
339378

340379
$messageModel = $this->messageFactory->create();
341380
$messageModel
342381
->setData($data)
343382
->save();
344-
$chat = $this->chatFactory->create()->load((int)$messageChatId);
383+
345384
$number_message = (int)$chat->getData('number_message') + 1;
346385

347386
$enable_auto_assign_user = $this->_helper->getConfig('system/enable_auto_assign_user');

0 commit comments

Comments
 (0)