Bug: Fix conversation not loading from the links in email (#602)

Bug: Load conversation from links
This commit is contained in:
Pranav Raj S 2020-03-08 22:08:25 +05:30 committed by GitHub
parent a8ac048716
commit 2a4fb7b056
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 118 additions and 36 deletions

2
.gitignore vendored
View file

@ -53,3 +53,5 @@ coverage
# ignore packages # ignore packages
node_modules node_modules
package-lock.json package-lock.json
*.dump

View file

@ -1,8 +1,18 @@
class Api::V1::Conversations::MessagesController < Api::BaseController class Api::V1::Conversations::MessagesController < Api::BaseController
before_action :set_conversation, only: [:create] before_action :set_conversation, only: [:index, :create]
def index
@messages = message_finder.perform
end
def create def create
mb = Messages::Outgoing::NormalBuilder.new(current_user, @conversation, params) mb = Messages::Outgoing::NormalBuilder.new(current_user, @conversation, params)
@message = mb.perform @message = mb.perform
end end
private
def message_finder
@message_finder ||= MessageFinder.new(@conversation, params)
end
end end

View file

@ -7,9 +7,7 @@ class Api::V1::ConversationsController < Api::BaseController
@conversations_count = result[:count] @conversations_count = result[:count]
end end
def show def show; end
@messages = messages_finder.perform
end
def toggle_status def toggle_status
@status = @conversation.toggle_status @status = @conversation.toggle_status
@ -34,8 +32,4 @@ class Api::V1::ConversationsController < Api::BaseController
def conversation_finder def conversation_finder
@conversation_finder ||= ConversationFinder.new(current_user, params) @conversation_finder ||= ConversationFinder.new(current_user, params)
end end
def messages_finder
@message_finder ||= MessageFinder.new(@conversation, params)
end
end end

View file

@ -15,7 +15,7 @@ class MessageApi extends ApiClient {
} }
getPreviousMessages({ conversationId, before }) { getPreviousMessages({ conversationId, before }) {
return axios.get(`${this.url}/${conversationId}`, { return axios.get(`${this.url}/${conversationId}/messages`, {
params: { before }, params: { before },
}); });
} }

View file

