diff --git a/classic/src/view/pages/callforward/CallForwardController.js b/classic/src/view/pages/callforward/CallForwardController.js index 3e3a3376..f149b778 100644 --- a/classic/src/view/pages/callforward/CallForwardController.js +++ b/classic/src/view/pages/callforward/CallForwardController.js @@ -174,15 +174,13 @@ Ext.define('NgcpCsc.view.pages.callforward.CallForwardController', { // TODO optimize, too many nested loops affects performance. // Ex. Where possible use break Ext.each by return false; Ext.Ajax.request({ - url: '/api/cfdestinationsets/?subscriber_id=' + localStorage.getItem('subscriber_id'), + url: '/api/cfdestinationsets/?rows=100&subscriber_id=' + localStorage.getItem('subscriber_id'), success: function(response, opts) { var decodedResponse = Ext.decode(response.responseText); if (decodedResponse._embedded) { var cfdestinationsets = decodedResponse._embedded['ngcp:cfdestinationsets']; - cfdestinationsets[0].destinations = me.sortDestinationsetByPriority(cfdestinationsets[0].destinations); Ext.each(cfTypeArrayOfObjects, function (cfTypeObjects, index) { var cfType = cfTypes[index]; - cfType !== 'cft' && me.addCftOwnPhone(cfdestinationsets[0].destinations); // cfType is 'cft' we invoke addCftOwnPhone() Ext.each(cfTypeObjects, function(cfTypeObject) { var cfmappings = {}; cfmappings.destinationsetName = cfTypeObject.destinationset; @@ -191,6 +189,12 @@ Ext.define('NgcpCsc.view.pages.callforward.CallForwardController', { if (cfmappings.timesetName == routeTimeset) { Ext.each(cfdestinationsets, function(cfdestinationset) { if (cfdestinationset.name == cfmappings.destinationsetName && !cfmappings._modelCreated) { + cfdestinationset.destinations = me.sortDestinationsetByPriority(cfdestinationset.destinations); + if (cfType === 'cft') { + if (cfTypeObjects.length > 0) { + me.addCftOwnPhone(cfdestinationset.destinations); + } + }; for (item in cfdestinationset.destinations) { var destinationToDisplayInGrid = me.getDestinationFromSipId(cfdestinationset.destinations[item].destination); var destinationAnnouncementId = cfdestinationset.announcement_id; @@ -576,7 +580,7 @@ Ext.define('NgcpCsc.view.pages.callforward.CallForwardController', { store.getAt(index).set('after_termination', false); }); }; - // Set all cft records to after_termination true if cfu records exist + // "Greyes out" destination if appearing after a terminating destination if (store.findRecord('type', 'cfu') && store.findRecord('type', 'cft')) { Ext.each(store.getRange(), function(record) { if (!record.get('after_termination') && record.get('type') === 'cft') { @@ -584,7 +588,6 @@ Ext.define('NgcpCsc.view.pages.callforward.CallForwardController', { }; }); }; - // Sorts "own phone" to top plus prevents it from being reordered Ext.each(store.getRange(), function(record) { if (record.get('destination') === 'own phone') { record.set('label', 'first ring'); @@ -899,13 +902,75 @@ Ext.define('NgcpCsc.view.pages.callforward.CallForwardController', { this.fireEvent('showconfirmbox', title, question, sucessMsg, 'confirmCFRemoval', rec); }, + // setCfuAsType() sets type to cfu for all destinations with given destinationset id, and Returns + // the updated store + setCfuAsType: function (store, id) { + Ext.each(store.getRange(), function(record) { + if (record.get('destinationset_id') === id) { + record.set('type', 'cfu'); + } + }); + return store; + }, + + recordHasStoreAndOwnPhone: function (record) { + var store = record.store; + return store && record.get('destination') === 'own phone'; + }, + confirmCFRemoval: function(record) { - var me = this; + var $cf = this; var store = record.store; - if (store) { + var subscriberId = localStorage.getItem('subscriber_id'); + if ($cf.recordHasStoreAndOwnPhone(record)) { + store = $cf.setCfuAsType(store, record.get('destinationset_id')); + Ext.Ajax.request({ + url: '/api/cfmappings/' + localStorage.getItem('subscriber_id'), + method: 'GET', + jsonData: {}, + success: function(response) { + var decodedResponse = Ext.decode(response.responseText); + var cfuMappings = decodedResponse['cfu']; + var cftMappings = decodedResponse['cft']; + cfuMappings.push({ + "destinationset": record.get('destinationset_name'), + "sourceset": record.get('sourceset'), + "timeset": record.get('timeset') + }); + cftMappings = cftMappings.filter(function( obj ) { + return obj.destinationset !== record.get('destinationset_name'); + }); + Ext.Ajax.request({ + url: '/api/cfmappings/' + subscriberId, + method: 'PATCH', + headers: { + 'Content-Type': 'application/json-patch+json' + }, + // We are writing to two paths based on the call forwarding + // types we are manipulating. Removing the mapping from cft + // and adding a new mapping for cfu + jsonData: [{ + "op": "add", + "path": "/cft", + "value": cftMappings + }, { + "op": "add", + "path": "/cfu", + "value": cfuMappings + }], + success: function (response) { + store.sync(); + }, + failure: function(response) { + console.log('server-side failure with status code ' + response.status); + } + }); + } + }); + } else if (store) { store.remove(record); store.sync(); - me.setLabelTerminationType(store); + $cf.setLabelTerminationType(store); }; }, @@ -1129,7 +1194,7 @@ Ext.define('NgcpCsc.view.pages.callforward.CallForwardController', { var newDestination = destination === 'Voicemail' ? 'voicebox' : destination.toLowerCase(); var newTimeout = !timeout ? null : timeout; if (!store.last()) { // if store empty we need to create new destset - var newDestinationsetName = 'csc_defined_' + newType + '_' + Date.now(); + var newDestinationsetName = 'csc_' + newType + '_' + Date.now(); var subscriberId = localStorage.getItem('subscriber_id'); Ext.Ajax.request({ url: '/api/cfdestinationsets/',