From 7c2353c7d9a08454d00352a162edef529e381da1 Mon Sep 17 00:00:00 2001 From: Fayaz Ahmed Date: Thu, 22 Aug 2024 16:48:02 +0530 Subject: [PATCH 1/3] chore: Repalce Hook Mixin with useHook composable [CW-3454] (#9994) # Pull Request Template ## Description Replace Hook mixin with useHook composable Fixes # (issue) ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Shivam Mishra --- .../spec/useIntegrationHook.spec.js | 56 +++++++++++++++ .../composables/useIntegrationHook.js | 68 +++++++++++++++++++ .../integrations/IntegrationHooks.vue | 39 +++++------ .../integrations/MultipleIntegrationHooks.vue | 14 ++-- .../settings/integrations/NewHook.vue | 16 +++-- .../integrations/SingleIntegrationHooks.vue | 15 ++-- 6 files changed, 174 insertions(+), 34 deletions(-) create mode 100644 app/javascript/dashboard/composables/spec/useIntegrationHook.spec.js create mode 100644 app/javascript/dashboard/composables/useIntegrationHook.js diff --git a/app/javascript/dashboard/composables/spec/useIntegrationHook.spec.js b/app/javascript/dashboard/composables/spec/useIntegrationHook.spec.js new file mode 100644 index 000000000..1837cf1dd --- /dev/null +++ b/app/javascript/dashboard/composables/spec/useIntegrationHook.spec.js @@ -0,0 +1,56 @@ +import { useIntegrationHook } from '../useIntegrationHook'; +import { useMapGetter } from 'dashboard/composables/store'; +import { nextTick } from 'vue'; + +vi.mock('dashboard/composables/store'); + +describe('useIntegrationHook', () => { + let integrationGetter; + + beforeEach(() => { + integrationGetter = vi.fn(); + useMapGetter.mockReturnValue({ value: integrationGetter }); + }); + + it('should return the correct computed properties', async () => { + const mockIntegration = { + id: 1, + hook_type: 'inbox', + hooks: ['hook1', 'hook2'], + allow_multiple_hooks: true, + }; + integrationGetter.mockReturnValue(mockIntegration); + + const hook = useIntegrationHook(1); + + await nextTick(); + + expect(hook.integration.value).toEqual(mockIntegration); + expect(hook.integrationType.value).toBe('multiple'); + expect(hook.isIntegrationMultiple.value).toBe(true); + expect(hook.isIntegrationSingle.value).toBe(false); + expect(hook.isHookTypeInbox.value).toBe(true); + expect(hook.hasConnectedHooks.value).toBe(true); + }); + + it('should handle single integration type correctly', async () => { + const mockIntegration = { + id: 2, + hook_type: 'channel', + hooks: [], + allow_multiple_hooks: false, + }; + integrationGetter.mockReturnValue(mockIntegration); + + const hook = useIntegrationHook(2); + + await nextTick(); + + expect(hook.integration.value).toEqual(mockIntegration); + expect(hook.integrationType.value).toBe('single'); + expect(hook.isIntegrationMultiple.value).toBe(false); + expect(hook.isIntegrationSingle.value).toBe(true); + expect(hook.isHookTypeInbox.value).toBe(false); + expect(hook.hasConnectedHooks.value).toBe(false); + }); +}); diff --git a/app/javascript/dashboard/composables/useIntegrationHook.js b/app/javascript/dashboard/composables/useIntegrationHook.js new file mode 100644 index 000000000..42546cceb --- /dev/null +++ b/app/javascript/dashboard/composables/useIntegrationHook.js @@ -0,0 +1,68 @@ +import { computed } from 'vue'; +import { useMapGetter } from 'dashboard/composables/store'; + +/** + * Composable for managing integration hooks + * @param {string|number} integrationId - The ID of the integration + * @returns {Object} An object containing computed properties for the integration + */ +export const useIntegrationHook = integrationId => { + const integrationGetter = useMapGetter('integrations/getIntegration'); + + /** + * The integration object + * @type {import('vue').ComputedRef} + */ + const integration = computed(() => { + return integrationGetter.value(integrationId); + }); + + /** + * Whether the integration hook type is 'inbox' + * @type {import('vue').ComputedRef} + */ + const isHookTypeInbox = computed(() => { + return integration.value.hook_type === 'inbox'; + }); + + /** + * Whether the integration has any connected hooks + * @type {import('vue').ComputedRef} + */ + const hasConnectedHooks = computed(() => { + return !!integration.value.hooks.length; + }); + + /** + * The type of integration: 'multiple' or 'single' + * @type {import('vue').ComputedRef} + */ + const integrationType = computed(() => { + return integration.value.allow_multiple_hooks ? 'multiple' : 'single'; + }); + + /** + * Whether the integration allows multiple hooks + * @type {import('vue').ComputedRef} + */ + const isIntegrationMultiple = computed(() => { + return integrationType.value === 'multiple'; + }); + + /** + * Whether the integration allows only a single hook + * @type {import('vue').ComputedRef} + */ + const isIntegrationSingle = computed(() => { + return integrationType.value === 'single'; + }); + + return { + integration, + integrationType, + isIntegrationMultiple, + isIntegrationSingle, + isHookTypeInbox, + hasConnectedHooks, + }; +}; diff --git a/app/javascript/dashboard/routes/dashboard/settings/integrations/IntegrationHooks.vue b/app/javascript/dashboard/routes/dashboard/settings/integrations/IntegrationHooks.vue index ad2daa5f3..0f8b9155e 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/integrations/IntegrationHooks.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/integrations/IntegrationHooks.vue @@ -2,7 +2,7 @@ import { isEmptyObject } from '../../../../helper/commons'; import { mapGetters } from 'vuex'; import { useAlert } from 'dashboard/composables'; -import hookMixin from './hookMixin'; +import { useIntegrationHook } from 'dashboard/composables/useIntegrationHook'; import NewHook from './NewHook.vue'; import SingleIntegrationHooks from './SingleIntegrationHooks.vue'; import MultipleIntegrationHooks from './MultipleIntegrationHooks.vue'; @@ -13,13 +13,28 @@ export default { SingleIntegrationHooks, MultipleIntegrationHooks, }, - mixins: [hookMixin], props: { integrationId: { type: [String, Number], required: true, }, }, + setup(props) { + const { integrationId } = props; + + const { + integration, + isIntegrationMultiple, + isIntegrationSingle, + isHookTypeInbox, + } = useIntegrationHook(integrationId); + return { + integration, + isIntegrationMultiple, + isIntegrationSingle, + isHookTypeInbox, + }; + }, data() { return { loading: {}, @@ -31,23 +46,9 @@ export default { }, computed: { ...mapGetters({ uiFlags: 'integrations/getUIFlags' }), - integration() { - return this.$store.getters['integrations/getIntegration']( - this.integrationId - ); - }, showIntegrationHooks() { return !this.uiFlags.isFetching && !isEmptyObject(this.integration); }, - integrationType() { - return this.integration.allow_multiple_hooks ? 'multiple' : 'single'; - }, - isIntegrationMultiple() { - return this.integrationType === 'multiple'; - }, - isIntegrationSingle() { - return this.integrationType === 'single'; - }, showAddButton() { return this.showIntegrationHooks && this.isIntegrationMultiple; }, @@ -120,14 +121,14 @@ export default {
@@ -135,7 +136,7 @@ export default {
- + import { mapGetters } from 'vuex'; -import hookMixin from './hookMixin'; +import { useIntegrationHook } from 'dashboard/composables/useIntegrationHook'; export default { - mixins: [hookMixin], props: { - integration: { - type: Object, - default: () => ({}), + integrationId: { + type: String, + required: true, }, }, + setup(props) { + const { integration, isHookTypeInbox, hasConnectedHooks } = + useIntegrationHook(props.integrationId); + return { integration, isHookTypeInbox, hasConnectedHooks }; + }, computed: { ...mapGetters({ globalConfig: 'globalConfig/get', diff --git a/app/javascript/dashboard/routes/dashboard/settings/integrations/NewHook.vue b/app/javascript/dashboard/routes/dashboard/settings/integrations/NewHook.vue index afaf059a6..935268592 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/integrations/NewHook.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/integrations/NewHook.vue @@ -2,16 +2,22 @@ From a48f98de9dd605b6c1319fce50b5321376c60b34 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 22 Aug 2024 19:41:11 +0530 Subject: [PATCH 2/3] revert: "chore: Replace messageMixing with useMessage composable [CW-3475]" (#10011) Reverts chatwoot/chatwoot#9942 This was causing the widget email input to break --- .../widget/components/AgentMessage.vue | 13 +--- .../widget/components/UserMessage.vue | 9 +-- .../composables/specs/useMessage.spec.js | 71 ------------------- .../widget/composables/useMessage.js | 22 ------ app/javascript/widget/mixins/messageMixin.js | 13 ++++ .../widget/mixins/specs/messageMixin.spec.js | 37 ++++++++++ 6 files changed, 54 insertions(+), 111 deletions(-) delete mode 100644 app/javascript/widget/composables/specs/useMessage.spec.js delete mode 100644 app/javascript/widget/composables/useMessage.js create mode 100644 app/javascript/widget/mixins/messageMixin.js create mode 100644 app/javascript/widget/mixins/specs/messageMixin.spec.js diff --git a/app/javascript/widget/components/AgentMessage.vue b/app/javascript/widget/components/AgentMessage.vue index e7d592abd..3296d5128 100755 --- a/app/javascript/widget/components/AgentMessage.vue +++ b/app/javascript/widget/components/AgentMessage.vue @@ -9,7 +9,7 @@ import FileBubble from 'widget/components/FileBubble.vue'; import Thumbnail from 'dashboard/components/widgets/Thumbnail.vue'; import { MESSAGE_TYPE } from 'widget/helpers/constants'; import configMixin from '../mixins/configMixin'; -import { useMessage } from '../composables/useMessage'; +import messageMixin from '../mixins/messageMixin'; import { isASubmittedFormMessage } from 'shared/helpers/MessageTypeHelper'; import darkModeMixin from 'widget/mixins/darkModeMixin.js'; import ReplyToChip from 'widget/components/ReplyToChip.vue'; @@ -28,7 +28,7 @@ export default { MessageReplyButton, ReplyToChip, }, - mixins: [configMixin, darkModeMixin], + mixins: [configMixin, messageMixin, darkModeMixin], props: { message: { type: Object, @@ -39,15 +39,6 @@ export default { default: () => {}, }, }, - setup(props) { - const { messageContentAttributes, hasAttachments } = useMessage( - props.message - ); - return { - messageContentAttributes, - hasAttachments, - }; - }, data() { return { hasImageError: false, diff --git a/app/javascript/widget/components/UserMessage.vue b/app/javascript/widget/components/UserMessage.vue index 7c7d844e9..7f8528e08 100755 --- a/app/javascript/widget/components/UserMessage.vue +++ b/app/javascript/widget/components/UserMessage.vue @@ -6,7 +6,7 @@ import VideoBubble from 'widget/components/VideoBubble.vue'; import FluentIcon from 'shared/components/FluentIcon/Index.vue'; import FileBubble from 'widget/components/FileBubble.vue'; import { messageStamp } from 'shared/helpers/timeHelper'; -import { useMessage } from '../composables/useMessage'; +import messageMixin from '../mixins/messageMixin'; import ReplyToChip from 'widget/components/ReplyToChip.vue'; import DragWrapper from 'widget/components/DragWrapper.vue'; import { BUS_EVENTS } from 'shared/constants/busEvents'; @@ -25,6 +25,7 @@ export default { ReplyToChip, DragWrapper, }, + mixins: [messageMixin], props: { message: { type: Object, @@ -35,12 +36,6 @@ export default { default: () => {}, }, }, - setup(props) { - const { hasAttachments } = useMessage(props.message); - return { - hasAttachments, - }; - }, data() { return { hasImageError: false, diff --git a/app/javascript/widget/composables/specs/useMessage.spec.js b/app/javascript/widget/composables/specs/useMessage.spec.js deleted file mode 100644 index 1498c87af..000000000 --- a/app/javascript/widget/composables/specs/useMessage.spec.js +++ /dev/null @@ -1,71 +0,0 @@ -import { describe, it, expect } from 'vitest'; -import { reactive, nextTick } from 'vue'; -import { useMessage } from '../useMessage'; - -describe('useMessage', () => { - it('should handle deleted messages', () => { - const message = reactive({ - content_attributes: { deleted: true }, - attachments: [], - }); - - const { messageContentAttributes, hasAttachments } = useMessage(message); - - expect(messageContentAttributes.value).toEqual({ deleted: true }); - expect(hasAttachments.value).toBe(false); - }); - - it('should handle non-deleted messages with attachments', () => { - const message = reactive({ - content_attributes: {}, - attachments: ['attachment1', 'attachment2'], - }); - - const { messageContentAttributes, hasAttachments } = useMessage(message); - - expect(messageContentAttributes.value).toEqual({}); - expect(hasAttachments.value).toBe(true); - }); - - it('should handle messages without content_attributes', () => { - const message = reactive({ - attachments: [], - }); - - const { messageContentAttributes, hasAttachments } = useMessage(message); - - expect(messageContentAttributes.value).toEqual({}); - expect(hasAttachments.value).toBe(false); - }); - - it('should handle messages with empty attachments array', () => { - const message = reactive({ - content_attributes: { someAttribute: 'value' }, - attachments: [], - }); - - const { messageContentAttributes, hasAttachments } = useMessage(message); - - expect(messageContentAttributes.value).toEqual({ someAttribute: 'value' }); - expect(hasAttachments.value).toBe(false); - }); - - it('should update reactive properties when message changes', async () => { - const message = reactive({ - content_attributes: {}, - attachments: [], - }); - - const { messageContentAttributes, hasAttachments } = useMessage(message); - - expect(messageContentAttributes.value).toEqual({}); - expect(hasAttachments.value).toBe(false); - - message.content_attributes = { updated: true }; - message.attachments.push('newAttachment'); - await nextTick(); - - expect(messageContentAttributes.value).toEqual({ updated: true }); - expect(hasAttachments.value).toBe(true); - }); -}); diff --git a/app/javascript/widget/composables/useMessage.js b/app/javascript/widget/composables/useMessage.js deleted file mode 100644 index 97b7a7800..000000000 --- a/app/javascript/widget/composables/useMessage.js +++ /dev/null @@ -1,22 +0,0 @@ -import { computed } from 'vue'; - -/** - * Composable for handling message-related computations. - * @param {Object} message - The message object to be processed. - * @returns {Object} An object containing computed properties for message content and attachments. - */ -export function useMessage(message) { - const messageContentAttributes = computed(() => { - const { content_attributes: attribute = {} } = message; - return attribute; - }); - - const hasAttachments = computed(() => { - return !!(message.attachments && message.attachments.length > 0); - }); - - return { - messageContentAttributes, - hasAttachments, - }; -} diff --git a/app/javascript/widget/mixins/messageMixin.js b/app/javascript/widget/mixins/messageMixin.js new file mode 100644 index 000000000..dfa1e0e6a --- /dev/null +++ b/app/javascript/widget/mixins/messageMixin.js @@ -0,0 +1,13 @@ +export default { + computed: { + messageContentAttributes() { + const { content_attributes: attribute = {} } = this.message; + return attribute; + }, + hasAttachments() { + return !!( + this.message.attachments && this.message.attachments.length > 0 + ); + }, + }, +}; diff --git a/app/javascript/widget/mixins/specs/messageMixin.spec.js b/app/javascript/widget/mixins/specs/messageMixin.spec.js new file mode 100644 index 000000000..a6443ccd6 --- /dev/null +++ b/app/javascript/widget/mixins/specs/messageMixin.spec.js @@ -0,0 +1,37 @@ +import { shallowMount } from '@vue/test-utils'; +import messageMixin from '../messageMixin'; +import messages from './messageFixture'; + +describe('messageMixin', () => { + let Component = { + render() {}, + title: 'TestComponent', + mixins: [messageMixin], + }; + + it('deleted messages', async () => { + const wrapper = shallowMount(Component, { + data() { + return { + message: messages.deletedMessage, + }; + }, + }); + expect(wrapper.vm.messageContentAttributes).toEqual({ + deleted: true, + }); + expect(wrapper.vm.hasAttachments).toBe(false); + }); + it('non-deleted messages', async () => { + const wrapper = shallowMount(Component, { + data() { + return { + message: messages.nonDeletedMessage, + }; + }, + }); + expect(wrapper.vm.deletedMessage).toBe(undefined); + expect(wrapper.vm.messageContentAttributes).toEqual({}); + expect(wrapper.vm.hasAttachments).toBe(true); + }); +}); From abc511d00f5105bebde146cb23752bc8598a6e0e Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 22 Aug 2024 23:04:14 +0530 Subject: [PATCH 3/3] fix: inconsistent OpenAI cache interface (#10009) Signed-off-by: Shivam Mishra Co-authored-by: Muhsin Keloth --- .../accounts/integrations/hooks_controller.rb | 7 ++++++- .../conversation/LabelSuggestion.vue | 2 +- lib/integrations/openai_base_service.rb | 17 +++++++++++++++-- lib/redis/redis_keys.rb | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/accounts/integrations/hooks_controller.rb b/app/controllers/api/v1/accounts/integrations/hooks_controller.rb index 13bb2738b..087a9b78d 100644 --- a/app/controllers/api/v1/accounts/integrations/hooks_controller.rb +++ b/app/controllers/api/v1/accounts/integrations/hooks_controller.rb @@ -12,7 +12,12 @@ class Api::V1::Accounts::Integrations::HooksController < Api::V1::Accounts::Base def process_event response = @hook.process_event(params[:event]) - if response[:error] + + # for cases like an invalid event, or when conversation does not have enough messages + # for a label suggestion, the response is nil + if response.nil? + render json: { message: nil } + elsif response[:error] render json: { error: response[:error] }, status: :unprocessable_entity else render json: { message: response[:message] } diff --git a/app/javascript/dashboard/components/widgets/conversation/conversation/LabelSuggestion.vue b/app/javascript/dashboard/components/widgets/conversation/conversation/LabelSuggestion.vue index 4593f25e2..da8488262 100644 --- a/app/javascript/dashboard/components/widgets/conversation/conversation/LabelSuggestion.vue +++ b/app/javascript/dashboard/components/widgets/conversation/conversation/LabelSuggestion.vue @@ -172,7 +172,7 @@ export default { delay: { show: 600, hide: 0 }, hideOnClick: true, }" - class="label-suggestion--option" + class="label-suggestion--option !px-0" @click="pushOrAddLabel(label.title)" >