|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +function ApiService($http, apiEndpoint, apiEndpointRedirect, $q) { |
| 4 | + this.http = $http; |
| 5 | + this.apiEndpoint = apiEndpoint; |
| 6 | + this.apiEndpointRedirect = apiEndpointRedirect; |
| 7 | + this.q = $q; |
| 8 | +} |
| 9 | + |
| 10 | +ApiService.prototype.request = function (param) { |
| 11 | + return this.http.get(this.apiEndpoint + param).then(function (response) { |
| 12 | + return response.data; |
| 13 | + }); |
| 14 | +}; |
| 15 | + |
| 16 | +ApiService.prototype.getProjects = function(){ |
| 17 | + return this.request('projects.json').then(function (data) { |
| 18 | + return data.projects; |
| 19 | + }); |
| 20 | +}; |
| 21 | + |
| 22 | +ApiService.prototype.getVersions = function (project) { |
| 23 | + return this.request('projects/' + project + '/versions.json').then(function (data) { |
| 24 | + return data.versions; |
| 25 | + }); |
| 26 | +}; |
| 27 | + |
| 28 | +ApiService.prototype.getIssues = function (config) { |
| 29 | + var params = this.generateGeneralIssuesParameters(config); |
| 30 | + var limit = config.limit ? config.limit : Number.MAX_SAFE_INTEGER; |
| 31 | + return this.getIssuesWithParamsAndLimit(params, limit); |
| 32 | +}; |
| 33 | + |
| 34 | +ApiService.prototype.getIssuesForChart = function(config) { |
| 35 | + var allIssues = []; |
| 36 | + var limit = config.limit ? config.limit : Number.MAX_SAFE_INTEGER; |
| 37 | + var params1 = this.generateParametersForIssuesOpenOnEnd(config); |
| 38 | + var params2 = this.generateParametersForIssuesClosedBetweenStartAndEnd(config); |
| 39 | + var params3 = this.generateParametersForIssuesOpen(config); |
| 40 | + return this.q.all([this.getIssuesWithParamsAndLimit(params1, limit), this.getIssuesWithParamsAndLimit(params2, limit), |
| 41 | + this.getIssuesWithParamsAndLimit(params3, limit)]).then(function (responses) { |
| 42 | + angular.forEach(responses, function (issues) { |
| 43 | + angular.forEach(issues, function (issue) { |
| 44 | + allIssues.push(issue); |
| 45 | + }); |
| 46 | + }); |
| 47 | + return allIssues; |
| 48 | + }); |
| 49 | +}; |
| 50 | + |
| 51 | +ApiService.prototype.getIssuesWithParamsAndLimit = function (params, limit) { |
| 52 | + var allIssues = []; |
| 53 | + return this.collectPageIssues(params, 0).then(function (issues) { |
| 54 | + angular.forEach(issues.issues, function (issue) { |
| 55 | + allIssues.push(issue); |
| 56 | + }); |
| 57 | + var requests = []; |
| 58 | + for (var i = 100; i < issues.total_count && i < limit; i = i + 100) { |
| 59 | + requests.push(this.collectPageIssues(params, i)); |
| 60 | + } |
| 61 | + if (params.length > 0) { |
| 62 | + return this.q.all(requests).then(function (responses) { |
| 63 | + angular.forEach(responses, function (response) { |
| 64 | + angular.forEach(response.issues, function (issue) { |
| 65 | + allIssues.push(issue); |
| 66 | + }); |
| 67 | + }); |
| 68 | + return allIssues; |
| 69 | + }); |
| 70 | + } else { |
| 71 | + return allIssues; |
| 72 | + } |
| 73 | + }.bind(this)); |
| 74 | +}; |
| 75 | + |
| 76 | + |
| 77 | +ApiService.prototype.collectPageIssues = function (params, offset) { |
| 78 | + return this.request('issues.json' + params + '&offset=' + offset).then(function (issues) { |
| 79 | + return issues; |
| 80 | + }); |
| 81 | +}; |
| 82 | + |
| 83 | +ApiService.prototype.generateParametersForIssuesOpenOnEnd = function (data) { |
| 84 | + var params = this.generateGeneralIssuesParameters(data); |
| 85 | + params += '&status_id=*'; |
| 86 | + var toDate = new Date(data.timespan.toDateTime); |
| 87 | + params += '&created_on=<=' + this.dateToYMD(toDate); |
| 88 | + params += '&closed_on=>=' + this.dateToYMD(toDate); |
| 89 | + return params; |
| 90 | +}; |
| 91 | + |
| 92 | +ApiService.prototype.generateParametersForIssuesOpen = function (data) { |
| 93 | + var params = this.generateGeneralIssuesParameters(data); |
| 94 | + params += '&status_id=open'; |
| 95 | + var toDate = new Date(data.timespan.toDateTime); |
| 96 | + params += '&created_on=<=' + this.dateToYMD(toDate); |
| 97 | + return params; |
| 98 | +}; |
| 99 | + |
| 100 | +ApiService.prototype.generateParametersForIssuesClosedBetweenStartAndEnd = function (data) { |
| 101 | + var params = this.generateGeneralIssuesParameters(data); |
| 102 | + params += '&status_id=*'; |
| 103 | + var fromDate = new Date(data.timespan.fromDateTime); |
| 104 | + var toDate = new Date(data.timespan.toDateTime); |
| 105 | + params += '&closed_on=><' + this.dateToYMD(fromDate) + '|' + this.dateToYMD(toDate); |
| 106 | + return params; |
| 107 | +}; |
| 108 | + |
| 109 | +ApiService.prototype.generateGeneralIssuesParameters = function (data) { |
| 110 | + var params = '?limit=100&sort=created_on'; |
| 111 | + if (data.project && data.project !== 'All') { |
| 112 | + params += '&project_id=' + angular.fromJson(data.project).id; |
| 113 | + } |
| 114 | + if (data.filterWithAssigned && data.assigned_to_id) { |
| 115 | + params += '&assigned_to_id=' + data.assigned_to_id; |
| 116 | + } |
| 117 | + if (data.showClosed) { |
| 118 | + params += '&status_id=*'; |
| 119 | + } |
| 120 | + if (data.filterWithVersion && data.version) { |
| 121 | + params += '&fixed_version_id=' + angular.fromJson(data.version).id; |
| 122 | + } |
| 123 | + if (data.filterWithTracker && data.tracker) { |
| 124 | + params += '&tracker_id=' + angular.fromJson(data.tracker).id; |
| 125 | + } |
| 126 | + return params; |
| 127 | +}; |
| 128 | + |
| 129 | +ApiService.prototype.dateToYMD = function (date) { |
| 130 | + var d = date.getDate(); |
| 131 | + var m = date.getMonth() + 1; |
| 132 | + var y = date.getFullYear(); |
| 133 | + return '' + y + '-' + (m <= 9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d); |
| 134 | +}; |
| 135 | + |
| 136 | +ApiService.prototype.getCustomQueries = function () { |
| 137 | + return this.request('queries.json?limit=100'); |
| 138 | +}; |
| 139 | + |
| 140 | +ApiService.prototype.getIssuesByQueryId = function (queryId, projectId) { |
| 141 | + return this.request('issues.json?query_id=' + queryId + '&project_id=' + projectId).then(function (data) { |
| 142 | + return data.issues; |
| 143 | + }); |
| 144 | +}; |
| 145 | + |
| 146 | +ApiService.prototype.getRedmineEndpoint = function () { |
| 147 | + return this.apiEndpoint; |
| 148 | +}; |
| 149 | + |
| 150 | +ApiService.prototype.getRedmineRedirectEndpoint = function () { |
| 151 | + return this.apiEndpointRedirect; |
| 152 | +}; |
| 153 | + |
| 154 | +ApiService.prototype.getTrackers = function () { |
| 155 | + return this.request('trackers.json').then(function (data) { |
| 156 | + return data.trackers; |
| 157 | + }); |
| 158 | +}; |
| 159 | + |
| 160 | +ApiService.prototype.getMyIssues = function () { |
| 161 | + return this.request('issues.json?assigned_to_id=me').then(function (data) { |
| 162 | + return data; |
| 163 | + }); |
| 164 | +}; |
0 commit comments