UNPKG

fabric8-planner

Version:
533 lines 21.2 kB
import { Inject, Injectable } from '@angular/core'; import { of as ObservableOf, throwError } from 'rxjs'; import { catchError, map } from 'rxjs/operators'; import { Logger } from 'ngx-base'; import { Spaces } from 'ngx-fabric8-wit'; import { WIT_API_URL } from 'ngx-fabric8-wit'; import { UserService } from 'ngx-login-client'; import { WorkItem } from '../models/work-item'; import { HttpBackendClient, HttpClientService } from './../shared/http-module/http.service'; import { AreaService } from './area.service'; var WorkItemService = /** @class */ (function () { function WorkItemService(httpClientService, httpBackendClient, logger, areaService, userService, spaces, baseApiUrl) { var _this = this; this.httpClientService = httpClientService; this.httpBackendClient = httpBackendClient; this.logger = logger; this.areaService = areaService; this.userService = userService; this.spaces = spaces; this.baseApiUrl = baseApiUrl; this.workItemUrl = null; this.renderUrl = null; this.workItemTypes = []; /** * All the soon to be deprecated method stays behind this line */ /** * @deprecated Planner :: this property will be deprecated soon */ this.userIdMap = {}; this.spaces.current.subscribe(function (val) { return _this._currentSpace = val; }); this.logger.log('Launching WorkItemService'); } WorkItemService.prototype.notifyError = function (message, httpError) { this.logger.log('ERROR [WorkItemService] ' + message + (httpError.message ? ' ' + httpError.message : '')); }; WorkItemService.prototype.handleError = function (error, msg) { this.notifyError(msg, error); return throwError(new Error(error.message)); }; /** * Takes a URL to the list of child * work items of a particular work item * and performs a get operation for all the child items * @param url url to the child work items */ WorkItemService.prototype.getChildren = function (url) { var _this = this; return this.httpClientService .get(url) .pipe(map(function (response) { return response.data; }), catchError(function (err) { return _this.handleError(err, 'Getting children failed.'); })); }; /** * Usage: Performs a get operation on work items * * @param pageSize number of work items to be fetched * @param filters filters to the work items */ WorkItemService.prototype.getWorkItems = function (pageSize, filters) { var _this = this; if (pageSize === void 0) { pageSize = 20; } var url = ''; this.workItemUrl = this.baseApiUrl + 'search'; url = this.workItemUrl + '?page[limit]=' + pageSize + '&' + Object.keys(filters).map(function (k) { return 'filter[' + k + ']=' + encodeURIComponent(JSON.stringify(filters[k])); }).join('&'); return this.httpClientService .get(url) .pipe(map(function (resp) { return { workItems: resp.data, nextLink: resp.links.next ? resp.links.next : null, totalCount: resp.meta ? resp.meta.totalCount : 0, included: resp.included ? resp.included : [], ancestorIDs: resp.meta.ancestorIDs ? resp.meta.ancestorIDs : [] }; }), catchError(function (error) { _this.notifyError('Getting work items failed.', error); return throwError(new Error(error.message)); })); }; /** * This function is called from next page onwards in the scroll * It does pretty much same as the getWorkItems function */ WorkItemService.prototype.getMoreWorkItems = function (url) { var _this = this; if (url) { return this.httpClientService .get(url) .pipe(map(function (resp) { return { workItems: resp.data, nextLink: resp.links.next ? resp.links.next : null, included: resp.included ? resp.included : [], ancestorIDs: resp.meta.ancestorIDs ? resp.meta.ancestorIDs : [] }; }), catchError(function (error) { _this.notifyError('Getting more work items failed.', error); return throwError(new Error(error.message)); })); } else { return throwError('No more item found'); } }; /** * Usage: This method gives a single work item by display number. * * @param id : string - number * @param owner : string * @param space : string */ WorkItemService.prototype.getWorkItemByNumber = function (id, owner, space) { var _this = this; if (owner === void 0) { owner = ''; } if (space === void 0) { space = ''; } if (owner && space) { return this.httpBackendClient.get(this.baseApiUrl + 'namedspaces' + '/' + owner + '/' + space + '/workitems/' + id) .pipe(map(function (item) { return item.data; }), catchError(function (error) { _this.notifyError('Getting work item data failed.', error); return throwError(new Error(error.message)); })); } else { return this.httpClientService.get(this.baseApiUrl + 'workitems/' + id) .pipe(map(function (i) { return i.data; }), catchError(function (error) { _this.notifyError('Getting work item data failed.', error); return throwError(new Error(error.message)); })); } }; /** * Usage: This method is to get the events for a work item * This method is only called when a single item is fetched for the * details page. * * @param: WorkItem - wItem */ WorkItemService.prototype.getEvents = function (url) { return this.httpClientService .get(url) .pipe(map(function (response) { return response.data; })); }; /** * Usage: This method is to resolve the comments for a work item * This method is only called when a single item is fetched for the * details page. * * @param: WorkItem - wItem */ WorkItemService.prototype.resolveComments = function (url) { return this.httpClientService .get(url) .pipe(map(function (response) { return { data: response.data, meta: response.meta, links: response.links }; })); }; /** * Usage: This method is to resolve the linked items for a work item * This method is only called when a single item is fetched for the * details page. * * @param: WorkItem - wItem */ WorkItemService.prototype.resolveLinks = function (url) { var _this = this; return this.httpClientService .get(url) .pipe(map(function (response) { return [response.data, response.included]; }), catchError(function (error) { _this.notifyError('Getting linked items data failed.', error); return throwError(new Error(error.message)); })); }; /** * Usage: fetch all the work item types * * @param workItemTypeUrl : string */ WorkItemService.prototype.getWorkItemTypes = function (workItemTypeUrl) { var _this = this; return this.httpClientService .get(workItemTypeUrl) .pipe(map(function (response) { // TODO : this line can be removed when // getWorkItemTypesById function is removed _this.workItemTypes = response.data; return response.data; }), catchError(function (error) { _this.notifyError('Getting work item type information failed.', error); return throwError(new Error(error.message)); })); }; /** * Usage: This method deletes an work item * @param workItem WorkItem */ WorkItemService.prototype.delete = function (workItem) { var _this = this; var endpoint = workItem.links.self; return this.httpClientService.delete(endpoint) .pipe(catchError(function (error) { _this.notifyError('Deleting workitem failed.', error); return throwError(error); })); }; /** * Usage: This method create a new item * * @param: url * @param: WorkItem - workItem (Item to be created) */ WorkItemService.prototype.create = function (url, workItem) { var payload = JSON.stringify({ data: workItem }); return this.httpClientService .post(url, payload) .pipe(map(function (response) { return response.data; })); }; /** * Usage: This method update an existing item * updates the item in the big list * resolve the users for the item * * @param: WorkItem - workItem (Item to be created) */ WorkItemService.prototype.update = function (workItem) { var _this = this; return this.httpClientService .patch(workItem.links.self, JSON.stringify({ data: workItem })) .pipe(map(function (response) { return response.data; }), catchError(function (error) { _this.notifyError('Updating work item failed.', error); return throwError(new Error(error.message)); })); }; /** * Usage: This method create a comment for a workItem * * @param: string - id (Work Item ID) * @param: Comment */ WorkItemService.prototype.createComment = function (url, comment) { var _this = this; return this.httpClientService .post(url, { data: comment }) .pipe(map(function (response) { return response.data; }), catchError(function (error) { _this.notifyError('Creating comment failed.', error); return throwError(new Error(error.message)); })); }; /** * Usage: to update comment pass in with the ID * * @param comment Comment type */ WorkItemService.prototype.updateComment = function (comment) { var _this = this; var endpoint = comment.links.self; return this.httpClientService .patch(endpoint, { 'data': comment }) .pipe(map(function (response) { var comment = response.data; var theUser = _this.userService.getSavedLoggedInUser(); comment.relationalData = { 'creator': theUser }; return comment; }), catchError(function (error) { _this.notifyError('Updating comment failed.', error); return throwError(new Error(error.message)); })); }; /** * Usage: To delete a comment * @param comment Comment */ WorkItemService.prototype.deleteComment = function (comment) { var _this = this; var endpoint = comment.links.self; return this.httpClientService.delete(endpoint) .pipe(catchError(function (error) { _this.notifyError('Deleting comment failed.', error); return throwError(new Error(error.message)); })); }; /** * Usage: This function fetches all the work item link types * Store it in an instance variable * * @return Promise of LinkType[] */ WorkItemService.prototype.getAllLinkTypes = function (url) { var _this = this; return this.httpClientService.get(url) .pipe(map(function (d) { return d.data; }), catchError(function (error) { _this.notifyError('Getting link meta info failed (forward).', error); return throwError(new Error(error.message)); })); }; /** * Usage: Makes an API call to create a link * Recieves the new link response * Resolves and add the new link to the workItem * * @param link: Link - The new link object for request params * @returns Promise<Link> */ WorkItemService.prototype.createLink = function (url, link) { return this.httpClientService .post(url, JSON.stringify(link)) .pipe(map(function (response) { return [response.data, response.included]; })); }; /** * Usage: Makes an API call to delete a link * Removes the new link to the workItem * * @param link: Link * @returns Promise<void> */ WorkItemService.prototype.deleteLink = function (url) { return this.httpClientService .delete(url); }; WorkItemService.prototype.searchLinkWorkItem = function (term, spaceId) { var searchUrl = this.baseApiUrl + 'search?spaceID=' + spaceId + '&q=' + term; return this.httpClientService .get(searchUrl) .pipe(map(function (response) { return response.data; })); }; /** * Usage: Make a API call to save * the order of work item. * * @param spaceLink: string * @param workItemId: string * @param prevWiId: string * @param direction: string */ WorkItemService.prototype.reOrderWorkItem = function (spaceLink, workItem, prevWiId, direction) { if (prevWiId === void 0) { prevWiId = null; } var newWItem = new WorkItem(); var arr = []; newWItem.id = workItem.id.toString(); newWItem.attributes = new Map(); newWItem.attributes['version'] = workItem.attributes['version']; newWItem.type = workItem.type; arr.push(newWItem); var url = spaceLink + "/workitems/reorder"; return this.httpClientService .patch(url, JSON.stringify({ data: arr, position: { direction: direction, id: prevWiId } })) .pipe(map(function (response) { return response.data[0]; }), catchError(function (err) { return ObservableOf(newWItem); })); }; /** * Usage: This function is used to render markdown text in html * @param markDownText string */ WorkItemService.prototype.renderMarkDown = function (markDownText) { var params = { data: { attributes: { content: markDownText, markup: 'Markdown' }, type: 'rendering' } }; // FIXME: make the URL great again (when we know the right API URL for this)! this.renderUrl = this.baseApiUrl + 'render'; return this.httpClientService .post(this.renderUrl, JSON.stringify(params)) .pipe(map(function (response) { return response.data.attributes.renderedContent; })); }; /** * @deprecated Planner :: this function will be deprecated soon * @function buildUserIdMap builds a ID-User map to dynamically access list of users * This method takes the locally saved list of users from User Service * Before coming to this method we fetch the list of users using router resolver * in detail and list component. */ WorkItemService.prototype.buildUserIdMap = function () { var _this = this; // Fetch the current updated locally saved user list var users = this.userService.getLocallySavedUsers(); // Check if the map is putdated or not and if yes then rebuild it if (Object.keys(this.userIdMap).length < users.length) { this.userIdMap = {}; users.forEach(function (user) { _this.userIdMap[user.id] = user; }); } }; /** * @deprecated Planner :: we save all the types in the store in a key - value pair. * Therefore, this function is not needed anymore. * * Usage: This method is to fetch the work item types by ID */ WorkItemService.prototype.getWorkItemTypesById = function (id) { var _this = this; if (this._currentSpace && typeof (id) !== 'undefined') { var workItemType_1 = this.workItemTypes ? this.workItemTypes.find(function (type) { return type.id === id; }) : null; if (workItemType_1) { return ObservableOf(workItemType_1); } else { var workItemTypeUrl = this._currentSpace.links.self.split('/spaces/')[0] + '/workitemtypes/' + id; return this.httpClientService.get(workItemTypeUrl) .pipe(map(function (response) { workItemType_1 = response.data; if (_this.workItemTypes) { var existingType = _this.workItemTypes.find(function (type) { return type.id === workItemType_1.id; }); if (existingType) { existingType = workItemType_1; } else { _this.workItemTypes.push(workItemType_1); } } return workItemType_1; }), catchError(function (error) { _this.notifyError('Getting work item type info failed.', error); return throwError(new Error(error.message)); })); } } else { return ObservableOf({}); } }; /** * @deprecated Planner :: this method will be deprecated soon * Resolving relations is moved to querries * * Usage: Resolve the wi type for a WorkItem */ WorkItemService.prototype.resolveType = function (workItem) { this.getWorkItemTypesById(workItem.relationships.baseType.data.id) .subscribe(function (type) { workItem.relationships.baseType.data = type; }); }; /** * @deprecated Planner :: this method will be deprecated soon * Usage: Fetch an area by it's ID from the areas list */ WorkItemService.prototype.getAreaById = function (areaId) { return this.areaService.getAreas() .pipe(map(function (areas) { return areas.find(function (item) { return item.id == areaId; }); })); }; /** * @deprecated Planner :: this method will be deprecated soon * Resolving relations is moved to querries * * Usage: To resolve the areas in eact WorkItem * For now it resolves assignne and creator */ WorkItemService.prototype.resolveAreaForWorkItem = function (workItem) { if (!workItem.hasOwnProperty('relationalData')) { workItem.relationalData = {}; } if (!workItem.relationships.hasOwnProperty('area') || !workItem.relationships.area) { workItem.relationalData.area = null; return; } if (!workItem.relationships.area.hasOwnProperty('data')) { workItem.relationalData.area = null; return; } if (!workItem.relationships.area.data) { workItem.relationalData.area = null; return; } this.getAreaById(workItem.relationships.area.data.id) .subscribe(function (area) { return workItem.relationalData.area = area; }); }; /** * @deprecated Planner :: * Usage: Fetch an use by it's ID from the User-ID map */ WorkItemService.prototype.getUserById = function (userId) { if (userId in this.userIdMap) { return this.userIdMap[userId]; } else { return null; } }; /** * @deprecated Planner :: this method will be deprecated soon * Resolving relations is moved to querries * Usage: Resolve the creator for a WorkItem */ WorkItemService.prototype.resolveCreator = function (workItem) { if (!workItem.relationships.hasOwnProperty('creator') || !workItem.relationships.creator) { workItem.relationalData.creator = null; return; } if (!workItem.relationships.creator.hasOwnProperty('data')) { workItem.relationalData.creator = null; return; } if (!workItem.relationships.creator.data) { workItem.relationalData.creator = null; return; } // To handle some old items with no creator if (workItem.relationships.creator.data.id === 'me') { workItem.relationalData.creator = null; return; } workItem.relationalData.creator = this.getUserById(workItem.relationships.creator.data.id); }; WorkItemService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ WorkItemService.ctorParameters = function () { return [ { type: HttpClientService, }, { type: HttpBackendClient, }, { type: Logger, }, { type: AreaService, }, { type: UserService, }, { type: Spaces, }, { type: undefined, decorators: [{ type: Inject, args: [WIT_API_URL,] },] }, ]; }; return WorkItemService; }()); export { WorkItemService }; //# sourceMappingURL=work-item.service.js.map