diff --git a/src/api/subscriber.js b/src/api/subscriber.js index eeba9297..1edc3d21 100644 --- a/src/api/subscriber.js +++ b/src/api/subscriber.js @@ -8,6 +8,7 @@ import { patchAdd, patchAddFull, patchRemove, + patchRemoveFull, patchReplace, patchReplaceFull } from 'src/api/common' @@ -804,49 +805,31 @@ export async function getSubscriberProfile (id) { } export function getCustomerPreference (id) { - return new Promise((resolve, reject) => { - get({ - path: `api/customerpreferences/${id}` - }).then((customerPreferences) => { - resolve(customerPreferences) - }).catch((err) => { - reject(err) - }) + return get({ + path: `api/customerpreferences/${id}` }) } -export function setCustomerPreference (customerId, customerValue, fieldName) { - return new Promise((resolve, reject) => { - Promise.resolve().then(() => { - if (customerValue === undefined || customerValue === null || customerValue === '' || (Array.isArray(customerValue) && !customerValue.length)) { - return patchRemove({ - path: `api/customerpreferences/${customerId}`, - fieldPath: fieldName - }).then((customer) => { - resolve(customer.data) - }) - } - return patchReplaceFull({ - path: `api/customerpreferences/${customerId}`, - fieldPath: fieldName, - value: customerValue - }) - }).then((customer) => { - resolve(customer) - }).catch((err) => { - const errCode = `${err.status}` - if (errCode === '422') { - return patchAdd({ - path: `api/customerpreferences/${customerId}`, - fieldPath: fieldName, - value: customerValue - }) - } - }).then((customer) => { - resolve(customer.data) - }).catch((err) => { - reject(err) - }) +export async function addCustomerPreference (customerId, fieldPath, value) { + return patchAddFull({ + path: `api/customerpreferences/${customerId}`, + fieldPath, + value + }) +} + +export async function removeCustomerPreference (customerId, fieldPath) { + return patchRemoveFull({ + path: `api/customerpreferences/${customerId}`, + fieldPath + }) +} + +export async function setCustomerPreference (customerId, fieldPath, value) { + return patchReplaceFull({ + path: `api/customerpreferences/${customerId}`, + fieldPath, + value }) } diff --git a/src/pages/CscPageCustomerPreferences.vue b/src/pages/CscPageCustomerPreferences.vue index 4a68f1ff..11c65d63 100644 --- a/src/pages/CscPageCustomerPreferences.vue +++ b/src/pages/CscPageCustomerPreferences.vue @@ -259,7 +259,7 @@ export default { ignoreMembers () { this.updateIgnoreMembers({ customerId: this.getCustomerId, - ignore_cf: this.changes.ignore_cf_when_hunting + ignore_cf_when_hunting: this.changes.ignore_cf_when_hunting }) }, blockInMode () { diff --git a/src/store/customer.js b/src/store/customer.js index d4fdc90c..9e4a09d2 100644 --- a/src/store/customer.js +++ b/src/store/customer.js @@ -1,9 +1,33 @@ import { + addCustomerPreference, getCustomerPreference, + removeCustomerPreference, setCustomerPreference } from 'src/api/subscriber' import { RequestState } from 'src/store/common' +async function updateBooleanPreference (context, { customerId, key, value }) { + await savePreference(context, () => { + const preferences = Object.keys(context.state.customerPreferences ?? []) + if (preferences.includes(key)) { + return value + ? setCustomerPreference(customerId, key, value) + : removeCustomerPreference(customerId, key) + } + + return addCustomerPreference(customerId, key, value) + }) +} + +async function savePreference (context, apiFn) { + try { + const result = await apiFn() + context.commit('customerPreferencesUpdateLoaded', result) + } catch (err) { + context.commit('customerPreferencesLoadingFailed', err.message) + } +} + export default { namespaced: true, state: { @@ -14,7 +38,7 @@ export default { }, getters: { ignoreMembers (state) { - return state.ignoreMembers + return state.customerPreferences?.ignore_members_when_hunting } }, mutations: { @@ -48,67 +72,88 @@ export default { context.commit('customerPreferencesLoadingFailed', err.message) } }, - updateIgnoreMembers (context, options) { - setCustomerPreference(options.customerId, options.ignore_cf, 'ignore_cf_when_hunting').then((customerPreference) => { - context.commit('customerPreferencesUpdateLoaded', customerPreference) - }).catch((err) => { - context.commit('customerPreferencesLoadingFailed', err.message) + async updateIgnoreMembers (context, data) { + return updateBooleanPreference(context, { + customerId: data.customerId, + key: 'ignore_cf_when_hunting', + value: data.ignore_cf_when_hunting }) }, - updateBlockInMode (context, options) { - setCustomerPreference(options.customerId, options.block_in_mode, 'block_in_mode').then((customerPreference) => { - context.commit('customerPreferencesUpdateLoaded', customerPreference) - }).catch((err) => { - context.commit('customerPreferencesLoadingFailed', err.message) + async updateBlockInMode (context, data) { + return updateBooleanPreference(context, { + customerId: data.customerId, + key: 'block_in_mode', + value: data.block_in_mode }) }, - updateBlockInClir (context, options) { - setCustomerPreference(options.customerId, options.block_in_clir, 'block_in_clir').then((customerPreference) => { - context.commit('customerPreferencesUpdateLoaded', customerPreference) - }).catch((err) => { - context.commit('customerPreferencesLoadingFailed', err.message) + async updateBlockInClir (context, data) { + return updateBooleanPreference(context, { + customerId: data.customerId, + key: 'block_in_clir', + value: data.block_in_clir }) }, - updateBlockOutMode (context, options) { - setCustomerPreference(options.customerId, options.block_out_mode, 'block_out_mode').then((customerPreference) => { - context.commit('customerPreferencesUpdateLoaded', customerPreference) - }).catch((err) => { - context.commit('customerPreferencesLoadingFailed', err.message) + async updateBlockOutMode (context, data) { + return updateBooleanPreference(context, { + customerId: data.customerId, + key: 'block_out_mode', + value: data.block_out_mode }) }, - updateBlockInList (context, options) { - setCustomerPreference(options.customerId, options.block_in_list, 'block_in_list').then((customerPreference) => { - context.commit('customerPreferencesUpdateLoaded', customerPreference) - }).catch((err) => { - context.commit('customerPreferencesLoadingFailed', err.message) + async updateBlockInList (context, data) { + await savePreference(context, () => { + if (!Array.isArray(data.block_in_list) || !data.block_in_list.length) { + return removeCustomerPreference(data.customerId, 'block_in_list') + } + + const preferences = Object.keys(context.state.customerPreferences ?? []) + if (preferences.includes('block_in_list')) { + return setCustomerPreference(data.customerId, 'block_in_list', data.block_in_list) + } + + return addCustomerPreference(data.customerId, 'block_in_list', data.block_in_list) }) }, - updateBlockOutList (context, options) { - setCustomerPreference(options.customerId, options.block_out_list, 'block_out_list').then((customerPreference) => { - context.commit('customerPreferencesUpdateLoaded', customerPreference) - }).catch((err) => { - context.commit('customerPreferencesLoadingFailed', err.message) + async updateBlockOutList (context, data) { + await savePreference(context, () => { + if (!Array.isArray(data.block_out_list) || !data.block_out_list.length) { + return removeCustomerPreference(data.customerId, 'block_out_list') + } + + const preferences = Object.keys(context.state.customerPreferences ?? []) + if (preferences.includes('block_out_list')) { + return setCustomerPreference(data.customerId, 'block_out_list', data.block_out_list) + } + + return addCustomerPreference(data.customerId, 'block_out_list', data.block_out_list) }) }, - updateBlockOutOverridePin (context, options) { - setCustomerPreference(options.customerId, options.block_out_override_pin, 'block_out_override_pin').then((customerPreference) => { - context.commit('customerPreferencesUpdateLoaded', customerPreference) - }).catch((err) => { - context.commit('customerPreferencesLoadingFailed', err.message) + async updateBlockOutOverridePin (context, data) { + await savePreference(context, () => { + if (!data.block_out_override_pin?.trim()) { + return removeCustomerPreference(data.customerId, 'block_out_override_pin') + } + + const preferences = Object.keys(context.state.customerPreferences ?? []) + if (preferences.includes('block_out_override_pin')) { + return setCustomerPreference(data.customerId, 'block_out_override_pin', data.block_out_override_pin) + } + + return addCustomerPreference(data.customerId, 'block_out_override_pin', data.block_out_override_pin) }) }, - updatePlayAnnounceBeforeCallSetup (context, options) { - setCustomerPreference(options.customerId, options.play_announce_before_call_setup, 'play_announce_before_call_setup').then((customerPreference) => { - context.commit('customerPreferencesUpdateLoaded', customerPreference) - }).catch((err) => { - context.commit('customerPreferencesLoadingFailed', err.message) + async updatePlayAnnounceBeforeCallSetup (context, data) { + return updateBooleanPreference(context, { + customerId: data.customerId, + key: 'play_announce_before_call_setup', + value: data.play_announce_before_call_setup }) }, - updatePlayAnnounceToCallee (context, options) { - setCustomerPreference(options.customerId, options.play_announce_to_callee, 'play_announce_to_callee').then((customerPreference) => { - context.commit('customerPreferencesUpdateLoaded', customerPreference) - }).catch((err) => { - context.commit('customerPreferencesLoadingFailed', err.message) + async updatePlayAnnounceToCallee (context, data) { + return updateBooleanPreference(context, { + customerId: data.customerId, + key: 'play_announce_to_callee', + value: data.play_announce_to_callee }) } }