Pārlūkot izejas kodu

Fixed /orgchart state to show user's orgchart when no personId is provided.

Joseph White 8 gadi atpakaļ
vecāks
revīzija
29e2a77318

+ 8 - 0
src/main/angular/src/models/orgchart-data.model.ts

@@ -0,0 +1,8 @@
+import Person from './person.model';
+export default class OrgChartData {
+
+    constructor(public manager: Person,
+                public children: Person[],
+                public self: Person) {}
+
+}

+ 25 - 22
src/main/angular/src/peoplesearch/orgchart-search.component.ts

@@ -2,6 +2,7 @@ import { Component } from '../component';
 import { IPromise, IQService, IScope } from 'angular';
 import { IPeopleService } from '../services/people.service';
 import Person from '../models/person.model';
+import OrgChartData from '../models/orgchart-data.model';
 
 @Component({
     stylesheetUrl: require('peoplesearch/orgchart-search.component.scss'),
@@ -22,28 +23,17 @@ export default class OrgChartSearchComponent {
 
     $onInit(): void {
         // Retrieve data from the server
-        this.fetchData();
-    }
-
-    autoCompleteSearch(query: string): IPromise<Person[]> {
-        return this.peopleService.autoComplete(query);
-    }
-
-    onAutoCompleteItemSelected(person: Person): void {
-        this.$state.go('orgchart', { personId: person.userKey });
-    }
-
-    private fetchData(): void {
-        var personId: string = this.$stateParams['personId'];
         var self = this;
-
-        // Fetch data
-        if (personId) {
-            this.$q.all({
-                directReports: this.peopleService.getDirectReports(personId),
-                managementChain: this.peopleService.getManagementChain(personId),
-                person: this.peopleService.getPerson(personId)
-            })
+        var personId: string = this.$stateParams['personId'];
+        this.fetchOrgChartData(personId).then((orgChartData: OrgChartData) => {
+            // Override personId in case it was undefined
+            personId = orgChartData.self.userKey;
+            this.$q
+                .all({
+                    directReports: this.peopleService.getDirectReports(personId),
+                    managementChain: this.peopleService.getManagementChain(personId),
+                    person: this.peopleService.getPerson(personId)
+                })
                 .then((data) => {
                     this.$scope.$evalAsync(() => {
                         self.directReports = data['directReports'];
@@ -54,6 +44,19 @@ export default class OrgChartSearchComponent {
                 .catch((result) => {
                     console.log(result);
                 });
-        }
+        });
+    }
+
+    autoCompleteSearch(query: string): IPromise<Person[]> {
+        return this.peopleService.autoComplete(query);
+    }
+
+    onAutoCompleteItemSelected(person: Person): void {
+        this.$state.go('orgchart', { personId: person.userKey });
+    }
+
+    private fetchOrgChartData(personId): IPromise<OrgChartData> {
+        return this.peopleService.getOrgChartData(personId);
+
     }
 }

+ 18 - 2
src/main/angular/src/services/people.service.dev.ts

@@ -1,17 +1,17 @@
 import { IPromise, IQService, ITimeoutService } from 'angular';
 import Person from '../models/person.model';
 import { IPeopleService } from './people.service';
+import OrgChartData from '../models/orgchart-data.model';
 
 var peopleData = require('./people.data');
 
 export default class PeopleService implements IPeopleService {
     private people: Person[];
-
     static $inject = ['$q'];
+
     constructor(private $q: IQService) {
         this.people = peopleData.map((person) => new Person(person));
     }
-
     autoComplete(query: string): IPromise<Person[]> {
         return this.search(query)
             .then((people: Person[]) => {
@@ -49,6 +49,22 @@ export default class PeopleService implements IPeopleService {
         return this.$q.reject(`Person with id: "${id}" not found.`);
     }
 
+    getOrgChartData(personId: string): angular.IPromise<OrgChartData> {
+        if (!personId) {
+            personId = '9';
+        }
+
+        var self = this.findPerson(personId);
+        var manager = self.detail['manager'];
+        var children = this.people.filter((person: Person) => {
+            return self.detail['manager']['typeMetaData'].userKey == personId;
+        });
+
+        var orgChartData = new OrgChartData(manager, children, self);
+
+        return this.$q.resolve(orgChartData);
+    }
+
     getNumberOfDirectReports(personId: string): IPromise<number> {
         return this.getDirectReports(personId)
             .then((directReports: Person[]) => {

+ 27 - 16
src/main/angular/src/services/people.service.ts

@@ -1,6 +1,7 @@
 import { IHttpService, IPromise, IQService } from 'angular';
 import Person from '../models/person.model';
 import PwmService from './pwm.service';
+import OrgChartData from '../models/orgchart-data.model';
 
 export interface IPeopleService {
     autoComplete(query: string): IPromise<Person[]>;
@@ -8,6 +9,7 @@ export interface IPeopleService {
     getDirectReports(personId: string): IPromise<Person[]>;
     getNumberOfDirectReports(personId: string): IPromise<number>;
     getManagementChain(personId: string): IPromise<Person[]>;
+    getOrgChartData(personId: string): IPromise<OrgChartData>;
     getPerson(id: string): IPromise<Person>;
     isOrgChartEnabled(id: string): IPromise<boolean>;
     search(query: string): IPromise<Person[]>;
@@ -43,12 +45,10 @@ export default class PeopleService extends PwmService implements IPeopleService
     }
 
     getDirectReports(id: string): IPromise<Person[]> {
-        return this.$http.post(this.getServerUrl('orgChartData'), {
-            userKey: id
-        }).then((response) => {
+        return this.getOrgChartData(id).then((orgChartData: OrgChartData) => {
             let people: Person[] = [];
 
-            for (let directReport of response.data['data']['children']) {
+            for (let directReport of orgChartData.children) {
                 let person: Person = new Person(directReport);
                 people.push(person);
             }
@@ -58,10 +58,8 @@ export default class PeopleService extends PwmService implements IPeopleService
     }
 
     getNumberOfDirectReports(personId: string): IPromise<number> {
-        return this.$http.post(this.getServerUrl('orgChartData'), {
-            userKey: personId
-        }).then((response) => {
-            return this.$q.resolve(response.data['data']['children'].length);
+        return this.getOrgChartData(personId).then((orgChartData: OrgChartData) => {
+            return this.$q.resolve(orgChartData.children.length);
         });
     }
 
@@ -71,21 +69,34 @@ export default class PeopleService extends PwmService implements IPeopleService
     }
 
     private getManagerRecursive(id: string, people: Person[]): IPromise<Person[]> {
-        return this.$http.post(this.getServerUrl('orgChartData'), {
-            userKey: id
-        }).then((response) => {
-            let responseData = response.data['data'];
-            if ('parent' in responseData) {
-                let manager: Person = responseData['parent'];
-                people.push(manager);
+        return this.getOrgChartData(id).then((orgChartData: OrgChartData) => {
+            if (orgChartData.manager) {
+                people.push(orgChartData.manager);
 
-                return this.getManagerRecursive(manager.userKey, people);
+                return this.getManagerRecursive(orgChartData.manager.userKey, people);
             }
 
             return this.$q.resolve(people);
         });
     }
 
+    getOrgChartData(personId: string): angular.IPromise<OrgChartData> {
+        return this.$http
+            .post(this.getServerUrl('orgChartData'), { userKey: personId })
+            .then((response) => {
+                let responseData = response.data['data'];
+
+                var manager: Person;
+                if ('parent' in responseData) { manager = new Person(responseData['parent']); }
+                var children = responseData['children'].map((child: any) => new Person(child));
+                var self = new Person(responseData['self']);
+
+                var orgChartData = new OrgChartData(manager, children, self);
+
+                return this.$q.resolve(orgChartData);
+            });
+    }
+
     getPerson(id: string): IPromise<Person> {
         return this.$http.post(this.getServerUrl('detail'), {
             userKey: id