Reformat WatchersService code

pull/3860/head
Alex Dik 9 years ago
parent c299311eee
commit ea9592eb18
  1. 110
      frontend/app/components/panels/services/watchers.service.js

@ -35,72 +35,80 @@ function WatchersService($http, $q) {
var getWatchers = function(path) {
return function() {
var watchers = $q.defer();
$http.get(path).success(function(data) {
watchers.resolve(data._embedded.elements);
}).error(function(err) {
watchers.reject(err);
});
return watchers.promise;
};
};
var watching = function(workPackage) {
var path = workPackage.links.watchers.url();
return getWatchers(path)();
},
available = function(workPackage) {
var path = workPackage.links.availableWatchers.url();
return getWatchers(path)();
},
all = function(workPackage) {
var watchers = $q.defer();
$q.all([watching(workPackage), available(workPackage)]).then(function(allWatchers) {
var watching = allWatchers[0],
available = _.filter(allWatchers[1], function(user) {
return _.findIndex(watching, function(watchingUser) {
return user.id === watchingUser.id;
}) === -1;
});
watchers.resolve({ watching: watching, available: available });
}, function(err) {
watchers.reject(err);
});
return watchers.promise;
},
add = function(workPackage, watcher) {
var added = $q.defer(),
// somehow, the path will not be correctly inferred when using url()
path = workPackage.links.addWatcher.props.href,
method = workPackage.links.addWatcher.props.method,
payload = {
user: {
href: watcher._links.self.href // watcher is not a ressource
}
};
$http[method](path, payload).then(function() {
added.resolve(watcher);
}, function(err) {
added.reject(err);
});
var path = workPackage.links.watchers.url();
return getWatchers(path)();
};
return added.promise;
},
remove = function(workPackage, watcher) {
var removed = $q.defer(),
path = workPackage.links.removeWatcher.props.href,
method = workPackage.links.removeWatcher.props.method;
var available = function(workPackage) {
var path = workPackage.links.availableWatchers.url();
return getWatchers(path)();
};
path = path.replace(/\{user\_id\}/, watcher.id);
var all = function(workPackage) {
var watchers = $q.defer();
$http[method](path).then(function() {
removed.resolve(watcher);
}, function(err) {
remove.reject(err);
$q.all([watching(workPackage), available(workPackage)]).then(function(allWatchers) {
var watching = allWatchers[0],
available = _.filter(allWatchers[1], function(user) {
return _.findIndex(watching, function(watchingUser) {
return user.id === watchingUser.id;
}) === -1;
});
return removed.promise;
};
watchers.resolve({ watching: watching, available: available });
}, function(err) {
watchers.reject(err);
});
return watchers.promise;
};
var add = function(workPackage, watcher) {
var added = $q.defer(),
path = workPackage.links.addWatcher.props.href,
method = workPackage.links.addWatcher.props.method,
payload = { user: { href: watcher._links.self.href } };
$http[method](path, payload).then(function() {
added.resolve(watcher);
}, function(err) {
added.reject(err);
});
return added.promise;
};
var remove = function(workPackage, watcher) {
var removed = $q.defer(),
path = workPackage.links.removeWatcher.props.href,
method = workPackage.links.removeWatcher.props.method;
path = path.replace(/\{user\_id\}/, watcher.id);
$http[method](path).then(function() {
removed.resolve(watcher);
}, function(err) {
remove.reject(err);
});
return removed.promise;
};
/*
* NOTE: In theory, this service is independent from WorkPackages,

Loading…
Cancel
Save