From 498d69032f4186ce635dbe385e320fc0ccc8aec2 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Thu, 7 May 2015 08:27:24 +0200 Subject: [PATCH] Refactoring services --- app/modules/home/duties/duty.directive.coffee | 12 ------- .../home/duties/duty.directive.spec.coffee | 2 -- app/modules/home/duties/duty.jade | 4 +-- app/modules/home/home.directive.coffee | 31 +++++++++++------ app/modules/home/home.directive.spec.coffee | 34 +++++++++++++++++-- app/modules/home/home.service.coffee | 31 +++++++++++++++-- app/modules/home/home.service.spec.coffee | 7 ++++ app/modules/projects/projects.service.coffee | 25 ++++++++------ .../projects/projects.service.spec.coffee | 16 ++++----- 9 files changed, 111 insertions(+), 51 deletions(-) diff --git a/app/modules/home/duties/duty.directive.coffee b/app/modules/home/duties/duty.directive.coffee index 539fafd4..bfda7866 100644 --- a/app/modules/home/duties/duty.directive.coffee +++ b/app/modules/home/duties/duty.directive.coffee @@ -12,18 +12,6 @@ DutyDirective = (navurls, projectsService, $translate) -> if scope.vm.duty._name == "issues" return $translate.instant("COMMON.ISSUE") - scope.vm.getUrl = () -> - if scope.vm.duty - ctx = { - project: projectsService.projectsById.get(String(scope.vm.duty.project)).slug - ref: scope.vm.duty.ref - } - return navurls.resolve("project-#{scope.vm.duty._name}-detail", ctx) - - scope.vm.getProjectName = () -> - if scope.vm.duty - return projectsService.projectsById.get(String(scope.vm.duty.project)).name - directive = { templateUrl: "home/duties/duty.html" scope: { diff --git a/app/modules/home/duties/duty.directive.spec.coffee b/app/modules/home/duties/duty.directive.spec.coffee index 88ceb7b7..c8dbb867 100644 --- a/app/modules/home/duties/duty.directive.spec.coffee +++ b/app/modules/home/duties/duty.directive.spec.coffee @@ -73,5 +73,3 @@ describe "dutyDirective", () -> elm = createDirective() scope.$apply() expect(elm.isolateScope().vm.getDutyType()).to.be.equal("User story translated") - expect(elm.isolateScope().vm.getUrl()).to.be.equal("http://jstesting.taiga.io") - expect(elm.isolateScope().vm.getProjectName()).to.be.equal("testing js project") diff --git a/app/modules/home/duties/duty.jade b/app/modules/home/duties/duty.jade index 0565a71e..b1b94e8a 100644 --- a/app/modules/home/duties/duty.jade +++ b/app/modules/home/duties/duty.jade @@ -1,4 +1,4 @@ -a(href="{{ ::vm.getUrl() }}", title="{{ ::duty.subject }}") +a(href="{{ ::vm.duty.url }}", title="{{ ::duty.subject }}") img.avatar( src="{{ ::vm.duty.assigned_to_extra_info.photo }}" title="{{ ::vm.duty.assigned_to_extra_info.full_name_display }}") @@ -10,4 +10,4 @@ a(href="{{ ::vm.getUrl() }}", title="{{ ::duty.subject }}") span.duty-title span.duty-id(tg-bo-ref="duty.ref") span.duty-name {{ ::duty.subject }} - div.duty-project {{ ::vm.getProjectName()}} + div.duty-project {{ ::vm.duty.projectName}} diff --git a/app/modules/home/home.directive.coffee b/app/modules/home/home.directive.coffee index 1b74a29d..47d8f99c 100644 --- a/app/modules/home/home.directive.coffee +++ b/app/modules/home/home.directive.coffee @@ -1,18 +1,25 @@ -HomeDirective = (homeService) -> +HomeDirective = ($q, homeService, projectsService) -> link = (scope, el, attrs, ctrl) -> scope.vm = {} - taiga.defineImmutableProperty(scope.vm, "workInProgress", () -> homeService.workInProgress) - scope.$watch "vm.workInProgress", (workInProgress) -> - if workInProgress.size > 0 - userStories = workInProgress.get("assignedTo").get("userStories") - tasks = workInProgress.get("assignedTo").get("tasks") - issues = workInProgress.get("assignedTo").get("issues") + projectsPromise = projectsService.getCurrentUserProjects() + workInProgresPromise = homeService.getWorkInProgress() + + $q.all([projectsPromise, workInProgresPromise]).then -> + homeService.attachProjectInfoToWorkInProgress(projectsService.currentUserProjectsById) + + taiga.defineImmutableProperty(scope.vm, "projects", () -> projectsService.currentUserProjects) + taiga.defineImmutableProperty(scope.vm, "workInProgress", () -> homeService.workInProgress) + + if scope.vm.workInProgress.size > 0 + userStories = scope.vm.workInProgress.get("assignedTo").get("userStories") + tasks = scope.vm.workInProgress.get("assignedTo").get("tasks") + issues = scope.vm.workInProgress.get("assignedTo").get("issues") scope.vm.assignedTo = userStories.concat(tasks).concat(issues) - userStories = workInProgress.get("watching").get("userStories") - tasks = workInProgress.get("watching").get("tasks") - issues = workInProgress.get("watching").get("issues") + userStories = scope.vm.workInProgress.get("watching").get("userStories") + tasks = scope.vm.workInProgress.get("watching").get("tasks") + issues = scope.vm.workInProgress.get("watching").get("issues") scope.vm.watching = userStories.concat(tasks).concat(issues) return { @@ -22,7 +29,9 @@ HomeDirective = (homeService) -> } HomeDirective.$inject = [ - "tgHomeService" + "$q", + "tgHomeService", + "tgProjectsService" ] angular.module("taigaHome").directive("tgHome", HomeDirective) diff --git a/app/modules/home/home.directive.spec.coffee b/app/modules/home/home.directive.spec.coffee index 9d96a1ee..e91aa9cb 100644 --- a/app/modules/home/home.directive.spec.coffee +++ b/app/modules/home/home.directive.spec.coffee @@ -1,5 +1,7 @@ describe "homeDirective", () -> - scope = compile = provide = null + scope = compile = provide = timeout = null + mockTgHomeService = mockTgProjectsService = null + thenStubGetCurrentUserProjectsById = thenStubGetWorkInProgress = null template = "
" createDirective = () -> @@ -7,7 +9,10 @@ describe "homeDirective", () -> return elm _mockTgHomeService = () -> + thenStubGetWorkInProgress = sinon.stub() + mockTgHomeService = { + getWorkInProgress: sinon.stub() workInProgress: Immutable.fromJS({ assignedTo: { userStories: [{"id": 1}] @@ -20,8 +25,12 @@ describe "homeDirective", () -> issues: [{"id": 6}] } }) + attachProjectInfoToWorkInProgress: sinon.stub() } + mockTgHomeService.getWorkInProgress.returns({ + then: thenStubGetWorkInProgress + }) provide.value "tgHomeService", mockTgHomeService _mockTranslateFilter = () -> @@ -35,6 +44,20 @@ describe "homeDirective", () -> _mockHomeProjectList = () -> provide.factory 'tgHomeProjectListDirective', () -> {} + _mockTgProjectsService = () -> + thenStubGetCurrentUserProjectsById = sinon.stub() + mockTgProjectsService = { + getCurrentUserProjects: sinon.stub() + currentUserProjectsById: { + get: sinon.stub() + } + } + + mockTgProjectsService.getCurrentUserProjects.returns({ + then: thenStubGetCurrentUserProjectsById + }) + provide.value "tgProjectsService", mockTgProjectsService + _mocks = () -> module ($provide) -> provide = $provide @@ -42,6 +65,7 @@ describe "homeDirective", () -> _mockHomeProjectList() _mockTgHomeService() _mockTranslateFilter() + _mockTgProjectsService() return null beforeEach -> @@ -50,12 +74,18 @@ describe "homeDirective", () -> _mocks() - inject ($rootScope, $compile) -> + inject ($rootScope, $compile, $timeout) -> scope = $rootScope.$new() compile = $compile + timeout = $timeout it "home directive content", () -> elm = createDirective() scope.$apply() + + thenStubGetCurrentUserProjectsById.callArg(0) + thenStubGetWorkInProgress.callArg(0) + timeout.flush() + expect(elm.isolateScope().vm.assignedTo.size).to.be.equal(3) expect(elm.isolateScope().vm.watching.size).to.be.equal(3) diff --git a/app/modules/home/home.service.coffee b/app/modules/home/home.service.coffee index c6123c4c..f0d6c5a4 100644 --- a/app/modules/home/home.service.coffee +++ b/app/modules/home/home.service.coffee @@ -1,7 +1,7 @@ class HomeService extends taiga.Service - @.$inject = ["$q", "$tgResources", "$rootScope", "$projectUrl", "$tgAuth"] + @.$inject = ["$q", "$tgNavUrls", "$tgResources", "$rootScope", "$projectUrl", "$tgAuth"] - constructor: (@q, @rs, @rootScope, @projectUrl, @auth) -> + constructor: (@q, @navurls, @rs, @rootScope, @projectUrl, @auth) -> @._workInProgress = Immutable.Map() @._projectPromise = null @._inProgress = false @@ -10,6 +10,32 @@ class HomeService extends taiga.Service @.fetchWorkInProgress() + attachProjectInfoToWorkInProgress: (projectsById) -> + _attachProjectInfoToDuty = (duty) => + project = projectsById.get(String(duty.project)) + ctx = { + project: project.slug + ref: duty.ref + } + Object.defineProperty(duty, "url", {get: () => @navurls.resolve("project-#{duty._name}-detail", ctx)}) + Object.defineProperty(duty, "projectName", {get: () => project.name}) + + @._workInProgress = Immutable.fromJS({ + assignedTo: { + userStories: _.map(_.clone(@.assignedToUserStories), _attachProjectInfoToDuty) + tasks: _.map(_.clone(@.assignedToTasks), _attachProjectInfoToDuty) + issues: _.map(_.clone(@.assignedToIssues), _attachProjectInfoToDuty) + } + watching: { + userStories: _.map(_.clone(@.watchingUserStories), _attachProjectInfoToDuty) + tasks: _.map(_.clone(@.watchingTasks), _attachProjectInfoToDuty) + issues: _.map(_.clone(@.watchingIssues), _attachProjectInfoToDuty) + } + }) + + getWorkInProgress: () -> + return @._projectPromise + fetchWorkInProgress: () -> userId = @auth.getUser().id @@ -28,7 +54,6 @@ class HomeService extends taiga.Service assignedIssuesPromise = @rs.issues.listInAllProjects(params).then (issues) => @.assignedToIssues = issues - params = { status__is_closed: false watchers: userId diff --git a/app/modules/home/home.service.spec.coffee b/app/modules/home/home.service.spec.coffee index f2cb34ce..5d7c1cc8 100644 --- a/app/modules/home/home.service.spec.coffee +++ b/app/modules/home/home.service.spec.coffee @@ -76,6 +76,12 @@ describe "tgHome", -> provide.value "$tgAuth", mocks.auth + _mockTgNavUrls = () -> + mocks.tgNavUrls = { + resolve: sinon.stub() + } + provide.value "$tgNavUrls", mocks.tgNavUrls + _inject = (callback) -> inject (_$q_, _$tgResources_, _$rootScope_, _$projectUrl_, _$timeout_, _tgHomeService_) -> timeout = _$timeout_ @@ -88,6 +94,7 @@ describe "tgHome", -> _mockResources() _mockProjectUrl() _mockAuth() + _mockTgNavUrls() return null _setup = -> diff --git a/app/modules/projects/projects.service.coffee b/app/modules/projects/projects.service.coffee index ba109f6c..e02191c0 100644 --- a/app/modules/projects/projects.service.coffee +++ b/app/modules/projects/projects.service.coffee @@ -5,22 +5,25 @@ class ProjectsService extends taiga.Service @.$inject = ["$tgResources", "$rootScope", "$projectUrl", "tgLightboxFactory"] constructor: (@rs, @rootScope, @projectUrl, @lightboxFactory) -> - @._projects = Immutable.Map() - @._projectsById = Immutable.Map() + @._currentUserProjects = Immutable.Map() + @._currentUserProjectsById = Immutable.Map() @._inProgress = false - @._projectsPromise = null + @._currentUserProjectsPromise = null - taiga.defineImmutableProperty @, "projects", () => return @._projects - taiga.defineImmutableProperty @, "projectsById", () => return @._projectsById + taiga.defineImmutableProperty @, "currentUserProjects", () => return @._currentUserProjects + taiga.defineImmutableProperty @, "currentUserProjectsById", () => return @._currentUserProjectsById @.fetchProjects() + getCurrentUserProjects: -> + return @._currentUserProjectsPromise + fetchProjects: -> if not @._inProgress @._inProgress = true - @._projectsPromise = @rs.projects.listByMember(@rootScope.user?.id) - @._projectsPromise.then (projects) => + @._currentUserProjectsPromise = @rs.projects.listByMember(@rootScope.user?.id) + @._currentUserProjectsPromise.then (projects) => _.map projects, (project) => project.url = @projectUrl.get(project) @@ -33,19 +36,19 @@ class ProjectsService extends taiga.Service color = project.tags_colors[tag] return {name: tag, color: color} - @._projects = Immutable.fromJS({ + @._currentUserProjects = Immutable.fromJS({ all: projects, recents: projects.slice(0, 10) }) - @._projectsById = Immutable.fromJS(groupBy(projects, (p) -> p.id)) + @._currentUserProjectsById = Immutable.fromJS(groupBy(projects, (p) -> p.id)) return @.projects - @._projectsPromise.finally => + @._currentUserProjectsPromise.finally => @._inProgress = false - return @._projectsPromise + return @._currentUserProjectsPromise newProject: -> @lightboxFactory.create("tg-lb-create-project", { diff --git a/app/modules/projects/projects.service.spec.coffee b/app/modules/projects/projects.service.spec.coffee index 615ac5ce..0ba28182 100644 --- a/app/modules/projects/projects.service.spec.coffee +++ b/app/modules/projects/projects.service.spec.coffee @@ -81,8 +81,8 @@ describe "tgProjects", -> it "all & recents filled", () -> mocks.thenStub.callArg(0, projects) - expect(projectsService.projects.get("all").toJS()).to.be.eql(projects) - expect(projectsService.projects.get("recents").toJS()).to.be.eql(projects.slice(0, 10)) + expect(projectsService.currentUserProjects.get("all").toJS()).to.be.eql(projects) + expect(projectsService.currentUserProjects.get("recents").toJS()).to.be.eql(projects.slice(0, 10)) it "_inProgress change to false when tgResources end", () -> expect(projectsService._inProgress).to.be.true @@ -103,15 +103,15 @@ describe "tgProjects", -> it "group projects by id", () -> mocks.thenStub.callArg(0, projects) - expect(projectsService.projectsById.size).to.be.equal(12) - expect(projectsService.projectsById.toJS()[1].id).to.be.equal(projects[0].id) + expect(projectsService.currentUserProjectsById.size).to.be.equal(12) + expect(projectsService.currentUserProjectsById.toJS()[1].id).to.be.equal(projects[0].id) it "add urls in the project object", () -> mocks.thenStub.callArg(0, projects) - expect(projectsService.projectsById.toJS()[1].url).to.be.equal("url-1") - expect(projectsService.projects.get("all").toJS()[0].url).to.be.equal("url-1") - expect(projectsService.projects.get("recents").toJS()[0].url).to.be.equal("url-1") + expect(projectsService.currentUserProjectsById.toJS()[1].url).to.be.equal("url-1") + expect(projectsService.currentUserProjects.get("all").toJS()[0].url).to.be.equal("url-1") + expect(projectsService.currentUserProjects.get("recents").toJS()[0].url).to.be.equal("url-1") it "add sorted colorized_tags project object", () -> mocks.thenStub.callArg(0, projects) @@ -123,7 +123,7 @@ describe "tgProjects", -> ]; - colorized_tags = projectsService.projects.get("all").toJS()[0].colorized_tags + colorized_tags = projectsService.currentUserProjects.get("all").toJS()[0].colorized_tags expect(colorized_tags).to.be.eql(tags)