vicinityManager/server/services/contracts/contracts.js
/**
Modify contracts related with removed device
* @return {Callback}
*/
Modify contracts related with removed device
@return {Callback}
Modify contracts related with removed device @return { Callback }
function removeDevice(item, otherParams, callback){
  var ctids = [];
  var mycid = item.cid.id._id;
  var friends = [];
  var notifs = [];
  if(item.accessLevel === 0){
    getOnlyId(ctids, item.hasContracts);
  } else {
    getOnlyId(friends, item.cid.id.knows);
    for(var i = 0; i < item.hasContracts.length; i++){
      if(friends.indexOf(item.hasContracts[i].contractingParty) === -1){
        ctids.push(item.hasContracts[i].id.toString());
      }
    }
  }
  itemOp.update({_id: item._id}, {$pull: {hasContracts: {id: {$in: ctids}}}}, { multi: true })
  .then(function(){ return contractOp.update({_id: {$in: ctids}}, {$pull: {'iotOwner.items' : {id: item._id}}}, { multi: true }); })
  .then(function(){ return contractOp.update({_id: {$in: ctids}}, {$pull: {'serviceProvider.items' : {id: item._id}}}, { multi: true }); })
  .then(function(){
    for(var i = 0; i < item.hasContracts.length; i++){
      if(ctids.indexOf(item.hasContracts[i].id.toString()) !== -1){
        commServer.callCommServer({}, 'users/' + item.oid + '/groups/' + item.hasContracts[i].extid, 'DELETE');
      }
    }
    return true;
  })
  )
  .then(function(){
    contractValidity(ctids);
  })
  .then(function(){
    callback(item.oid, 'success');
  })
  .catch(function(err){
    callback(item.oid, err);
  });
}
function removeDevice ( item , otherParams , callback ) { var ctids = [ ] ; var mycid = item . cid . id . _id ; var friends = [ ] ; var notifs = [ ] ; if ( item . accessLevel === 0 ) { getOnlyId ( ctids , item . hasContracts ) ; } else { getOnlyId ( friends , item . cid . id . knows ) ; for ( var i = 0 ; i < item . hasContracts . length ; i ++ ) { if ( friends . indexOf ( item . hasContracts [ i ] . contractingParty ) === - 1 ) { ctids . push ( item . hasContracts [ i ] . id . toString ( ) ) ; } } } itemOp . update ( { _id : item . _id } , { $pull : { hasContracts : { id : { $in : ctids } } } } , { multi : true } ) . then ( function ( ) { return contractOp . update ( { _id : { $in : ctids } } , { $pull : { 'iotOwner.items' : { id : item . _id } } } , { multi : true } ) ; } ) . then ( function ( ) { return contractOp . update ( { _id : { $in : ctids } } , { $pull : { 'serviceProvider.items' : { id : item . _id } } } , { multi : true } ) ; } ) . then ( function ( ) { for ( var i = 0 ; i < item . hasContracts . length ; i ++ ) { if ( ctids . indexOf ( item . hasContracts [ i ] . id . toString ( ) ) !== - 1 ) { commServer . callCommServer ( { } , 'users/' + item . oid + '/groups/' + item . hasContracts [ i ] . extid , 'DELETE' ) ; } } return true ; } ) . then ( function ( ) { contractValidity ( ctids ) ; } ) . then ( function ( ) { callback ( item . oid , 'success' ) ; } ) . catch ( function ( err ) { callback ( item . oid , err ) ; } ) ; }
Modify contracts related with removed device
@return {Callback}
Modify contracts related with removed device @return { Callback }
// for(var j = 0; j < item.hasContracts.length; j++){ //   notifs.push({mycid: mycid, othercid: item.hasContracts[j].contractingParty, thing: item.hasContracts[j].id, type: 22}); // } // notifs.push({mycid: mycid, othercid: item.hasContracts[i].contractingParty, thing: item.hasContracts[i].id, type: 22}); // .then(function(){ //   for(item in notifs){ //     createNotif(notifs[item].mycid, notifs[item].othercid, notifs[item].thing, notifs[item].type); //   } //   return true; // })