UNPKG

my-test123

Version:
603 lines (601 loc) 28.8 kB
import { Injectable } from '@angular/core'; import { cloneDeep } from 'lodash'; // mock data generators import { SchemaMockGenerator } from './mock-data/schema-mock-generator'; import { WorkItemMockGenerator } from './mock-data/work-item-mock-generator'; import { UserMockGenerator } from './mock-data/user-mock-generator'; import { FilterMockGenerator } from './mock-data/filter-mock-generator'; import { SpaceMockGenerator } from './mock-data/space-mock-generator'; import { AreaMockGenerator } from './mock-data/area-mock-generator'; import { IterationMockGenerator } from './mock-data/iteration-mock-generator'; import { LabelMockGenerator } from './mock-data/label-mock-generator'; import { GroupTypesMockGenerator } from './mock-data/group-types-mock-generator'; /* This class provides a mock database store for entities. It provides CRUD operations on entities as well as some auxiliary methods, like users and identities. NOTE: IMPORTANT: if returning data to the http service, ALWAYS return vanity copies of objects, NOT THE LIVING REFERENCES TO STRUCTURES STORED HERE. As the 'real' networked service always returns detached object copies, this resembles the original behaviour. Also, the returnes references ARE RE-USED, so data could change without this class noticing it! THIS HAPPENS. IT HAPPENED. IT SUCKS! ANOTHER NOTE, ALSO IMPORTANT: This is some sort of inmemory database. The whole thing relies on being a singleton for the whole application. If you use this class, make sure there is only one instance in existence! If you have more than one instance of this, you will get weird errors, data values jumping around and you will have a fun time finding that problem. I did have a fun time finding that issue. */ var MockDataService = /** @class */ (function () { function MockDataService() { // mock data generators this.schemaMockGenerator = new SchemaMockGenerator(); this.workItemMockGenerator = new WorkItemMockGenerator(); this.userMockGenerator = new UserMockGenerator(); this.filterMockGenerator = new FilterMockGenerator(); this.spaceMockGenerator = new SpaceMockGenerator(); this.iterationMockGenerator = new IterationMockGenerator(); this.areaMockGenerator = new AreaMockGenerator(); this.labelMockGenerator = new LabelMockGenerator(); this.groupTypesMockGenerator = new GroupTypesMockGenerator(); this.selfId = this.createId(); // create initial data store this.workItems = this.workItemMockGenerator.createWorkItems(); this.workItemLinks = this.workItemMockGenerator.createWorkItemLinks(); this.workItemComments = this.workItemMockGenerator.createWorkItemComments(); this.workItemChilds = this.workItemMockGenerator.createWorkItemChilds(); this.spaces = this.spaceMockGenerator.createSpaces(); this.iterations = this.iterationMockGenerator.createIterations(); this.areas = this.areaMockGenerator.createAreas(); this.labels = this.labelMockGenerator.getAllLabels(); this.groupTypes = this.groupTypesMockGenerator.createGroupTypes(); this.selfId = this.createId(); console.log('Started MockDataService service instance ' + this.selfId); } // utility methods MockDataService.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; }; MockDataService.prototype.makeCopy = function (input) { return JSON.parse(JSON.stringify(input)); }; // data accessors // work items and dependend entities (comments, ..) /* Filter the workitems */ MockDataService.prototype.getWorkItemsFiltered = function (pathParams) { var assigneeFilter = pathParams['filter[assignee]']; var creatorFilter = pathParams['filter[creator]']; var areaFilter = pathParams['filter[area]']; var workItemTypeFilter = pathParams['filter[workitemtype]']; var workItemStateFilter = pathParams['filter[state]']; var iterationFilter = pathParams['filter[iteration]']; var hasParentFilter = pathParams['filter[parentexists]']; console.log('Filtering on: ' + (assigneeFilter ? 'assignee==' + assigneeFilter + ' ' : '') + (creatorFilter ? 'creator==' + creatorFilter + ' ' : '') + (areaFilter ? 'area==' + areaFilter + '' : '') + (workItemTypeFilter ? 'workitemtype==' + workItemTypeFilter + ' ' : '') + (workItemStateFilter ? 'state==' + workItemStateFilter + ' ' : '') + (iterationFilter ? 'iteration==' + iterationFilter + ' ' : '') + (hasParentFilter ? 'parentexists==' + hasParentFilter + ' ' : '')); var filteredWorkitems = new Array(); var counter = 0; for (var i = 0; i < this.workItems.length; i++) { var filterMatches = true; if (assigneeFilter) filterMatches = filterMatches && (this.workItems[i].relationships.assignees.data && this.workItems[i].relationships.assignees.data[0].id === assigneeFilter); if (creatorFilter) filterMatches = filterMatches && (this.workItems[i].relationships.creator.data && this.workItems[i].relationships.creator.data.id === creatorFilter); if (areaFilter) filterMatches = filterMatches && (this.workItems[i].relationships.area.data && this.workItems[i].relationships.area.data.id === areaFilter); if (workItemTypeFilter) filterMatches = filterMatches && (this.workItems[i].relationships.baseType.data.id === workItemTypeFilter); if (workItemStateFilter) filterMatches = filterMatches && (this.workItems[i].attributes['system.state'] === workItemStateFilter); if (iterationFilter) filterMatches = filterMatches && this.workItems[i].relationships.iteration.data && (this.workItems[i].relationships.iteration.data.id === iterationFilter); if (hasParentFilter && hasParentFilter == 'false') { // this is very dirty and relies on the mock data being of a certain form. // this should be replaced by a proper link behaviour of parent-childs (see links in this class). var hasParent = false; if (['id5', 'id6', 'id7', 'id8', 'id9', 'id10'].indexOf(this.workItems[i].id) != -1) { console.log('WorkItem ' + this.workItems[i].id + ' has a parent.'); hasParent = true; } else { console.log('WorkItem ' + this.workItems[i].id + ' has no parent.'); } filterMatches = filterMatches && !hasParent; } if (filterMatches) filteredWorkitems.push(this.makeCopy(this.workItems[i])); } return filteredWorkitems; }; MockDataService.prototype.getWorkItems = function () { return this.makeCopy(this.workItems); }; MockDataService.prototype.createWorkItemOrEntity = function (extraPath, entity) { if (extraPath && extraPath.indexOf('/') != -1) { // request for subentities on a wi var parts = extraPath.split('/'); var wiId = parts[0]; var subselect = parts[1]; if (subselect === 'comments') { console.log('Request create new comment for workitem ' + wiId); var newId = this.createId(); var newComment = { 'attributes': { 'body': entity.data.attributes.body, 'body.rendered': entity.data.attributes.body, 'markup': 'Markdown', 'created-at': '2000-01-01T09:00:00.000000Z' }, 'id': newId, 'links': { 'self': 'http://demo.api.almighty.io/api/comments/' + newId }, 'relationships': { 'created-by': { 'data': { 'id': 'user0', 'type': 'identities' } } }, 'type': 'comments' }; this.workItemComments[wiId].data.push(newComment); this.workItemComments[wiId].meta.totalCount += 1; return { data: newComment }; } return {}; } var localWorkItem = this.makeCopy(entity.data); localWorkItem.id = this.createId(); Object.assign(localWorkItem.attributes, { 'system.number': localWorkItem.id, 'system.description': localWorkItem.attributes['system.description'] ? localWorkItem.attributes['system.description'] : '', 'system.description.rendered': localWorkItem.attributes['system.description'] ? localWorkItem.attributes['system.description'] : '', }); localWorkItem.links = { 'self': 'http://mock.service/api/workitems/id' + localWorkItem.id, }; this.workItemComments['id' + localWorkItem.id] = { 'data': [], 'meta': { 'totalCount': 0 } }; localWorkItem.relationships = { 'assignees': localWorkItem.relationships.assignees ? { 'data': [{ 'id': localWorkItem.relationships.assignees.data[0].id, 'links': { 'self': 'http://mock.service/api/user/' + localWorkItem.relationships.assignees.data[0].id }, 'type': 'identities' }] } : {}, 'iteration': localWorkItem.relationships.iteration ? { 'data': { 'id': localWorkItem.relationships.iteration.data.id, 'links': { 'self': 'http://mock.service/api/iterations/' + localWorkItem.relationships.iteration.data.id }, 'type': 'iterations' } } : { 'data': { 'id': 'iteration-id1', 'links': { 'self': 'http://mock.service/api/iterations/root-iteration-id' }, 'type': 'iterations' } }, 'area': localWorkItem.relationships.area ? { 'data': { 'id': localWorkItem.relationships.area.data.id, 'links': { 'self': 'http://mock.service/api/areas/' + localWorkItem.relationships.area.data.id }, 'type': 'iterations' } } : { 'data': { 'id': 'rootarea', 'links': { 'self': 'http://mock.service/api/areas/rootarea' }, 'type': 'areas' } }, 'labels': localWorkItem.relationships.labels ? { 'data': localWorkItem.relationships.labels.data } : {}, 'baseType': { 'data': { 'id': '86af5178-9b41-469b-9096-57e5155c3f31', 'type': 'workitemtypes' } }, 'comments': { 'links': { 'related': 'http://mock.service/api/workitems/id' + localWorkItem.id + '/comments', 'self': 'http://mock.service/api/workitems/id' + localWorkItem.id + '/relationships/comments' } }, 'creator': { 'data': { 'id': 'user0', 'imageURL': 'https://avatars.githubusercontent.com/u/2410471?v=3', 'links': { 'self': 'http://mock.service/api/user' }, 'type': 'identities' } }, 'children': { 'links': { 'related': 'http://mock.service/api/workitems/id' + localWorkItem.id + '/childs' }, 'meta': { 'hasChildren': false, } } }; if (entity.data.relationships.baseType.data) { // existing type on entity localWorkItem.relationships.baseType = this.makeCopy(entity.data.relationships.baseType); } this.workItems.push(localWorkItem); return { data: this.makeCopy(localWorkItem) }; }; MockDataService.prototype.getWorkItemOrEntity = function (extraPath) { var _this = this; if (extraPath && extraPath.indexOf('/') != -1) { // request for subentities on a wi var parts = extraPath.split('/'); var wiId = parts[0]; var subselect = parts[1]; if (subselect === 'comments') { console.log('Requested comments for workitem ' + wiId); return this.makeCopy(this.workItemComments[wiId]); } else if (subselect === 'labels') { // bad me karenge } else if (subselect === 'relationships') { console.log('Request for relationships for workitem ' + wiId); if (parts[2] === 'links') { var links = this.getWorkItemLinksForWorkItem(wiId); var linkIncludes_1 = []; links.forEach(function (link) { return linkIncludes_1 = linkIncludes_1.concat(_this.createWorkItemLinkIncludes(link)); }); return { data: links, meta: { totalCount: links.length }, included: linkIncludes_1 }; } } else if (subselect === 'childs') { console.log('Request for childs for workitem ' + wiId); return { data: this.makeCopy(this.workItemChilds[wiId]) }; } // should never happen return {}; } console.log('Requested workitem ' + extraPath); for (var i = 0; i < this.workItems.length; i++) if (this.workItems[i].id === extraPath) { return { data: this.makeCopy(this.workItems[i]) }; } }; ; MockDataService.prototype.updateWorkItem = function (workItem) { var _this = this; console.log(workItem); var localWorkItem = cloneDeep(workItem); for (var i = 0; i < this.workItems.length; i++) { if (this.workItems[i].id === localWorkItem.id) { // Some relationship update if (workItem.relationships) { // Iteration update if (typeof (workItem.relationships.iteration) !== 'undefined') { if (workItem.relationships.iteration.data) this.workItems[i].relationships.iteration.data = this.getIteration(workItem.relationships.iteration.data.id); else this.workItems[i].relationships.iteration = {}; } else if (typeof (workItem.relationships.area) !== 'undefined') { if (workItem.relationships.area.data) this.workItems[i].relationships.area.data = this.getArea(workItem.relationships.area.data.id); else this.workItems[i].relationships.area = {}; } // Assignee update if (workItem.relationships.assignees && workItem.relationships.assignees.data) { if (workItem.relationships.assignees.data.length) { this.workItems[i].relationships.assignees.data = workItem.relationships.assignees.data.map(function (assignee) { return _this.getUserById(assignee.id); }); } else { this.workItems[i].relationships.assignees = {}; } } // Label update if (workItem.relationships.labels && workItem.relationships.labels.data) { if (workItem.relationships.labels.data.length) { this.workItems[i].relationships.labels.data = workItem.relationships.labels.data; } else { this.workItems[i].relationships.labels.data = []; } } } else { Object.assign(this.workItems[i].attributes, localWorkItem.attributes); // Description update this.workItems[i].attributes['system.description.rendered'] = workItem.attributes['system.description']; } return cloneDeep(this.workItems[i]); } } return null; }; MockDataService.prototype.deleteWorkItem = function (id) { for (var i = 0; i < this.workItems.length; i++) if (this.workItems[i].id === id) { this.workItems.splice(i, 1); return true; } return false; }; MockDataService.prototype.searchWorkItem = function (term) { for (var i = 0; i < this.workItems.length; i++) if (this.workItems[i].fields['system.title'].indexOf(term) != -1) { return this.makeCopy(this.workItems[i]); } return false; }; // work item links MockDataService.prototype.getWorkItemLinks = function () { return this.makeCopy(this.workItemLinks); }; MockDataService.prototype.getWorkItemLinksForWorkItem = function (workItemId) { var result = []; for (var i = 0; i < this.workItemLinks.length; i++) { if (this.workItemLinks[i].relationships.source.data.id === workItemId || this.workItemLinks[i].relationships.target.data.id === workItemId) { result.push(this.makeCopy(this.workItemLinks[i])); } } return result; }; MockDataService.prototype.createWorkItemLink = function (workItemLink) { var localWorkItemLink = this.makeCopy(workItemLink); localWorkItemLink.id = this.createId(); this.workItemLinks.push(localWorkItemLink); return this.makeCopy(localWorkItemLink); }; MockDataService.prototype.createWorkItemLinkIncludes = function (workItemLink) { var localWorkItemLink = this.makeCopy(workItemLink); var workItems = this.getWorkItems(); var workItem_1 = workItems.find(function (i) { return i.id == localWorkItemLink.relationships.source.data.id; }); var workItem_2 = workItems.find(function (i) { return i.id == localWorkItemLink.relationships.target.data.id; }); var wiLinkType = this.getWorkItemLinkTypes().data.find(function (i) { return i.id == localWorkItemLink.relationships.link_type.data.id; }); return [this.makeCopy(workItem_1), this.makeCopy(workItem_2), this.makeCopy(wiLinkType)]; }; MockDataService.prototype.updateWorkItemLink = function (workItemLink) { var localWorkItemLink = this.makeCopy(workItemLink); for (var i = 0; i < this.workItemLinks.length; i++) if (this.workItemLinks[i].id === localWorkItemLink.id) { this.workItemLinks.splice(i, 1, localWorkItemLink); return this.makeCopy(localWorkItemLink); } return null; }; MockDataService.prototype.deleteWorkItemLink = function (id) { for (var i = 0; i < this.workItemLinks.length; i++) if (this.workItemLinks[i].id === id) { this.workItemLinks.splice(i, 1); return true; } return false; }; // user and identities MockDataService.prototype.getUser = function () { return this.userMockGenerator.getUser(); }; MockDataService.prototype.getUserById = function (id) { return this.userMockGenerator.getAllUsers().find(function (u) { return u.id === id; }); }; MockDataService.prototype.getAllUsers = function () { return this.userMockGenerator.getAllUsers(); }; MockDataService.prototype.getAllLabels = function () { return this.labelMockGenerator.getAllLabels(); }; MockDataService.prototype.createLabel = function (body) { return this.labelMockGenerator.createLabel(body); }; MockDataService.prototype.getLoginStatus = function () { return { 'status': 200, 'responseText': 'Good Job' }; }; MockDataService.prototype.getFilters = function () { return this.filterMockGenerator.getFilters(); }; // spaces MockDataService.prototype.getAllSpaces = function () { return this.spaces; }; //areas MockDataService.prototype.getAllAreas = function () { return this.areas; }; MockDataService.prototype.getArea = function (id) { return this.areas.find(function (a) { return a.id === id; }); }; // iterations MockDataService.prototype.updateWorkItemCountsOnIteration = function () { console.log('Updating work item counts on service side.'); for (var i = 0; i < this.iterations.length; i++) { var thisIteration = this.iterations[i]; thisIteration.relationships.workitems.meta.total = 0; thisIteration.relationships.workitems.meta.closed = 0; // get correct work item count for iteration if (thisIteration.attributes.parent_path === '/') { // root iteration, meta count will be all of workitems thisIteration.relationships.workitems.meta.total = this.workItems.length; for (var j = 0; j < this.workItems.length; j++) { if (this.workItems[j].attributes['system.state'] === 'closed') thisIteration.relationships.workitems.meta.closed++; } console.log('Got count for root iteration: ' + thisIteration.relationships.workitems.meta.total); } else { // standard iteration for (var j = 0; j < this.workItems.length; j++) { if (this.workItems[j].relationships.iteration.data && this.workItems[j].relationships.iteration.data.id === thisIteration.id) { thisIteration.relationships.workitems.meta.total++; if (this.workItems[j].attributes['system.state'] === 'closed') thisIteration.relationships.workitems.meta.closed++; } } console.log('Got count for standard iteration: ' + thisIteration.relationships.workitems.meta.total + ' iteration id ' + thisIteration.id); } } }; MockDataService.prototype.getAllIterations = function () { this.updateWorkItemCountsOnIteration(); return this.makeCopy(this.iterations); }; MockDataService.prototype.getIteration = function (id) { this.updateWorkItemCountsOnIteration(); for (var i = 0; i < this.iterations.length; i++) if (this.iterations[i].id === id) { return this.makeCopy(this.iterations[i]); } return null; }; MockDataService.prototype.createIteration = function (iteration, parentIterationId) { console.log('CREATE ITERATION'); console.log(iteration); var localIteration = this.makeCopy(iteration.data); localIteration.id = 'iteration-id-' + this.createId(); if (!localIteration.attributes.hasOwnProperty('state') && !localIteration.attributes.state) { localIteration.attributes['state'] = 'new'; } if (localIteration.attributes['user_active'] === true) { localIteration.attributes['active_status'] = true; } else { localIteration.attributes['active_status'] = false; } if (parentIterationId) { var parentIteration = this.getIteration(parentIterationId); localIteration.attributes.parent_path = '/' + parentIteration.id; localIteration.attributes.resolved_parent_path = '/' + parentIteration.attributes.name; localIteration.relationships.parent = { 'data': { 'id': parentIteration.id, 'type': 'iteration' }, 'links': { 'self': 'http://mock.service/api/iterations/' + parentIteration.id } }; } localIteration.relationships.workitems = { 'links': { 'related': 'http://mock.service/api/workitems?filter[iteration]=' + localIteration.id }, 'meta': { 'total': 0, 'closed': 0 } }; localIteration.links = { 'self': 'http://mock.service/api/iterations/' + localIteration.id }; this.iterations.push(localIteration); this.updateWorkItemCountsOnIteration(); return this.makeCopy(localIteration); }; MockDataService.prototype.updateIteration = function (iteration) { var localIteration = this.makeCopy(iteration.data); for (var i = 0; i < this.iterations.length; i++) if (this.iterations[i].id === localIteration.id) { // TODO: we might have to do a proper merge of the values at some point. if (!localIteration.attributes.hasOwnProperty('state') || !localIteration.attributes.state) { localIteration.attributes['state'] = this.iterations[i].attributes['state']; } if (localIteration.attributes['user_active'] === true) { localIteration.attributes['active_status'] = true; } else { localIteration.attributes['active_status'] = false; } this.iterations.splice(i, 1, localIteration); return this.makeCopy(localIteration); } return null; }; MockDataService.prototype.deleteIteration = function (id) { for (var i = 0; i < this.iterations.length; i++) if (this.iterations[i].id === id) { this.iterations.splice(i, 1); return true; } return false; }; // comments MockDataService.prototype.deleteComment = function (id) { var _this = this; Object.getOwnPropertyNames(this.workItemComments).map(function (key) { var thisCommentEntry = _this.workItemComments[key]; for (var j = 0; j < thisCommentEntry.data.length; j++) { if (thisCommentEntry.data[j].id === id) { thisCommentEntry.data.splice(j, 1); delete _this.workItemComments[key]; return true; } } }); return false; }; // schemas MockDataService.prototype.getWorkItemTypes = function () { return this.schemaMockGenerator.getWorkItemTypes(); }; MockDataService.prototype.getWorkItemTypeById = function (id) { return this.schemaMockGenerator.getWorkItemTypeById(id); }; MockDataService.prototype.getWorkItemLinkTypes = function () { return this.schemaMockGenerator.getWorkItemLinkTypes(); }; MockDataService.prototype.getRedneredText = function (data) { return this.schemaMockGenerator.renderText(data.attributes.content); }; MockDataService.prototype.getAllGroupTypes = function () { return this.groupTypes; }; MockDataService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ MockDataService.ctorParameters = function () { return []; }; return MockDataService; }()); export { MockDataService }; //# sourceMappingURL=mock-data.service.js.map