@ -4728,37 +4728,6 @@ define(function(require) {
} ) ;
} ,
usersGetDevice : function ( deviceId , callback ) {
var self = this ;
self . callApi ( {
resource : 'device.get' ,
data : {
accountId : self . accountId ,
deviceId : deviceId
} ,
success : function ( data ) {
callback ( data . data ) ;
}
} ) ;
} ,
usersUpdateDevice : function ( data , callback ) {
var self = this ;
self . callApi ( {
resource : 'device.update' ,
data : {
accountId : self . accountId ,
data : data ,
deviceId : data . id
} ,
success : function ( data ) {
callback ( data . data ) ;
}
} ) ;
} ,
usersListDeviceUser : function ( userId , callback ) {
var self = this ;
@ -4838,146 +4807,146 @@ define(function(require) {
} ) ;
} ,
usersUpdateDevices : function ( data , userId , callbackAfterUpdate ) {
/ * *
* @ param { Object } data
* @ param { String [ ] } data . newDevices List of device IDs to assign
* @ param { String [ ] } data . oldDevices List of device IDs to unassign
* @ param { String } userId
* @ param { Function } callback
* /
usersUpdateDevices : function ( data , userId , callback ) {
var self = this ,
updateDevices = function ( userCallflow ) {
var listFnParallel = [ ] ,
updateDeviceRequest = function ( newDataDevice , callback ) {
self . usersUpdateDevice ( newDataDevice , function ( updatedDataDevice ) {
callback ( null , updatedDataDevice ) ;
} ) ;
} ;
_ . each ( data . newDevices , function ( deviceId ) {
listFnParallel . push ( function ( callback ) {
self . usersGetDevice ( deviceId , function ( data ) {
data . owner_id = userId ;
if ( data . device_type === 'mobile' ) {
self . usersSearchMobileCallflowsByNumber ( userId , data . mobile . mdn , function ( listCallflowData ) {
self . callApi ( {
resource : 'callflow.get' ,
data : {
accountId : self . accountId ,
callflowId : listCallflowData . id
} ,
success : function ( rawCallflowData , status ) {
var callflowData = rawCallflowData . data ;
if ( userCallflow ) {
$ . extend ( true , callflowData , {
owner_id : userId ,
flow : {
module : 'callflow' ,
data : {
id : userCallflow . id
}
}
} ) ;
} else {
$ . extend ( true , callflowData , {
owner_id : userId ,
flow : {
module : 'device' ,
data : {
id : deviceId
}
}
} ) ;
}
self . usersUpdateCallflow ( callflowData , function ( ) {
updateDeviceRequest ( data , callback ) ;
} ) ;
}
} ) ;
} ) ;
} else {
updateDeviceRequest ( data , callback ) ;
getUserMainCallflow = function getUserMainCallflow ( userId , next ) {
self . usersGetMainCallflow ( userId , _ . partial ( next , null ) ) ;
} ,
assignDeviceFactory = function assignDeviceFactory ( userId , userMainCallflowId , deviceId ) {
return _ . bind ( self . usersUpdateDeviceAssignmentFromUser , self , deviceId , userId , userMainCallflowId ) ;
} ,
unassignDeviceFactory = function unassignDeviceFactory ( deviceId ) {
return _ . bind ( self . usersUpdateDeviceAssignmentFromUser , self , deviceId , null , null ) ;
} ,
updateCallflowEndpoints = function updateCallflowEndpoints ( updatedEndpoints , callflowId , next ) {
self . patchCallflow ( {
data : {
callflowId : callflowId ,
data : {
flow : _ . isEmpty ( updatedEndpoints ) ? {
module : 'user' ,
data : {
can_call_self : false ,
endpoints : null ,
id : userId ,
timeout : 20
}
} : {
data : {
endpoints : updatedEndpoints
}
}
} ) ;
} ) ;
}
} ,
success : _ . partial ( next , null ) ,
error : _ . partial ( next , true )
} ) ;
_ . each ( data . oldDevices , function ( deviceId ) {
listFnParallel . push ( function ( callback ) {
self . usersGetDevice ( deviceId , function ( data ) {
delete data . owner_id ;
if ( data . device_type === 'mobile' ) {
self . usersSearchMobileCallflowsByNumber ( userId , data . mobile . mdn , function ( listCallflowData ) {
self . callApi ( {
resource : 'callflow.get' ,
data : {
accountId : self . accountId ,
callflowId : listCallflowData . id
} ,
success : function ( rawCallflowData , status ) {
var callflowData = rawCallflowData . data ;
delete callflowData . owner_id ;
$ . extend ( true , callflowData , {
flow : {
module : 'device' ,
data : {
id : deviceId
}
}
} ) ;
self . usersUpdateCallflow ( callflowData , function ( ) {
updateDeviceRequest ( data , callback ) ;
} ) ;
}
} ) ;
} ) ;
} else {
updateDeviceRequest ( data , callback ) ;
} ,
disableFindMeFollowMeForUserId = function disableFindMeFollowMeForUserId ( userId , next ) {
self . usersPatchUser ( {
data : {
userId : userId ,
data : {
smartpbx : {
find_me_follow_me : false
}
} ) ;
} ) ;
}
} ,
success : _ . partial ( next , null ) ,
error : _ . partial ( next , true )
} ) ;
} ,
maybeUpdateUserAndCallflow = function maybeUpdateUserAndCallflow ( userId , userCallflow , deviceIdsRemoved , next ) {
var hasRingGroupModule = _ . get ( userCallflow , 'flow.module' ) === 'ring_group' ,
currentEndpoints = _ . get ( userCallflow , 'flow.data.endpoints' , [ ] ) ,
updatedEndpoints = _ . reject ( currentEndpoints , _ . flow (
_ . partial ( _ . get , _ , 'id' ) ,
_ . partial ( _ . includes , deviceIdsRemoved )
) ) ,
whereEndpointsRemoved = _ . size ( updatedEndpoints ) < _ . size ( currentEndpoints ) ;
monster . parallel ( _ . flatten ( [
hasRingGroupModule && whereEndpointsRemoved ? [
_ . partial ( updateCallflowEndpoints , updatedEndpoints , userCallflow . id )
] : [ ] ,
hasRingGroupModule && whereEndpointsRemoved && _ . isEmpty ( updatedEndpoints ) ? [
_ . partial ( disableFindMeFollowMeForUserId , userId )
] : [ ]
] ) , next ) ;
} ,
updateEntities = function updateEntities ( userId , devices , userMainCallflow , next ) {
monster . parallel ( _ . flatten ( [
_ . map ( data . newDevices , _ . partial ( assignDeviceFactory , userId , _ . get ( userMainCallflow , 'id' ) ) ) ,
_ . map ( data . oldDevices , unassignDeviceFactory ) ,
_ . partial ( maybeUpdateUserAndCallflow , userId , userMainCallflow , data . oldDevices )
] ) , next ) ;
} ;
if ( data . oldDevices . length > 0 && userCallflow && userCallflow . flow . module === 'ring_group' ) {
var endpointsCount = userCallflow . flow . data . endpoints . length ;
userCallflow . flow . data . endpoints = _ . filter ( userCallflow . flow . data . endpoints , function ( endpoint ) {
return ( data . oldDevices . indexOf ( endpoint . id ) < 0 ) ;
} ) ;
monster . waterfall ( [
_ . partial ( getUserMainCallflow , userId ) ,
_ . partial ( updateEntities , userId , data )
] , callback ) ;
} ,
if ( userCallflow . flow . data . endpoints . length < endpointsCount ) {
if ( userCallflow . flow . data . endpoints . length === 0 ) {
userCallflow . flow . module = 'user' ;
userCallflow . flow . data = {
can_call_self : false ,
id : userId ,
timeout : '20'
} ;
listFnParallel . push ( function ( callback ) {
self . usersGetUser ( userId , function ( user ) {
user . smartpbx . find_me_follow_me . enabled = false ;
self . usersUpdateUser ( user , function ( data ) {
callback ( null , data ) ;
} ) ;
} ) ;
} ) ;
}
listFnParallel . push ( function ( callback ) {
self . usersUpdateCallflow ( userCallflow , function ( data ) {
callback ( null , data ) ;
} ) ;
} ) ;
}
usersUpdateDeviceAssignmentFromUser : function ( deviceId , userId , userMainCallflowId , mainCallback ) {
var self = this ,
getDevice = function getDevice ( deviceId , callback ) {
self . callApi ( {
resource : 'device.get' ,
data : {
accountId : self . accountId ,
deviceId : deviceId
} ,
success : _ . flow (
_ . partial ( _ . get , _ , 'data' ) ,
_ . partial ( callback , null )
) ,
error : _ . partial ( callback , true )
} ) ;
} ,
patchDevice = function patchDevice ( data , deviceId , callback ) {
self . callApi ( {
resource : 'device.patch' ,
data : {
accountId : self . accountId ,
deviceId : deviceId ,
data : data
} ,
success : _ . flow (
_ . partial ( _ . get , _ , 'data' ) ,
_ . partial ( callback , null )
) ,
error : _ . partial ( callback , true )
} ) ;
} ,
maybeUpdateMobileCallflowAssignment = function maybeUpdateMobileCallflowAssignment ( userId , userMainCallflowId , device , callback ) {
if ( device . device_type !== 'mobile' ) {
return callback ( null ) ;
}
self . updateMobileCallflowAssignment ( userId , userMainCallflowId , device , callback ) ;
} ,
updateDeviceAssignment = function updateDeviceAssignment ( userId , userMainCallflowId , device , callback ) {
var updatedDevice = {
owner_id : userId
} ;
monster . parallel ( listFnParallel , function ( err , results ) {
callbackAfterUpdate && callbackAfterUpdate ( results ) ;
} ) ;
monster . parallel ( [
_ . partial ( maybeUpdateMobileCallflowAssignment , userId , userMainCallflowId , device ) ,
_ . partial ( patchDevice , updatedDevice , device . id )
] , callback ) ;
} ;
self . usersGetMainCallflow ( userId , function ( callflow ) {
updateDevices ( callflow ) ;
} ) ;
monster . waterfall ( [
_ . partial ( getDevice , deviceId ) ,
_ . partial ( updateDeviceAssignment , userId , userMainCallflowId )
] , mainCallback ) ;
} ,
usersUpdateCallflowNumbers : function ( userId , callflowId , numbers , callback ) {