diff --git a/src/api/pbx-callqueues.js b/src/api/pbx-callqueues.js index c6c1075c..65a5f337 100644 --- a/src/api/pbx-callqueues.js +++ b/src/api/pbx-callqueues.js @@ -1,9 +1,10 @@ import _ from 'lodash' import { - addPreference, addPreferenceFull, getAllPreferences, - getSubscriber + getSubscriber, + removeCallQueueConfig, + setPreference } from 'src/api/subscriber' export function getCallQueues () { @@ -24,63 +25,29 @@ export function getCallQueues () { }) } -export function getCallQueueList () { - return new Promise((resolve, reject) => { - let callQueues = [] - Promise.resolve().then(() => { - return getCallQueues() - }).then(($callQueues) => { - callQueues = $callQueues - const subscriberPromises = [] - callQueues.items.forEach((callQueue) => { - subscriberPromises.push(getSubscriber(callQueue.id)) - }) - return Promise.all(subscriberPromises) - }).then((subscribers) => { - resolve({ - subscribers: { - items: subscribers - }, - callQueues - }) - }).catch((err) => { - reject(err) - }) - }) +export async function getCallQueueList () { + const callQueues = await getCallQueues() + const subscribers = await Promise.all( + callQueues.items.map((callQueue) => getSubscriber(callQueue.subscriber_id)) + ) + return { + subscribers: { + items: subscribers + }, + callQueues + } } -/** - * @param options.subscriber_id - * @param options.max_queue_length - * @param options.queue_wrap_up_time - * @return {Promise} - */ -export function createCallQueue (options) { - return new Promise((resolve, reject) => { - Promise.resolve().then(() => { - return Promise.all([ - addPreference(options.subscriber_id, 'cloud_pbx_callqueue', true), - addPreference(options.subscriber_id, 'max_queue_length', options.max_queue_length), - addPreference(options.subscriber_id, 'queue_wrap_up_time', options.queue_wrap_up_time) - ]) - }).then(() => { - resolve() - }).catch((err) => { - reject(err) - }) - }) +export async function createCallQueue ({ subscriberId, maxQueueLength, queueWrapUpTime }) { + await Promise.all([ + setPreference(subscriberId, 'cloud_pbx_callqueue', true), + setPreference(subscriberId, 'max_queue_length', maxQueueLength), + setPreference(subscriberId, 'queue_wrap_up_time', queueWrapUpTime) + ]) } -export function removeCallQueue (subscriberId) { - return new Promise((resolve, reject) => { - Promise.resolve().then(() => { - return addPreference(subscriberId, 'cloud_pbx_callqueue', false) - }).then(() => { - resolve() - }).catch((err) => { - reject(err) - }) - }) +export async function removeCallQueue (callQueueId) { + await removeCallQueueConfig(callQueueId) } export function setCallQueueMaxLength (options) { diff --git a/src/api/subscriber.js b/src/api/subscriber.js index 25709f34..b6ffd8ce 100644 --- a/src/api/subscriber.js +++ b/src/api/subscriber.js @@ -21,6 +21,11 @@ const generateNumbers = '0123456789' const generateLowercase = 'abcdefghijklmnopqrstuvwxyz' const generateUppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' const generateLength = 12 +export const CALL_QUEUE_PREFERENCE_FIELDS = [ + 'cloud_pbx_callqueue', + 'max_queue_length', + 'queue_wrap_up_time' +] export function getPreferences (id) { return new Promise((resolve, reject) => { @@ -577,9 +582,10 @@ export function setWrapUpTime (id, wrapUpTime) { return editCallQueuePreference(id, { queue_wrap_up_time: wrapUpTime }) } -export function removeCallQueueConfig (subscriberId) { - const param = { cloud_pbx_callqueue: false } - return httpApi.put(`api/subscriberpreferences/${subscriberId}`, param) +export async function removeCallQueueConfig (subscriberId) { + const preferences = await getPreferences(subscriberId) + const fields = CALL_QUEUE_PREFERENCE_FIELDS.filter((field) => Object.hasOwn(preferences, field)) + await Promise.all(fields.map((field) => removePreference(subscriberId, field))) } export function getAllPreferences (options) { diff --git a/src/boot/routes.js b/src/boot/routes.js index 574e2863..8e366625 100644 --- a/src/boot/routes.js +++ b/src/boot/routes.js @@ -6,6 +6,8 @@ import { import { store } from 'src/boot/store' export default ({ app, router }) => { + store.$router = router + router.beforeEach((to, from, next) => { const publicUrls = ['/login', '/recoverpassword', '/changepassword'] // not authorized user diff --git a/src/components/pages/PbxConfiguration/CscPbxCallQueue.vue b/src/components/pages/PbxConfiguration/CscPbxCallQueue.vue index 87f7da52..3409495e 100644 --- a/src/components/pages/PbxConfiguration/CscPbxCallQueue.vue +++ b/src/components/pages/PbxConfiguration/CscPbxCallQueue.vue @@ -267,6 +267,7 @@ export default { if (this.hasMaxQueueLengthChanged && this.v$.changes.max_queue_length.$errors.length <= 0) { this.$emit('save-max-queue-length', { callQueueId: this.callQueue.id, + subscriberId: this.callQueue.subscriber_id, maxQueueLength: this.changes.max_queue_length }) } @@ -275,6 +276,7 @@ export default { if (this.hasQueueWrapUpTimeChanged && this.v$.changes.queue_wrap_up_time.$errors.length <= 0) { this.$emit('save-queue-wrap-up-time', { callQueueId: this.callQueue.id, + subscriberId: this.callQueue.subscriber_id, queueWrapUpTime: this.changes.queue_wrap_up_time }) } diff --git a/src/components/pages/PbxConfiguration/CscPbxCallQueueAddForm.vue b/src/components/pages/PbxConfiguration/CscPbxCallQueueAddForm.vue index 5a619475..d4857cb4 100644 --- a/src/components/pages/PbxConfiguration/CscPbxCallQueueAddForm.vue +++ b/src/components/pages/PbxConfiguration/CscPbxCallQueueAddForm.vue @@ -187,7 +187,11 @@ export default { this.$emit('cancel') }, save () { - this.$emit('submit', this.data) + this.$emit('submit', { + subscriberId: this.data.subscriber_id, + maxQueueLength: this.data.max_queue_length, + queueWrapUpTime: this.data.queue_wrap_up_time + }) }, reset () { this.data = this.getDefaults() diff --git a/src/pages/CscPagePbxCallQueues.vue b/src/pages/CscPagePbxCallQueues.vue index 08b4d504..5e10df5e 100644 --- a/src/pages/CscPagePbxCallQueues.vue +++ b/src/pages/CscPagePbxCallQueues.vue @@ -52,7 +52,7 @@ :loading="isCallQueueLoading(callQueue.id)" :expanded="isCallQueueExpanded(callQueue.id)" :call-queue="callQueue" - :subscriber="subscriberMap[callQueue.id]" + :subscriber="subscriberMap[callQueue.subscriber_id]" :default-max-queue-length="defaultMaxQueueLength" :default-queue-wrap-up-time="defaultQueueWrapUpTime" @remove="openCallQueueRemovalDialog" @@ -89,7 +89,6 @@ import CscRemoveDialog from 'components/CscRemoveDialog' import CscPbxCallQueue from 'components/pages/PbxConfiguration/CscPbxCallQueue' import CscPbxCallQueueAddForm from 'components/pages/PbxConfiguration/CscPbxCallQueueAddForm' import CscFade from 'components/transitions/CscFade' -import { getSubscriberId } from 'src/auth' import { showGlobalError, showToast @@ -195,7 +194,7 @@ export default { } }, mounted () { - this.loadCallQueueList({ subscriberId: getSubscriberId() }) + this.loadCallQueueList() }, methods: { ...mapActions('pbx', [ diff --git a/src/pages/CscPagePbxSettingsCallQueues.vue b/src/pages/CscPagePbxSettingsCallQueues.vue index f56f8d1c..9eef3ef1 100644 --- a/src/pages/CscPagePbxSettingsCallQueues.vue +++ b/src/pages/CscPagePbxSettingsCallQueues.vue @@ -180,7 +180,8 @@ export default { methods: { ...mapWaitingActions('callSettings', { loadSubscriberPreferencesAction: 'csc-pbx-call-settings-load-preferences', - fieldUpdateAction: 'csc-pbx-call-settings-update-preferences' + fieldUpdateAction: 'csc-pbx-call-settings-update-preferences', + removeCallQueueAction: 'csc-pbx-call-settings-update-preferences' }), resetMaxQueueLength () { this.changes.max_queue_length = this.getDefaultData().max_queue_length @@ -219,10 +220,16 @@ export default { queue_wrap_up_time: this.subscriberPreferences.queue_wrap_up_time || this.defaultQueueWrapUpTime.toString(), subscriber_id: getSubscriberId() } - this.cloud_pbx_callqueue = this.subscriberPreferences.cloud_pbx_callqueue ? this.subscriberPreferences.cloud_pbx_callqueue : this.cloud_pbx_callqueue + this.cloud_pbx_callqueue = this.subscriberPreferences.cloud_pbx_callqueue === true }, - addOrRemoveCallQueue () { - this.fieldUpdateAction({ field: 'cloud_pbx_callqueue', value: this.cloud_pbx_callqueue }) + async addOrRemoveCallQueue () { + if (this.cloud_pbx_callqueue) { + await this.fieldUpdateAction({ field: 'cloud_pbx_callqueue', value: true }) + } else { + await this.removeCallQueueAction() + this.getCallQueue() + this.changes = this.getDefaultData() + } } } } diff --git a/src/store/call-settings.js b/src/store/call-settings.js index 8ee1d247..525425c6 100644 --- a/src/store/call-settings.js +++ b/src/store/call-settings.js @@ -1,6 +1,8 @@ import { + CALL_QUEUE_PREFERENCE_FIELDS, getPreferences, getPreferencesDefs, + removeCallQueueConfig, removePreference, setPreference } from 'src/api/subscriber' @@ -45,6 +47,9 @@ export default { subscriberPreferencesUpdate (state, { field, value }) { state.subscriberPreferences[field] = value }, + subscriberPreferencesRemove (state, field) { + delete state.subscriberPreferences[field] + }, preferencesDefsSucceeded (state, res) { state.preferencesDefs = res } @@ -62,6 +67,12 @@ export default { value: options.value }) }, + async removeCallQueueAction (context) { + await removeCallQueueConfig(context.getters.subscriberId) + CALL_QUEUE_PREFERENCE_FIELDS.forEach((field) => { + context.commit('subscriberPreferencesRemove', field) + }) + }, async loadPreferencesDefsAction (context) { const preferencesDefs = await getPreferencesDefs() context.commit('preferencesDefsSucceeded', preferencesDefs) diff --git a/src/store/pbx-callqueues.js b/src/store/pbx-callqueues.js index d4b9fbb7..a5f9ed34 100644 --- a/src/store/pbx-callqueues.js +++ b/src/store/pbx-callqueues.js @@ -1,5 +1,4 @@ import { i18n } from 'boot/i18n' -import _ from 'lodash' import { createCallQueue, getCallQueueList, @@ -7,7 +6,6 @@ import { setCallQueueMaxLength, setCallQueueWrapUpTime } from 'src/api/pbx-callqueues' -import { getPreferences } from 'src/api/subscriber' import { CreationState, RequestState } from 'src/store/common' export default { @@ -51,40 +49,40 @@ export default { return state.callQueueUpdateState === RequestState.requesting }, isCallQueueRemoving (state) { - return state.callQueueRemoving === RequestState.requesting + return state.callQueueRemovalState === RequestState.requesting }, isCallQueueLoading (state) { - return (id) => { + return (callQueueId) => { return (state.callQueueRemovalState === RequestState.requesting && - state.callQueueRemoving !== null && state.callQueueRemoving.id === id) || + state.callQueueRemoving !== null && state.callQueueRemoving.id === callQueueId) || (state.callQueueUpdateState === RequestState.requesting && - state.callQueueUpdating !== null && state.callQueueUpdating.id === id) + state.callQueueUpdating !== null && state.callQueueUpdating.id === callQueueId) } }, isCallQueueExpanded (state) { - return (id) => { - return state.callQueueSelected !== null && state.callQueueSelected.id === id + return (callQueueId) => { + return state.callQueueSelected !== null && state.callQueueSelected.id === callQueueId } }, getCallQueueRemoveDialogMessage (state) { if (state.callQueueRemoving !== null) { return i18n.global.t('You are about to remove call queue for {subscriber}', { - subscriber: state.subscriberMap[state.callQueueRemoving.id].display_name + subscriber: state.subscriberMap[state.callQueueRemoving.subscriber_id]?.display_name ?? '' }) } return '' }, getCallQueueRemovingName (state) { - const subscriber = _.get(state.subscriberMap, _.get(state.callQueueRemoving, 'id', null), null) - return _.get(subscriber, 'display_name', '') + const subscriber = state.subscriberMap[state.callQueueRemoving?.subscriber_id] + return subscriber?.display_name ?? '' }, getCallQueueCreatingName (state) { - const subscriber = _.get(state.subscriberMap, _.get(state.callQueueCreationData, 'subscriber_id', null), null) - return _.get(subscriber, 'display_name', '') + const subscriber = state.subscriberMap[state.callQueueCreationData?.subscriberId] + return subscriber?.display_name ?? '' }, getCallQueueUpdatingName (state) { - const subscriber = _.get(state.subscriberMap, _.get(state.callQueueUpdating, 'id', null), null) - return _.get(subscriber, 'display_name', '') + const subscriber = state.subscriberMap[state.callQueueUpdating?.subscriber_id] + return subscriber?.display_name ?? '' }, getCallQueueUpdatingField (state) { return state.callQueueUpdatingField @@ -119,11 +117,14 @@ export default { }, callQueueListSucceeded (state, callQueueList) { state.callQueueListState = RequestState.succeeded - state.callQueueList = _.get(callQueueList, 'callQueues.items', []) + state.callQueueList = callQueueList?.callQueues?.items ?? [] + state.callQueueMap = {} state.callQueueList.forEach((callQueue) => { state.callQueueMap[callQueue.id] = callQueue }) - _.get(callQueueList, 'subscribers.items', []).forEach((subscriber) => { + state.subscriberMap = {} + const subscribers = callQueueList?.subscribers?.items ?? [] + subscribers.forEach((subscriber) => { state.subscriberMap[subscriber.id] = subscriber }) state.callQueueListVisible = true @@ -167,15 +168,15 @@ export default { }, callQueueUpdateSucceeded (state, preferences) { state.callQueueUpdateState = RequestState.succeeded - if (preferences) { - for (let i = 0; i < state.callQueueList.length; i++) { - if (state.callQueueList[i].id === preferences.id) { - state.callQueueList[i] = preferences - } - } - delete state.callQueueMap[preferences.id] - state.callQueueMap[preferences.id] = preferences + if (!preferences) { + return + } + const callQueueId = preferences.id + const index = state.callQueueList.findIndex((callQueue) => callQueue.id === callQueueId) + if (index !== -1) { + state.callQueueList[index] = preferences } + state.callQueueMap[callQueueId] = preferences }, callQueueUpdateFailed (state, err) { state.callQueueUpdateState = RequestState.failed @@ -190,30 +191,29 @@ export default { expandCallQueue (state, callQueueId) { state.callQueueSelected = state.callQueueMap[callQueueId] }, + expandCallQueueBySubscriberId (state, subscriberId) { + state.callQueueSelected = state.callQueueList.find((callQueue) => { + return callQueue.subscriber_id === subscriberId + }) || null + }, collapseCallQueue (state) { state.callQueueSelected = null - }, - setDefaultQueueWrapUpTime (state, value) { - state.defaultQueueWrapUpTime = value } }, actions: { async loadCallQueueList (context, options) { - const subscriberId = _.get(options, 'subscriberId', null) - const listVisible = _.get(options, 'listVisible', false) - const selectedId = _.get(options, 'selectedId', null) + const listVisible = options?.listVisible ?? false + const selectedId = options?.selectedId ?? null + const selectedSubscriberId = options?.selectedSubscriberId ?? null context.commit('callQueueListRequesting', { listVisible }) try { const callQueueList = await getCallQueueList() context.commit('callQueueListSucceeded', callQueueList) - const subscriberPreferences = await getPreferences(subscriberId) - const wrapUpTime = _.get(subscriberPreferences, 'queue_wrap_up_time', 10) - context.commit('setDefaultQueueWrapUpTime', wrapUpTime) - if (selectedId !== null) { - context.commit('expandCallQueue', callQueueList) - context.commit('highlightCallQueue', callQueueList) + context.commit('expandCallQueue', selectedId) + } else if (selectedSubscriberId !== null) { + context.commit('expandCallQueueBySubscriberId', selectedSubscriberId) } } catch (err) { context.commit('callQueueListFailed', err.message) @@ -223,7 +223,8 @@ export default { context.commit('callQueueCreationRequesting', callQueueData) createCallQueue(callQueueData).then(() => { return context.dispatch('loadCallQueueList', { - listVisible: true + listVisible: true, + selectedSubscriberId: callQueueData.subscriberId }) }).then(() => { context.commit('callQueueCreationSucceeded') @@ -267,7 +268,7 @@ export default { }, jumpToCallQueue (context, subscriber) { this.$router?.push({ path: '/user/pbx-configuration/call-queues' }) - context.commit('expandCallQueue', subscriber.id) + context.commit('expandCallQueueBySubscriberId', subscriber.id) } } }