MT#64726 Refactor customer preferences actions

- Convert all actions from .then()/.catch() chains to async/await
- Extract savePreference() helper to centralise error handling and
  state commits, avoiding repetition across every action
- Extract updateBooleanPreference() helper. It covers the following
  scenarios:
  * new value is false: remove PATCH property in DB
  * new value is true: add PATCH property in DB
  * new value is true AND the value already exist in the
    customer preferences: replace PATCH to amend property in DB
    (added to avoid regression introduced by previous code, which
    saved a property as false instead of removing it)s
- Add proper add/set/remove logic to updateBlockInList,
  updateBlockOutList and updateBlockOutOverridePin: previously all
  three always called setCustomerPreference regardless of whether the
  preference existed or was empty
- Split logic to add, remove and edit customer preferences

Change-Id: Ie419de31631b9ef1b06653446dfbe1a33a10c225
(cherry picked from commit 58e7a88272)
(cherry picked from commit 625596e549)
mr13.5.1
Debora Crescenzo 3 months ago committed by Crescenzo Debora
parent a66023fede
commit 1e83c730e6

@ -8,6 +8,7 @@ import {
patchAdd, patchAdd,
patchAddFull, patchAddFull,
patchRemove, patchRemove,
patchRemoveFull,
patchReplace, patchReplace,
patchReplaceFull patchReplaceFull
} from 'src/api/common' } from 'src/api/common'
@ -804,49 +805,31 @@ export async function getSubscriberProfile (id) {
} }
export function getCustomerPreference (id) { export function getCustomerPreference (id) {
return new Promise((resolve, reject) => { return get({
get({ path: `api/customerpreferences/${id}`
path: `api/customerpreferences/${id}`
}).then((customerPreferences) => {
resolve(customerPreferences)
}).catch((err) => {
reject(err)
})
}) })
} }
export function setCustomerPreference (customerId, customerValue, fieldName) { export async function addCustomerPreference (customerId, fieldPath, value) {
return new Promise((resolve, reject) => { return patchAddFull({
Promise.resolve().then(() => { path: `api/customerpreferences/${customerId}`,
if (customerValue === undefined || customerValue === null || customerValue === '' || (Array.isArray(customerValue) && !customerValue.length)) { fieldPath,
return patchRemove({ value
path: `api/customerpreferences/${customerId}`, })
fieldPath: fieldName }
}).then((customer) => {
resolve(customer.data) export async function removeCustomerPreference (customerId, fieldPath) {
}) return patchRemoveFull({
} path: `api/customerpreferences/${customerId}`,
return patchReplaceFull({ fieldPath
path: `api/customerpreferences/${customerId}`, })
fieldPath: fieldName, }
value: customerValue
}) export async function setCustomerPreference (customerId, fieldPath, value) {
}).then((customer) => { return patchReplaceFull({
resolve(customer) path: `api/customerpreferences/${customerId}`,
}).catch((err) => { fieldPath,
const errCode = `${err.status}` value
if (errCode === '422') {
return patchAdd({
path: `api/customerpreferences/${customerId}`,
fieldPath: fieldName,
value: customerValue
})
}
}).then((customer) => {
resolve(customer.data)
}).catch((err) => {
reject(err)
})
}) })
} }

