diff --git a/src/api/call-forwarding.js b/src/api/call-forwarding.js index a846c4ef..c1feebbb 100644 --- a/src/api/call-forwarding.js +++ b/src/api/call-forwarding.js @@ -24,6 +24,20 @@ export async function cfLoadDestinationSets (subscriberId) { }) } +export async function cfGetMostRecentDestinationSetByName (params) { + const res = await getList({ + resource: 'cfdestinationsets', + params: { + name: params.name, + subscriber_id: params.subscriberId, + order_by: 'id', + order_by_direction: 'desc' + } + }) + + return res.items[0] +} + export async function cfLoadSourceSets (subscriberId) { return getList({ resource: 'cfsourcesets', diff --git a/src/store/call-forwarding/actions.js b/src/store/call-forwarding/actions.js index 68522174..30f236c1 100644 --- a/src/store/call-forwarding/actions.js +++ b/src/store/call-forwarding/actions.js @@ -1,4 +1,5 @@ import { + cfCreateDestinationSet, cfCreateOfficeHours, cfCreateSourceSet, cfCreateTimeSetDate, @@ -7,6 +8,7 @@ import { cfDeleteDestinationSet, cfDeleteSourceSet, cfDeleteTimeSet, + cfGetMostRecentDestinationSetByName, cfLoadDestinationSets, cfLoadMappingsFull, cfLoadSourceSets, @@ -61,22 +63,27 @@ export async function loadMappingsFull ({ dispatch, commit, rootGetters }, subsc export async function createMapping ({ dispatch, commit, state, rootGetters }, payload) { dispatch('wait/start', WAIT_IDENTIFIER, { root: true }) + const name = 'csc-' + v4() + const subscriberId = (payload.subscriberId) ? (payload.subscriberId) : rootGetters['user/getSubscriberId'] + + await cfCreateDestinationSet({ + name, + subscriber_id: subscriberId, + destinations: [createDefaultDestination()] + }) + + const destinationSet = await cfGetMostRecentDestinationSetByName({ + name, + subscriberId: subscriberId + }) + let type = payload.type if (payload.type === 'cfu' && state.mappings.cft && state.mappings.cft.length > 0) { type = 'cft' } - const subscriberId = (payload.subscriberId) ? (payload.subscriberId) : rootGetters['user/getSubscriberId'] const mappings = _.cloneDeep(state.mappings[type]) - const destinationSetId = await post({ - resource: 'cfdestinationsets', - body: { - name: 'csc-' + v4(), - subscriber_id: subscriberId, - destinations: [createDefaultDestination()] - } - }) mappings.push({ - destinationset_id: destinationSetId + destinationset_id: destinationSet.id }) const res = await Promise.all([ patchReplaceFull({