/** * Tranfer client to another helpdesk * * <b>Request Type: </b>POST<br/> * <b>Request Endpoint: </b>http://{server-domain}/api/chat/conversation/transfer * <b>Summary: </b> This api is for transfer helpdesk.<br/> * * <b>Request Parameters: </b><br/> * accesstoken: string<br/> * deskId: string, id of the desk.<br/> * clientOpenId: string, the openId of the client.<br/> * conversationId: string, the id of chatConversation.<br/> * targetDeskId: string, the id of the target desk * * <b>Response Example: </b><br/> * { * "status" => "ok" * } */ public function actionTransfer() { $cache = Yii::$app->cache; $targetDeskId = $this->getParams('targetDeskId'); $conversationId = $this->getParams('conversationId'); $accountId = $this->getAccountId(); $conversations = $cache->get('conversations' . $accountId); if ($conversations) { $lastChatConversation = ChatConversation::findByPk(new \MongoId($conversationId)); if ($lastChatConversation->status === ChatConversation::STATUS_CLOSED) { throw new BadRequestHttpException('ChatConversation has been closed'); } $client = $lastChatConversation->client; $clientOpenId = $lastChatConversation->client['openId']; $deskMongoId = $lastChatConversation->desk['id']; $deskId = (string) $deskMongoId; $targetDeskMongoId = new \MongoId($targetDeskId); $helpDesk = HelpDesk::findByPk($deskMongoId); $targetHelpDesk = HelpDesk::findByPk($targetDeskMongoId); $maxClientLimit = HelpDeskSetting::getMaxClientCount($accountId); if ($targetHelpDesk->clientCount >= $maxClientLimit) { throw new BadRequestHttpException('Target helpdesk has exceed the max serve number'); } // Set the previous chat conversation status to 'closed' $lastChatConversation->status = ChatConversation::STATUS_CLOSED; if (!$lastChatConversation->update()) { LogUtil::error(['message' => 'Update previous helpdesk chatConversation status failed', 'error' => $lastChatConversation->errors], 'helpdesk'); throw new ServerErrorHttpException('Update previous helpdesk chatConversation status failed'); } else { HelpDesk::decClientCount($deskMongoId); } // Remove the client from original desk client list, and add to transfered desk client list foreach ($conversations[$deskId] as $index => $openId) { if ($openId == $clientOpenId) { unset($conversations[$deskId][$index]); } } $conversations[$targetDeskId][] = $clientOpenId; Yii::$app->cache->set('conversations' . $accountId, $conversations); // Create chatConversation record in db $chatConversation = ChatConversation::saveRecord($targetHelpDesk, $client, ChatConversation::STATUS_OPEN, $accountId); // Generate response data $newChannelName = ChatConversation::getChannelName($targetDeskId, $clientOpenId); $lastChatTime = ChatConversation::getLastChatTime($targetDeskMongoId, $clientOpenId, $accountId); $chatTimes = ChatConversation::getChatTimes($clientOpenId, $accountId); $previousDesk = $lastChatConversation->desk; $previousDesk['id'] = $deskId; $desk = $chatConversation->desk; $desk['id'] = $targetDeskId; $data = ['conversationId' => (string) $chatConversation->_id, 'desk' => $desk, 'previousDesk' => $previousDesk, 'client' => $client, 'channel' => $newChannelName, 'lastChatTime' => $lastChatTime, 'chatTimes' => $chatTimes, 'startTime' => TimeUtil::msTime()]; // Trigger transfer event to global channel, and the desk-client channel to make the client change listening channel Yii::$app->tuisongbao->triggerEvent(ChatConversation::EVENT_DESK_TRANSFER, $data, [ChatConversation::CHANNEL_GLOBAL . $accountId, $lastChatConversation->conversation]); // Push state $pushExtra = ['openId' => $clientOpenId, 'conversationId' => (string) $chatConversation->_id, 'previousDeskId' => $deskId, 'sentTime' => TimeUtil::msTime()]; $previousDeskName = empty($previousDesk['name']) ? '' : $previousDesk['name']; $pushMessage = str_replace('{desk}', $previousDeskName, ChatConversation::PUSH_MESSAGE_TRANSFER); ChatConversation::pushMessage($previousDesk['id'], ChatConversation::EVENT_DESK_TRANSFER, $pushExtra); ChatConversation::pushMessage($desk['id'], ChatConversation::EVENT_DESK_TRANSFER, $pushExtra, $pushMessage); LogUtil::info(['event' => ChatConversation::EVENT_DESK_TRANSFER, 'desk' => $desk, 'previousDesk' => $previousDesk, 'client' => $client], 'helpdesk'); return array_merge($data, ['status' => 'ok']); } }
/** * Destroy the connection between client and helpdesk */ public static function destroyConnection($chatConversation, $extra) { $result = ['status' => 'ok']; $desk = $chatConversation->desk; $client = $chatConversation->client; $accountId = $chatConversation->accountId; $conversations = Yii::$app->cache->get('conversations' . $accountId); //set the status of the chatConversation to "closed" ChatConversation::closeById($chatConversation->_id); //remove the record in cache foreach ($conversations[(string) $desk['id']] as $index => $clientId) { if ($clientId === $client['openId']) { unset($conversations[(string) $desk['id']][$index]); //update clientCount for helpDesk in db. HelpDesk::decClientCount($desk['id']); } } Yii::$app->cache->set('conversations' . $accountId, $conversations); //trigger the client left event $channelName = ChatConversation::getChannelName((string) $desk['id'], $client['openId']); $data = ['conversationId' => (string) $chatConversation->_id, 'desk' => ['badge' => $desk['badge'], 'id' => (string) $desk['id'], 'email' => $desk['email'], 'avatar' => $desk['avatar']], 'client' => $client, 'extra' => $extra, 'channel' => $channelName, 'sentTime' => TimeUtil::msTime()]; Yii::$app->tuisongbao->triggerEvent(ChatConversation::EVENT_CLIENT_LEFT, $data, [$channelName]); //push event to helpdesk which login in the mobile $pushExtra = ['openId' => $client['openId'], 'conversationId' => (string) $chatConversation->_id, 'sentTime' => TimeUtil::msTime()]; ChatConversation::pushMessage($desk['id'], ChatConversation::EVENT_CLIENT_LEFT, $pushExtra); //send the desk left message to client for wechat, weibo or alipay LogUtil::error(['client' => $client, 'accountId' => $accountId, 'extra' => $extra]); self::sendSystemReplyByType($client, $accountId, $extra['type']); $needReplySourceArray = [ChatConversation::TYPE_WECHAT, ChatConversation::TYPE_WEIBO, ChatConversation::TYPE_ALIPAY]; if (in_array($client['source'], $needReplySourceArray)) { //send the disconnect event to WeConnect $modeKey = SelfHelpDeskSetting::CONVERSATION_MODE_PREFIX . (string) $accountId . '-' . $client['openId']; Yii::$app->cache->redis->del($modeKey); Yii::$app->weConnect->sendCustomerServiceMessage($client['openId'], $client['sourceChannel'], ['msgType' => ChatConversation::WECHAT_MESSAGE_TYPE_EVENT, 'content' => 'DISCONNECT']); } // get pending clients self::connectPendingClient((string) $desk['id'], $accountId); $result['channel'] = $channelName; LogUtil::info(['event' => ChatConversation::EVENT_CLIENT_LEFT, 'client' => $client, 'desk' => $desk], 'helpdesk'); return $result; }