@ -58,6 +58,7 @@ export default {
this.initialize(); this.initialize();
this.$watch('$store.state.route', () => this.initialize()); this.$watch('$store.state.route', () => this.initialize());
this.$watch('chatList.length', () => { this.$watch('chatList.length', () => {
this.fetchConversation();
this.setActiveChat(); this.setActiveChat();
}); });
}, },
@ -81,13 +82,28 @@ export default {
break; break;
default: default:
this.$store.dispatch('setActiveInbox', null); this.$store.dispatch('setActiveInbox', null);
this.$store.dispatch('clearSelectedState');
break; break;
} }
}, },
setActiveChat() { fetchConversation() {
if (!this.conversationId) {
return;
}
const chat = this.findConversation();
if (!chat) {
this.$store.dispatch('getConversation', this.conversationId);
}
},
findConversation() {
const conversationId = parseInt(this.conversationId, 10); const conversationId = parseInt(this.conversationId, 10);
const [chat] = this.chatList.filter(c => c.id === conversationId); const [chat] = this.chatList.filter(c => c.id === conversationId);
return chat;
},
setActiveChat() {
const chat = this.findConversation();
if (!chat) return; if (!chat) return;
this.$store.dispatch('setActiveChat', chat).then(() => { this.$store.dispatch('setActiveChat', chat).then(() => {
bus.$emit('scrollToMessage'); bus.$emit('scrollToMessage');

View file

@ -28,7 +28,7 @@ export default {
roles: ['administrator', 'agent'], roles: ['administrator', 'agent'],
component: ConversationView, component: ConversationView,
props: route => { props: route => {
return { conversationId: route.params.conversation_id }; return { inboxId: 0, conversationId: route.params.conversation_id };
}, },
}, },
{ {

View file

@ -7,6 +7,15 @@ import FBChannel from '../../../api/channel/fbChannel';
// actions // actions
const actions = { const actions = {
getConversation: async ({ commit }, conversationId) => {
try {
const response = await ConversationApi.show(conversationId);
commit(types.default.ADD_CONVERSATION, response.data);
} catch (error) {
// Ignore error
}
},
fetchAllConversations: async ({ commit, dispatch }, params) => { fetchAllConversations: async ({ commit, dispatch }, params) => {
commit(types.default.SET_LIST_LOADING_STATUS); commit(types.default.SET_LIST_LOADING_STATUS);
try { try {

View file

@ -33,12 +33,13 @@ const getters = {
getChatListLoadingStatus: ({ listLoadingStatus }) => listLoadingStatus, getChatListLoadingStatus: ({ listLoadingStatus }) => listLoadingStatus,
getAllMessagesLoaded(_state) { getAllMessagesLoaded(_state) {
const [chat] = getSelectedChatConversation(_state); const [chat] = getSelectedChatConversation(_state);
return chat.allMessagesLoaded === undefined return !chat || chat.allMessagesLoaded === undefined
? false ? false
: chat.allMessagesLoaded; : chat.allMessagesLoaded;
}, },
getUnreadCount(_state) { getUnreadCount(_state) {
const [chat] = getSelectedChatConversation(_state); const [chat] = getSelectedChatConversation(_state);
if (!chat) return [];
return chat.messages.filter( return chat.messages.filter(
chatMessage => chatMessage =>
chatMessage.created_at * 1000 > chat.agent_last_seen_at * 1000 && chatMessage.created_at * 1000 > chat.agent_last_seen_at * 1000 &&

View file

@ -0,0 +1,24 @@
import axios from 'axios';
import actions from '../../conversations/actions';
import * as types from '../../../mutation-types';
const commit = jest.fn();
global.axios = axios;
jest.mock('axios');
describe('#actions', () => {
describe('#getConversation', () => {
it('sends correct actions if API is success', async () => {
axios.get.mockResolvedValue({ data: { id: 1, meta: {} } });
await actions.getConversation({ commit }, 1);
expect(commit.mock.calls).toEqual([
[types.default.ADD_CONVERSATION, { id: 1, meta: {} }],
]);
});
it('sends correct actions if API is error', async () => {
axios.get.mockRejectedValue({ message: 'Incorrect header' });
await actions.getConversation({ commit });
expect(commit.mock.calls).toEqual([]);
});
});
});

View file

@ -0,0 +1,20 @@
json.meta do
json.labels @conversation.label_list
json.additional_attributes @conversation.additional_attributes
json.contact_id @conversation.contact_id
end
json.payload do
json.array! @messages do |message|
json.id message.id
json.content message.content
json.inbox_id message.inbox_id
json.conversation_id message.conversation.display_id
json.message_type message.message_type_before_type_cast
json.created_at message.created_at.to_i
json.private message.private
json.source_id message.source_id
json.attachment message.attachment.push_event_data if message.attachment
json.sender message.user.push_event_data if message.user
end
end

View file

@ -1,20 +1 @@
json.meta do json.partial! 'api/v1/conversations/partials/conversation.json.jbuilder', conversation: @conversation
json.labels @conversation.label_list
json.additional_attributes @conversation.additional_attributes
json.contact_id @conversation.contact_id
end
json.payload do
json.array! @messages do |message|
json.id message.id
json.content message.content
json.inbox_id message.inbox_id
json.conversation_id message.conversation.display_id
json.message_type message.message_type_before_type_cast
json.created_at message.created_at.to_i
json.private message.private
json.source_id message.source_id
json.attachment message.attachment.push_event_data if message.attachment
json.sender message.user.push_event_data if message.user
end
end

View file

@ -73,8 +73,8 @@ Rails.application.routes.draw do
end end
resources :conversations, only: [:index, :show] do resources :conversations, only: [:index, :show] do
scope module: :conversations do # for nested controller scope module: :conversations do
resources :messages, only: [:create] resources :messages, only: [:index, :create]
resources :assignments, only: [:create] resources :assignments, only: [:create]
resources :labels, only: [:create, :index] resources :labels, only: [:create, :index]
end end

View file

@ -31,4 +31,29 @@ RSpec.describe 'Conversation Messages API', type: :request do
end end
end end
end end
describe 'GET /api/v1/conversations/:id/messages' do
let(:conversation) { create(:conversation, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/conversations/#{conversation.display_id}/messages"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
it 'shows the conversation' do
get "/api/v1/conversations/#{conversation.display_id}/messages",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body, symbolize_names: true)[:meta][:contact_id]).to eq(conversation.contact_id)
end
end
end
end end

View file

@ -51,7 +51,7 @@ RSpec.describe 'Conversations API', type: :request do
as: :json as: :json
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(JSON.parse(response.body, symbolize_names: true)[:meta][:contact_id]).to eq(conversation.contact_id) expect(JSON.parse(response.body, symbolize_names: true)[:id]).to eq(conversation.display_id)
end end
end end
end end