@ -259,7 +259,7 @@ export default {
ignoreMembers () { ignoreMembers () {
this.updateIgnoreMembers({ this.updateIgnoreMembers({
customerId: this.getCustomerId, customerId: this.getCustomerId,
ignore_cf: this.changes.ignore_cf_when_hunting ignore_cf_when_hunting: this.changes.ignore_cf_when_hunting
}) })
}, },
blockInMode () { blockInMode () {

@ -1,9 +1,33 @@
import { import {
addCustomerPreference,
getCustomerPreference, getCustomerPreference,
removeCustomerPreference,
setCustomerPreference setCustomerPreference
} from 'src/api/subscriber' } from 'src/api/subscriber'
import { RequestState } from 'src/store/common' 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 { export default {
namespaced: true, namespaced: true,
state: { state: {
@ -14,7 +38,7 @@ export default {
}, },
getters: { getters: {
ignoreMembers (state) { ignoreMembers (state) {
return state.ignoreMembers return state.customerPreferences?.ignore_members_when_hunting
} }
}, },
mutations: { mutations: {
@ -48,67 +72,88 @@ export default {
context.commit('customerPreferencesLoadingFailed', err.message) context.commit('customerPreferencesLoadingFailed', err.message)
} }
}, },
updateIgnoreMembers (context, options) { async updateIgnoreMembers (context, data) {
setCustomerPreference(options.customerId, options.ignore_cf, 'ignore_cf_when_hunting').then((customerPreference) => { return updateBooleanPreference(context, {
context.commit('customerPreferencesUpdateLoaded', customerPreference) customerId: data.customerId,
}).catch((err) => { key: 'ignore_cf_when_hunting',
context.commit('customerPreferencesLoadingFailed', err.message) value: data.ignore_cf_when_hunting
}) })
}, },
updateBlockInMode (context, options) { async updateBlockInMode (context, data) {
setCustomerPreference(options.customerId, options.block_in_mode, 'block_in_mode').then((customerPreference) => { return updateBooleanPreference(context, {
context.commit('customerPreferencesUpdateLoaded', customerPreference) customerId: data.customerId,
}).catch((err) => { key: 'block_in_mode',
context.commit('customerPreferencesLoadingFailed', err.message) value: data.block_in_mode
}) })
}, },
updateBlockInClir (context, options) { async updateBlockInClir (context, data) {
setCustomerPreference(options.customerId, options.block_in_clir, 'block_in_clir').then((customerPreference) => { return updateBooleanPreference(context, {
context.commit('customerPreferencesUpdateLoaded', customerPreference) customerId: data.customerId,
}).catch((err) => { key: 'block_in_clir',
context.commit('customerPreferencesLoadingFailed', err.message) value: data.block_in_clir
}) })
}, },
updateBlockOutMode (context, options) { async updateBlockOutMode (context, data) {
setCustomerPreference(options.customerId, options.block_out_mode, 'block_out_mode').then((customerPreference) => { return updateBooleanPreference(context, {
context.commit('customerPreferencesUpdateLoaded', customerPreference) customerId: data.customerId,
}).catch((err) => { key: 'block_out_mode',
context.commit('customerPreferencesLoadingFailed', err.message) value: data.block_out_mode
}) })
}, },
updateBlockInList (context, options) { async updateBlockInList (context, data) {
setCustomerPreference(options.customerId, options.block_in_list, 'block_in_list').then((customerPreference) => { await savePreference(context, () => {
context.commit('customerPreferencesUpdateLoaded', customerPreference) if (!Array.isArray(data.block_in_list) || !data.block_in_list.length) {
}).catch((err) => { return removeCustomerPreference(data.customerId, 'block_in_list')
context.commit('customerPreferencesLoadingFailed', err.message) }
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) { async updateBlockOutList (context, data) {
setCustomerPreference(options.customerId, options.block_out_list, 'block_out_list').then((customerPreference) => { await savePreference(context, () => {
context.commit('customerPreferencesUpdateLoaded', customerPreference) if (!Array.isArray(data.block_out_list) || !data.block_out_list.length) {
}).catch((err) => { return removeCustomerPreference(data.customerId, 'block_out_list')
context.commit('customerPreferencesLoadingFailed', err.message) }
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) { async updateBlockOutOverridePin (context, data) {
setCustomerPreference(options.customerId, options.block_out_override_pin, 'block_out_override_pin').then((customerPreference) => { await savePreference(context, () => {
context.commit('customerPreferencesUpdateLoaded', customerPreference) if (!data.block_out_override_pin?.trim()) {
}).catch((err) => { return removeCustomerPreference(data.customerId, 'block_out_override_pin')
context.commit('customerPreferencesLoadingFailed', err.message) }
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) { async updatePlayAnnounceBeforeCallSetup (context, data) {
setCustomerPreference(options.customerId, options.play_announce_before_call_setup, 'play_announce_before_call_setup').then((customerPreference) => { return updateBooleanPreference(context, {
context.commit('customerPreferencesUpdateLoaded', customerPreference) customerId: data.customerId,
}).catch((err) => { key: 'play_announce_before_call_setup',
context.commit('customerPreferencesLoadingFailed', err.message) value: data.play_announce_before_call_setup
}) })
}, },
updatePlayAnnounceToCallee (context, options) { async updatePlayAnnounceToCallee (context, data) {
setCustomerPreference(options.customerId, options.play_announce_to_callee, 'play_announce_to_callee').then((customerPreference) => { return updateBooleanPreference(context, {
context.commit('customerPreferencesUpdateLoaded', customerPreference) customerId: data.customerId,
}).catch((err) => { key: 'play_announce_to_callee',
context.commit('customerPreferencesLoadingFailed', err.message) value: data.play_announce_to_callee
}) })
} }
} }

Loading…
Cancel
Save