diff --git a/app/coffee/app.coffee b/app/coffee/app.coffee index 63ea2edf..96965a00 100644 --- a/app/coffee/app.coffee +++ b/app/coffee/app.coffee @@ -356,6 +356,7 @@ modules = [ "taigaBase", "taigaCommon", "taigaResources", + "taigaResources2", "taigaAuth", "taigaEvents", diff --git a/app/coffee/modules/resources.coffee b/app/coffee/modules/resources.coffee index 8d2b5e0f..4631a432 100644 --- a/app/coffee/modules/resources.coffee +++ b/app/coffee/modules/resources.coffee @@ -146,8 +146,9 @@ urls = { # locales "locales": "/locales" - # user + # users "contacts": "/users/%s/contacts" + "stats": "/users/%s/stats" } # Initialize api urls service diff --git a/app/modules/profile/profile-contacts/profile-contacts.controller.coffee b/app/modules/profile/profile-contacts/profile-contacts.controller.coffee index e2d0e463..d14882b5 100644 --- a/app/modules/profile/profile-contacts/profile-contacts.controller.coffee +++ b/app/modules/profile/profile-contacts/profile-contacts.controller.coffee @@ -1,15 +1,15 @@ class ProfileContactsController @.$inject = [ - "tgUserService", + "tgResources", "$tgAuth" ] - constructor: (@userService, @auth) -> + constructor: (@rs, @auth) -> loadContacts: () -> userId = @auth.getUser().id - @userService.getUserContacts(userId) + @rs.users.getContacts(userId) .then (contacts) => @.contacts = contacts diff --git a/app/modules/profile/profile-contacts/profile-contacts.controller.spec.coffee b/app/modules/profile/profile-contacts/profile-contacts.controller.spec.coffee index 4bfd467d..44e0ea33 100644 --- a/app/modules/profile/profile-contacts/profile-contacts.controller.spec.coffee +++ b/app/modules/profile/profile-contacts/profile-contacts.controller.spec.coffee @@ -5,12 +5,14 @@ describe "ProfileContacts", -> $rootScope = null mocks = {} - _mockUserService = () -> - mocks.userService = { - getUserContacts: sinon.stub() + _mockResources = () -> + mocks.resources = { + users: { + getContacts: sinon.stub() + } } - provide.value "tgUserService", mocks.userService + provide.value "tgResources", mocks.resources _mockAuthService = () -> stub = sinon.stub() @@ -24,7 +26,7 @@ describe "ProfileContacts", -> _mocks = () -> module ($provide) -> provide = $provide - _mockUserService() + _mockResources() _mockAuthService() return null @@ -48,7 +50,7 @@ describe "ProfileContacts", -> {id: 3} ] - mocks.userService.getUserContacts = (userId) -> + mocks.resources.users.getContacts = (userId) -> expect(userId).to.be.equal(userId) return $q (resolve, reject) -> diff --git a/app/modules/profile/profile-projects/profile-projects.controller.coffee b/app/modules/profile/profile-projects/profile-projects.controller.coffee index f18a32af..6b47fc54 100644 --- a/app/modules/profile/profile-projects/profile-projects.controller.coffee +++ b/app/modules/profile/profile-projects/profile-projects.controller.coffee @@ -15,6 +15,5 @@ class ProfileProjectsController .then (projects) => @.projects = projects - angular.module("taigaProfile") .controller("ProfileProjects", ProfileProjectsController) diff --git a/app/modules/profile/profile-timeline/profile-timeline.controller.coffee b/app/modules/profile/profile-timeline/profile-timeline.controller.coffee index db97b33a..6b9854da 100644 --- a/app/modules/profile/profile-timeline/profile-timeline.controller.coffee +++ b/app/modules/profile/profile-timeline/profile-timeline.controller.coffee @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # -# File: modules/backlog/main.coffee +# File: modules/profile/profile-timeline/profile-timeline.controller.coffee ### taiga = @.taiga diff --git a/app/modules/resources/resources.coffee b/app/modules/resources/resources.coffee new file mode 100644 index 00000000..197a95c7 --- /dev/null +++ b/app/modules/resources/resources.coffee @@ -0,0 +1,22 @@ +services = [ + "tgProjectsResources" +] + +Resources = ($injector) -> + for serviceName in services + serviceFn = $injector.get(serviceName) + + service = $injector.invoke(serviceFn) + + for serviceProperty in Object.keys(service) + if @[serviceProperty] + console.warm("repeated resource " + serviceProperty) + + @[serviceProperty] = service[serviceProperty] + + return @ + + +Resources.$inject = ["$injector"] + +angular.module("taigaResources2").service("tgResources", Resources) diff --git a/app/modules/resources/resources.module.coffee b/app/modules/resources/resources.module.coffee new file mode 100644 index 00000000..b1fdef81 --- /dev/null +++ b/app/modules/resources/resources.module.coffee @@ -0,0 +1 @@ +angular.module("taigaResources2", []) diff --git a/app/modules/resources/users-resource.service.coffee b/app/modules/resources/users-resource.service.coffee new file mode 100644 index 00000000..40abd63c --- /dev/null +++ b/app/modules/resources/users-resource.service.coffee @@ -0,0 +1,46 @@ +Resource = (urlsService, http) -> + service = {} + + service.getStats = (userId) -> + url = urlsService.resolve("stats", userId) + + httpOptions = { + headers: { + "x-disable-pagination": "1" + } + } + + return http.get(url, {}, httpOptions) + .then (result) -> + return Immutable.fromJS(result.data) + + + service.getContacts = (userId) -> + url = urlsService.resolve("contacts", userId) + + httpOptions = { + headers: { + "x-disable-pagination": "1" + } + } + + return http.get(url, {}, httpOptions) + .then (result) -> + return Immutable.fromJS(result.data) + + service.getProjects = (userId) -> + url = urlsService.resolve("projects") + + params = {"member": userId, "order_by": "memberships__user_order"} + + return http.get(url, params) + .then (result) -> + return Immutable.fromJS(result.data) + + return () -> + return {"users": service} + +Resource.$inject = ["$tgUrls", "$tgHttp"] + +module = angular.module("taigaResources2") +module.factory("tgProjectsResources", Resource) diff --git a/app/modules/services/user.service.coffee b/app/modules/services/user.service.coffee index 743649e2..a9e53e0d 100644 --- a/app/modules/services/user.service.coffee +++ b/app/modules/services/user.service.coffee @@ -1,28 +1,28 @@ taiga = @.taiga class UserService extends taiga.Service - @.$inject = ["$tgResources"] + @.$inject = ["tgResources"] constructor: (@rs) -> getProjects: (userId) -> - return @rs.projects.listByMember(userId) - .then (projects) -> return Immutable.fromJS(projects) + return @rs.users.getProjects(userId) + + getContacts: (userId) -> + return @rs.users.getContacts(userId) attachUserContactsToProjects: (userId, projects) -> - return @.getUserContacts(userId) + return @.getContacts(userId) .then (contacts) -> projects = projects.map (project) -> - project.contacts = contacts.filter (contact) -> + contactsFiltered = contacts.filter (contact) -> contactId = contact.get("id") - return project.members.indexOf(contactId) != -1 + return project.get('members').indexOf(contactId) != -1 + + project = project.set("contacts", contactsFiltered) return project return projects - getUserContacts: (userId) -> - return @rs.users.contacts(userId) - .then (contacts) -> return Immutable.fromJS(contacts) - angular.module("taigaCommon").service("tgUserService", UserService) diff --git a/app/modules/services/user.service.spec.coffee b/app/modules/services/user.service.spec.coffee index ec16ce41..4c4269f3 100644 --- a/app/modules/services/user.service.spec.coffee +++ b/app/modules/services/user.service.spec.coffee @@ -1,6 +1,4 @@ -# pending new resouercees - -describe.skip "UserService", -> +describe "UserService", -> userService = null $q = null provide = null @@ -9,15 +7,12 @@ describe.skip "UserService", -> _mockResources = () -> mocks.resources = {} - mocks.resources.projects = { - listByMember: sinon.stub() - } - mocks.resources.users = { - contacts: sinon.stub() + getProjects: sinon.stub(), + getContacts: sinon.stub() } - provide.value "$tgResources", mocks.resources + provide.value "tgResources", mocks.resources _mocks = () -> module ($provide) -> @@ -37,7 +32,7 @@ describe.skip "UserService", -> _mocks() _inject() - it "get user projects", (done) -> + it "get user projects", () -> userId = 2 projects = [ @@ -46,28 +41,17 @@ describe.skip "UserService", -> {id: 3} ] - mocks.resources.projects.listByMember = (userId) -> - expect(userId).to.be.equal(userId) + mocks.resources.users.getProjects.withArgs(userId).returns(true) - return $q (resolve, reject) -> - resolve(projects) - - userService.getProjects(userId).then (_projects_) -> - expect(_projects_.toJS()).to.be.eql(projects) - done() - - $rootScope.$apply() + expect(userService.getProjects(userId)).to.be.true it "attach user contacts to projects", (done) -> userId = 2 - class Project - constructor: (@id, @members) -> - projects = Immutable.fromJS([ - new Project(1, [1, 2, 3]), - new Project(1, [2, 3]), - new Project(1, [1]) + {id: 1, members: [1, 2, 3]}, + {id: 2, members: [2, 3]}, + {id: 3, members: [1]} ]) contacts = Immutable.fromJS([ @@ -76,18 +60,16 @@ describe.skip "UserService", -> {id: 3, name: "fake3"} ]) - mocks.resources.users.contacts = (userId) -> + mocks.resources.users.getContacts = (userId) -> expect(userId).to.be.equal(userId) return $q (resolve, reject) -> resolve(contacts) userService.attachUserContactsToProjects(userId, projects).then (_projects_) -> - contacts = _projects_.get(0).contacts + contacts = _projects_.get(0).get("contacts") - console.log _projects_.get(0) - - expect(contacts[0]).to.be.equal('fake1') + expect(contacts.get(0).get("name")).to.be.equal('fake1') done() $rootScope.$apply() @@ -101,14 +83,14 @@ describe.skip "UserService", -> {id: 3} ] - mocks.resources.user.contacts = (userId) -> + mocks.resources.users.getContacts = (userId) -> expect(userId).to.be.equal(userId) return $q (resolve, reject) -> resolve(contacts) - userService.getUserContacts(userId).then (_contacts_) -> - expect(_contacts_.toJS()).to.be.eql(contacts) + userService.getContacts(userId).then (_contacts_) -> + expect(_contacts_).to.be.eql(contacts) done() $rootScope.$apply()