Bugfix: Use server timestamp to set agent_last_seen_at (#1114)

This commit is contained in:
Pranav Raj S
2020-08-03 13:40:20 +05:30
committed by GitHub
parent 941272cccd
commit 3b23aa7913
12 changed files with 62 additions and 26 deletions

View File

@@ -29,10 +29,8 @@ class ConversationApi extends ApiClient {
);
}
markMessageRead({ id, lastSeen }) {
return axios.post(`${this.url}/${id}/update_last_seen`, {
agent_last_seen_at: lastSeen,
});
markMessageRead({ id }) {
return axios.post(`${this.url}/${id}/update_last_seen`);
}
toggleTyping({ conversationId, status }) {

View File

@@ -218,12 +218,7 @@ export default {
},
makeMessagesRead() {
if (this.getUnreadCount !== 0 && this.getMessages !== undefined) {
this.$store.dispatch('markMessagesRead', {
id: this.currentChat.id,
lastSeen: this.getMessages.messages.last().created_at,
});
}
this.$store.dispatch('markMessagesRead', { id: this.currentChat.id });
},
},
};

View File

@@ -106,6 +106,9 @@ export default {
this.isEditing = false;
},
async fetchLabels(conversationId) {
if (!conversationId) {
return;
}
this.$store.dispatch('conversationLabels/get', conversationId);
},
},

View File

@@ -170,11 +170,18 @@ const actions = {
},
markMessagesRead: async ({ commit }, data) => {
setTimeout(() => {
commit(types.default.MARK_MESSAGE_READ, data);
}, 4000);
try {
await ConversationApi.markMessageRead(data);
const {
data: { id, agent_last_seen_at: lastSeen },
} = await ConversationApi.markMessageRead(data);
setTimeout(
() =>
commit(types.default.MARK_MESSAGE_READ, {
id,
lastSeen,
}),
4000
);
} catch (error) {
// Handle error
}

View File

@@ -134,7 +134,9 @@ export const mutations = {
[types.default.MARK_MESSAGE_READ](_state, { id, lastSeen }) {
const [chat] = _state.allConversations.filter(c => c.id === id);
chat.agent_last_seen_at = lastSeen;
if (chat) {
chat.agent_last_seen_at = lastSeen;
}
},
[types.default.CHANGE_CHAT_STATUS_FILTER](_state, data) {

View File

@@ -153,4 +153,28 @@ describe('#actions', () => {
expect(commit.mock.calls).toEqual([[types.default.ADD_MESSAGE, message]]);
});
});
describe('#markMessagesRead', () => {
beforeEach(() => {
jest.useFakeTimers();
});
it('sends correct mutations if api is successful', async () => {
const lastSeen = new Date().getTime() / 1000;
axios.post.mockResolvedValue({
data: { id: 1, agent_last_seen_at: lastSeen },
});
await actions.markMessagesRead({ commit }, { id: 1 });
jest.runAllTimers();
expect(commit).toHaveBeenCalledTimes(1);
expect(commit.mock.calls).toEqual([
[types.default.MARK_MESSAGE_READ, { id: 1, lastSeen }],
]);
});
it('sends correct mutations if api is unsuccessful', async () => {
axios.post.mockRejectedValue({ message: 'Incorrect header' });
await actions.markMessagesRead({ commit }, { id: 1 });
expect(commit.mock.calls).toEqual([]);
});
});
});

View File

@@ -20,6 +20,13 @@ describe('#mutations', () => {
{ id: 1, agent_last_seen_at: lastSeen },
]);
});
it('doesnot send any mutation if chat doesnot exist', () => {
const state = { allConversations: [] };
const lastSeen = new Date().getTime() / 1000;
mutations[types.MARK_MESSAGE_READ](state, { id: 1, lastSeen });
expect(state.allConversations).toEqual([]);
});
});
describe('#CLEAR_CURRENT_CHAT_WINDOW', () => {