diff --git a/app/controllers/api/v1/widget/contacts_controller.rb b/app/controllers/api/v1/widget/contacts_controller.rb index ad818d7ac..135d2ced3 100644 --- a/app/controllers/api/v1/widget/contacts_controller.rb +++ b/app/controllers/api/v1/widget/contacts_controller.rb @@ -1,6 +1,9 @@ class Api::V1::Widget::ContactsController < Api::V1::Widget::BaseController + before_action :process_hmac + + def show; end + def update - process_hmac contact_identify_action = ContactIdentifyAction.new( contact: @contact, params: permitted_params.to_h.deep_symbolize_keys diff --git a/app/javascript/widget/App.vue b/app/javascript/widget/App.vue index 48e89096d..e0e08911c 100755 --- a/app/javascript/widget/App.vue +++ b/app/javascript/widget/App.vue @@ -150,6 +150,7 @@ export default { this.setPopoutDisplay(message.showPopoutButton); this.fetchAvailableAgents(websiteToken); this.setHideMessageBubble(message.hideMessageBubble); + this.$store.dispatch('contacts/get'); } else if (message.event === 'widget-visible') { this.scrollConversationToBottom(); } else if (message.event === 'set-current-url') { diff --git a/app/javascript/widget/api/contacts.js b/app/javascript/widget/api/contacts.js index a72bb4910..a246e6dc2 100644 --- a/app/javascript/widget/api/contacts.js +++ b/app/javascript/widget/api/contacts.js @@ -3,6 +3,9 @@ import { API } from 'widget/helpers/axios'; const buildUrl = endPoint => `/api/v1/${endPoint}${window.location.search}`; export default { + get() { + return API.get(buildUrl('widget/contact')); + }, update(identifier, userObject) { return API.patch(buildUrl('widget/contact'), { identifier, diff --git a/app/javascript/widget/store/modules/contacts.js b/app/javascript/widget/store/modules/contacts.js index 0ecae3a55..582b78414 100644 --- a/app/javascript/widget/store/modules/contacts.js +++ b/app/javascript/widget/store/modules/contacts.js @@ -1,7 +1,27 @@ import ContactsAPI from '../../api/contacts'; import { refreshActionCableConnector } from '../../helpers/actionCable'; +const state = { + currentUser: {}, +}; + +const SET_CURRENT_USER = 'SET_CURRENT_USER'; + +export const getters = { + getCurrentUser(_state) { + return _state.currentUser; + }, +}; + export const actions = { + get: async ({ commit }) => { + try { + const { data } = await ContactsAPI.get(); + commit(SET_CURRENT_USER, data); + } catch (error) { + // Ignore error + } + }, update: async ({ dispatch }, { identifier, user: userObject }) => { try { const user = { @@ -14,6 +34,7 @@ export const actions = { data: { pubsub_token: pubsubToken }, } = await ContactsAPI.update(identifier, user); + dispatch('get'); if (userObject.identifier_hash) { dispatch('conversation/clearConversations', {}, { root: true }); dispatch('conversation/fetchOldConversations', {}, { root: true }); @@ -33,10 +54,17 @@ export const actions = { }, }; +export const mutations = { + [SET_CURRENT_USER]($state, user) { + const { currentUser } = $state; + $state.currentUser = { ...currentUser, ...user }; + }, +}; + export default { namespaced: true, - state: {}, - getters: {}, + state, + getters, actions, - mutations: {}, + mutations, }; diff --git a/app/javascript/widget/store/modules/specs/contact/actions.spec.js b/app/javascript/widget/store/modules/specs/contact/actions.spec.js new file mode 100644 index 000000000..c16b0eb20 --- /dev/null +++ b/app/javascript/widget/store/modules/specs/contact/actions.spec.js @@ -0,0 +1,21 @@ +import { API } from 'widget/helpers/axios'; +import { actions } from '../../contacts'; + +const commit = jest.fn(); +jest.mock('widget/helpers/axios'); + +describe('#actions', () => { + describe('#update', () => { + it('sends correct actions', async () => { + const user = { + email: 'thoma@sphadikam.com', + name: 'Adu Thoma', + avatar_url: '', + identifier_hash: 'malana_hash', + }; + API.patch.mockResolvedValue({ data: { pubsub_token: 'token' } }); + await actions.update({ commit }, { identifier: 1, user }); + expect(commit.mock.calls).toEqual([]); + }); + }); +}); diff --git a/app/javascript/widget/store/modules/specs/contact/getters.spec.js b/app/javascript/widget/store/modules/specs/contact/getters.spec.js new file mode 100644 index 000000000..43b0f15d7 --- /dev/null +++ b/app/javascript/widget/store/modules/specs/contact/getters.spec.js @@ -0,0 +1,21 @@ +import { getters } from '../../contacts'; + +describe('#getters', () => { + it('getCurrentUser', () => { + const user = { + email: 'thoma@sphadikam.com', + name: 'Adu Thoma', + avatar_url: '', + identifier_hash: 'malana_hash', + }; + const state = { + currentUser: user, + }; + expect(getters.getCurrentUser(state)).toEqual({ + email: 'thoma@sphadikam.com', + name: 'Adu Thoma', + avatar_url: '', + identifier_hash: 'malana_hash', + }); + }); +}); diff --git a/app/javascript/widget/store/modules/specs/contact/mutations.spec.js b/app/javascript/widget/store/modules/specs/contact/mutations.spec.js new file mode 100644 index 000000000..fed202bf2 --- /dev/null +++ b/app/javascript/widget/store/modules/specs/contact/mutations.spec.js @@ -0,0 +1,17 @@ +import { mutations } from '../../contacts'; + +describe('#mutations', () => { + describe('#SET_CURRENT_USER', () => { + it('set current user', () => { + const user = { + email: 'thoma@sphadikam.com', + name: 'Adu Thoma', + avatar_url: '', + identifier_hash: 'malana_hash', + }; + const state = { currentUser: {} }; + mutations.SET_CURRENT_USER(state, user); + expect(state.currentUser).toEqual(user); + }); + }); +}); diff --git a/app/javascript/widget/views/Home.vue b/app/javascript/widget/views/Home.vue index 9753c5cd2..32f784ec7 100755 --- a/app/javascript/widget/views/Home.vue +++ b/app/javascript/widget/views/Home.vue @@ -114,13 +114,15 @@ export default { conversationSize: 'conversation/getConversationSize', groupedMessages: 'conversation/getGroupedConversation', isFetchingList: 'conversation/getIsFetchingList', + currentUser: 'contacts/getCurrentUser', }), currentView() { + const { email: currentUserEmail = '' } = this.currentUser; if (this.isHeaderCollapsed) { if (this.conversationSize) { return 'messageView'; } - if (this.preChatFormEnabled) { + if (this.preChatFormEnabled && !currentUserEmail) { return 'preChatFormView'; } return 'messageView'; diff --git a/app/views/api/v1/widget/contacts/show.json.jbuilder b/app/views/api/v1/widget/contacts/show.json.jbuilder new file mode 100644 index 000000000..9853ccbee --- /dev/null +++ b/app/views/api/v1/widget/contacts/show.json.jbuilder @@ -0,0 +1,3 @@ +json.id @contact.id +json.name @contact.name +json.email @contact.email diff --git a/config/routes.rb b/config/routes.rb index d5e1162fa..cedb58a12 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -150,7 +150,7 @@ Rails.application.routes.draw do post :transcript end end - resource :contact, only: [:update] + resource :contact, only: [:show, :update] resources :inbox_members, only: [:index] resources :labels, only: [:create, :destroy] end