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
mr26.0
Debora Crescenzo 3 months ago committed by Crescenzo Debora
parent 33aad6a501
commit 58e7a88272

@ -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({
return get({
path: `api/customerpreferences/${id}`
}).then((customerPreferences) => {
resolve(customerPreferences)
}).catch((err) => {
reject(err)
})
})
}
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({
export async function addCustomerPreference (customerId, fieldPath, value) {
return patchAddFull({
path: `api/customerpreferences/${customerId}`,
fieldPath: fieldName
}).then((customer) => {
resolve(customer.data)
fieldPath,
value
})
}
return patchReplaceFull({
}
export async function removeCustomerPreference (customerId, fieldPath) {
return patchRemoveFull({
path: `api/customerpreferences/${customerId}`,
fieldPath: fieldName,
value: customerValue
fieldPath
})
}).then((customer) => {
resolve(customer)
}).catch((err) => {
const errCode = `${err.status}`
if (errCode === '422') {
return patchAdd({
}
export async function setCustomerPreference (customerId, fieldPath, value) {
return patchReplaceFull({
path: `api/customerpreferences/${customerId}`,
fieldPath: fieldName,
value: customerValue
})
}
}).then((customer) => {
resolve(customer.data)
}).catch((err) => {
reject(err)
})
fieldPath,
value
})
}

@ -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 () {

@ -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
})
}
}

Loading…
Cancel
Save