UNPKG

my-test123

Version:
349 lines 14.7 kB
import { GlobalSettings } from '../shared/globals'; import { Injectable } from '@angular/core'; import { Headers } from '@angular/http'; import { Subject } from 'rxjs/Subject'; import { Observable } from 'rxjs/Observable'; import { cloneDeep } from 'lodash'; import { Broadcaster, Logger } from 'ngx-base'; import { AuthenticationService } from 'ngx-login-client'; import { Spaces } from 'ngx-fabric8-wit'; import { Notifications } from 'ngx-base'; import { HttpService } from './http-service'; var IterationService = /** @class */ (function () { function IterationService(logger, http, auth, globalSettings, broadcaster, spaces, notifications) { var _this = this; this.logger = logger; this.http = http; this.auth = auth; this.globalSettings = globalSettings; this.broadcaster = broadcaster; this.spaces = spaces; this.notifications = notifications; this.iterations = []; this.transformedIterations = []; this.headers = new Headers({ 'Content-Type': 'application/json' }); this.dropWIObservable = new Subject(); this.createIterationObservable = new Subject(); this.spaces.current.subscribe(function (val) { return _this._currentSpace = val; }); this.selfId = this.createId(); this.logger.log('Launching IterationService instance id ' + this.selfId); } IterationService.prototype.notifyError = function (message, httpError) { this.logger.log('ERROR [IterationService] ' + message + (httpError.message ? ' ' + httpError.message : '')); /* this.notifications.message({ message: message + (httpError.message?' '+httpError.message:''), type: NotificationType.DANGER } as Notification); */ }; IterationService.prototype.createId = function () { var id = ''; var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; for (var i = 0; i < 5; i++) id += possible.charAt(Math.floor(Math.random() * possible.length)); console.log('Created new id ' + id); return id; }; /** * getIteration - We call this service method to fetch * @param iterationUrl - The url to get all the iteration * @return Promise of IterationModel[] - Array of iterations. */ IterationService.prototype.getIterations = function () { var _this = this; if (this.iterations.length > 0) { return Observable.of(this.iterations); } else { // get the current iteration url from the space service return this.spaces.current.take(1).switchMap(function (space) { var iterationsUrl = space.relationships.iterations.links.related; return _this.http .get(iterationsUrl) .map(function (response) { if (/^[5, 4][0-9]/.test(response.status.toString())) { throw new Error('API error occured'); } return response.json().data; }) .map(function (data) { _this.iterations = data.map(function (iteration) { var childIterations = _this.checkForChildIterations(iteration, data); if (childIterations.length > 0) { iteration.hasChildren = true; iteration.children = childIterations; } return iteration; }); return _this.iterations; }) .catch(function (error) { if (error.status === 401) { _this.notifyError('You have been logged out.', error); _this.auth.logout(); } else { console.log('Fetch iteration API returned some error - ', error.message); _this.notifyError('Fetching iterations has from server has failed.', error); } return Observable.throw(new Error(error.message)); }); }); } }; /** * getIteration - We call this service method to fetch * @param iterationUrl - The url to get all the iteration * @return Promise of IterationModel[] - Array of iterations. */ IterationService.prototype.getIterations2 = function (iterationUrl) { var _this = this; return this.http .get(iterationUrl) .map(function (response) { if (/^[5, 4][0-9]/.test(response.status.toString())) { throw new Error('API error occured'); } return response.json().data; }) .map(function (data) { _this.iterations = data.map(function (iteration) { var childIterations = _this.checkForChildIterations(iteration, data); if (childIterations.length > 0) { iteration.hasChildren = true; iteration.children = childIterations; } return iteration; }); return _this.iterations; }) .catch(function (error) { if (error.status === 401) { _this.notifyError('You have been logged out.', error); _this.auth.logout(); } else { console.log('Fetch iteration API returned some error - ', error.message); _this.notifyError('Fetching iterations has from server has failed.', error); } return Observable.throw(new Error(error.message)); }); }; /** * Create new iteration * @param iterationUrl - POST url * @param iteration - data to create a new iteration * @return new item */ IterationService.prototype.createIteration = function (iteration, parentIteration) { var _this = this; console.log('Create on iteration service.'); var iterationsUrl; delete iteration.id; if (parentIteration) { iterationsUrl = parentIteration.links.self; } else { iterationsUrl = this._currentSpace.relationships.iterations.links.related; } if (this._currentSpace) { iteration.relationships['space'] = { data: { id: this._currentSpace.id, type: 'spaces' }, links: { self: this._currentSpace.links.self } }; return this.http .post(iterationsUrl, { data: iteration }) .map(function (response) { if (/^[5, 4][0-9]/.test(response.status.toString())) { throw new Error('API error occured'); } return response.json().data; }) .map(function (newData) { // Add the newly added iteration on the top of the list _this.iterations.splice(0, 0, newData); return newData; }) .catch(function (error) { if (error.status === 401) { _this.notifyError('You have been logged out.', error); _this.auth.logout(); } else { console.log('Post iteration API returned some error - ', error.message); _this.notifyError('Creating iteration on server has failed.', error); return Observable.throw(new Error(error.message)); } }); } else { return Observable.throw(new Error('error')); } }; /** * Update an existing iteration * @param iteration - Updated iteration * @return updated iteration's reference from the list */ IterationService.prototype.updateIteration = function (iteration) { var _this = this; console.log('Update on iteration service.'); return this.http .patch(iteration.links.self, { data: iteration }) .map(function (response) { if (/^[5, 4][0-9]/.test(response.status.toString())) { throw new Error('API error occured'); } return response.json().data; }) .map(function (updatedData) { // Update existing iteration data var index = _this.iterations.findIndex(function (item) { return item.id === updatedData.id; }); if (index > -1) { //set hasChildren and children information var childIterations = _this.checkForChildIterations(updatedData, _this.iterations); if (childIterations.length > 0) { updatedData.hasChildren = true; updatedData.children = childIterations; } _this.iterations[index] = cloneDeep(updatedData); return _this.iterations[index]; } else { _this.iterations.splice(0, 0, updatedData); return _this.iterations[0]; } }) .catch(function (error) { if (error.status === 401) { _this.notifyError('You have been logged out.', error); _this.auth.logout(); } else { console.log('Patch iteration API returned some error - ', error.message); _this.notifyError('Updating iteration on server has failed.', error); return Observable.throw(new Error(error.message)); } }); }; IterationService.prototype.isRootIteration = function (parentPath) { return parentPath === '/'; }; IterationService.prototype.getRootIteration = function () { var _this = this; return this.getIterations().first() .map(function (resultIterations) { for (var i = 0; i < resultIterations.length; i++) { if (_this.isRootIteration(resultIterations[i].attributes.parent_path)) return resultIterations[i]; } }) .catch(function (err) { return Observable.throw(new Error(err.message)); }); }; IterationService.prototype.getIteration = function (iteration) { var _this = this; if (Object.keys(iteration).length) { var iterationLink = iteration.data.links.self; return this.http.get(iterationLink) .map(function (iterationresp) { return iterationresp.json().data; }) .catch(function (error) { _this.notifyError('Error getting iteration data.', error); return Observable.throw(new Error(error.message)); }); } else { return Observable.of(undefined); } }; IterationService.prototype.getIterationById = function (iterationId) { return this.getIterations().first() .map(function (resultIterations) { for (var i = 0; i < resultIterations.length; i++) { if (resultIterations[i].id === iterationId) { return resultIterations[i]; } } }) .catch(function (err) { return Observable.throw(new Error(err.message)); }); }; IterationService.prototype.getWorkItemCountInIteration = function (iteration) { return this.getIteration({ data: iteration }).first().map(function (resultIteration) { return resultIteration.relationships.workitems.meta.total; }); }; IterationService.prototype.emitDropWI = function (workItem, err) { if (err === void 0) { err = false; } this.dropWIObservable.next({ workItem: workItem, error: err }); }; IterationService.prototype.checkForChildIterations = function (parent, iterations) { var children = iterations.filter(function (i) { //check only for direct parent var path_arr = i.attributes.parent_path.split('/'); var id = path_arr[path_arr.length - 1]; return (id === parent.id); }); return children; }; IterationService.prototype.checkForChildIterations2 = function (parent, iterations) { var children = iterations.filter(function (i) { //check only for direct parent var path_arr = i.parentPath.split('/'); var id = path_arr[path_arr.length - 1]; return (id === parent.id); }); return children; }; IterationService.prototype.getTopLevelIterations = function (iterations) { var topLevelIterations = iterations.filter(function (iteration) { return ((iteration.attributes.parent_path.split('/')).length - 1) === 1; }); return topLevelIterations; }; IterationService.prototype.getTopLevelIterations2 = function (iterations) { var topLevelIterations = iterations.filter(function (iteration) { return ((iteration.parentPath.split('/')).length - 1) === 1; }); return topLevelIterations; }; IterationService.prototype.isTopLevelIteration = function (iteration) { return ((iteration.attributes.parent_path.split('/')).length - 1) === 1; }; IterationService.prototype.getDirectParent = function (iteration, iterations) { var path_arr = iteration.attributes.parent_path.split('/'); var id = path_arr[path_arr.length - 1]; return iterations.find(function (i) { return i.id === id; }); }; IterationService.prototype.emitCreateIteration = function (iteration) { this.createIterationObservable.next(iteration); }; IterationService.prototype.resetIterations = function () { //Hack on destroy empty iterations array this.iterations = []; }; IterationService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ IterationService.ctorParameters = function () { return [ { type: Logger, }, { type: HttpService, }, { type: AuthenticationService, }, { type: GlobalSettings, }, { type: Broadcaster, }, { type: Spaces, }, { type: Notifications, }, ]; }; return IterationService; }()); export { IterationService }; //# sourceMappingURL=iteration.service.js.map