From 87f758ee1f7ce39869a40902b315c08a2133686b Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Thu, 11 May 2023 12:56:43 +0530 Subject: [PATCH] feat: Order conversations by priority (#7053) --- app/finders/conversation_finder.rb | 3 +- app/javascript/dashboard/constants/globals.js | 1 + .../dashboard/i18n/locale/en/chatlist.json | 3 + .../dashboard/mixins/specs/time.spec.js | 1 + .../store/modules/conversations/getters.js | 11 +++- .../specs/conversations/getters.spec.js | 60 +++++++++++++++++++ app/javascript/shared/constants/messages.js | 8 +++ 7 files changed, 85 insertions(+), 2 deletions(-) diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index 9472dc623..074eed461 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -5,7 +5,8 @@ class ConversationFinder SORT_OPTIONS = { latest: 'latest', sort_on_created_at: 'sort_on_created_at', - last_user_message_at: 'last_user_message_at' + last_user_message_at: 'last_user_message_at', + sort_on_priority: 'sort_on_priority' }.with_indifferent_access # assumptions diff --git a/app/javascript/dashboard/constants/globals.js b/app/javascript/dashboard/constants/globals.js index 20497b039..e5aaf7814 100644 --- a/app/javascript/dashboard/constants/globals.js +++ b/app/javascript/dashboard/constants/globals.js @@ -15,6 +15,7 @@ export default { SORT_BY_TYPE: { LATEST: 'latest', CREATED_AT: 'sort_on_created_at', + PRIORITY: 'sort_on_priority', }, ARTICLE_STATUS_TYPES: { DRAFT: 0, diff --git a/app/javascript/dashboard/i18n/locale/en/chatlist.json b/app/javascript/dashboard/i18n/locale/en/chatlist.json index 6312d4c9f..731748696 100644 --- a/app/javascript/dashboard/i18n/locale/en/chatlist.json +++ b/app/javascript/dashboard/i18n/locale/en/chatlist.json @@ -47,6 +47,9 @@ }, "sort_on_created_at": { "TEXT": "Created at" + }, + "sort_on_priority": { + "TEXT": "Priority" } }, "ATTACHMENTS": { diff --git a/app/javascript/dashboard/mixins/specs/time.spec.js b/app/javascript/dashboard/mixins/specs/time.spec.js index 56b70ec8a..b1cd46d31 100644 --- a/app/javascript/dashboard/mixins/specs/time.spec.js +++ b/app/javascript/dashboard/mixins/specs/time.spec.js @@ -24,6 +24,7 @@ describe('#messageTimestamp', () => { describe('#dynamicTime', () => { it('returns correct value', () => { + Date.now = jest.fn(() => new Date(Date.UTC(2023, 1, 14)).valueOf()); expect(TimeMixin.methods.dynamicTime(1612971343)).toEqual( 'about 2 years ago' ); diff --git a/app/javascript/dashboard/store/modules/conversations/getters.js b/app/javascript/dashboard/store/modules/conversations/getters.js index d3a848d09..b5b38da0f 100644 --- a/app/javascript/dashboard/store/modules/conversations/getters.js +++ b/app/javascript/dashboard/store/modules/conversations/getters.js @@ -1,4 +1,7 @@ -import { MESSAGE_TYPE } from 'shared/constants/messages'; +import { + MESSAGE_TYPE, + CONVERSATION_PRIORITY_ORDER, +} from 'shared/constants/messages'; import { applyPageFilters } from './helpers'; export const getSelectedChatConversation = ({ @@ -13,6 +16,12 @@ const getters = { const comparator = { latest: (a, b) => b.last_activity_at - a.last_activity_at, sort_on_created_at: (a, b) => a.created_at - b.created_at, + sort_on_priority: (a, b) => { + return ( + CONVERSATION_PRIORITY_ORDER[a.priority] - + CONVERSATION_PRIORITY_ORDER[b.priority] + ); + }, }; return allConversations.sort(comparator[chatSortFilter]); diff --git a/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js index f9d775df2..2a012fd53 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js @@ -130,6 +130,66 @@ describe('#getters', () => { }, ]); }); + it('order conversations based on priority', () => { + const state = { + allConversations: [ + { + id: 1, + messages: [ + { + content: 'test1', + }, + ], + priority: 'low', + created_at: 1683645801, + last_activity_at: 2466424490, + }, + { + id: 2, + messages: [{ content: 'test2' }], + priority: 'urgent', + created_at: 1652109801, + last_activity_at: 1466424480, + }, + { + id: 3, + messages: [{ content: 'test3' }], + priority: 'medium', + created_at: 1652109801, + last_activity_at: 1466421280, + }, + ], + chatSortFilter: 'sort_on_priority', + }; + + expect(getters.getAllConversations(state)).toEqual([ + { + id: 2, + messages: [{ content: 'test2' }], + priority: 'urgent', + created_at: 1652109801, + last_activity_at: 1466424480, + }, + { + id: 3, + messages: [{ content: 'test3' }], + priority: 'medium', + created_at: 1652109801, + last_activity_at: 1466421280, + }, + { + id: 1, + messages: [ + { + content: 'test1', + }, + ], + priority: 'low', + created_at: 1683645801, + last_activity_at: 2466424490, + }, + ]); + }); }); describe('#getUnAssignedChats', () => { it('order returns only chats assigned to user', () => { diff --git a/app/javascript/shared/constants/messages.js b/app/javascript/shared/constants/messages.js index 77501cb76..c21248607 100644 --- a/app/javascript/shared/constants/messages.js +++ b/app/javascript/shared/constants/messages.js @@ -27,6 +27,14 @@ export const CONVERSATION_PRIORITY = { MEDIUM: 'medium', }; +export const CONVERSATION_PRIORITY_ORDER = { + urgent: 1, + high: 2, + medium: 3, + low: 4, + null: 5, +}; + // Size in mega bytes export const MAXIMUM_FILE_UPLOAD_SIZE = 40; export const MAXIMUM_FILE_UPLOAD_SIZE_TWILIO_SMS_CHANNEL = 5;