feat(jira): add error notifications and fix issue search

This commit is contained in:
Johannes Millan 2018-11-21 20:00:59 +01:00
parent b0286bd1bc
commit 44624459d1

View file

@ -9,23 +9,25 @@ import { JiraIssue } from './jira-issue/jira-issue.model';
import { JiraCfg } from './jira';
import { ElectronService } from 'ngx-electron';
import { IPC_JIRA_CB_EVENT, IPC_JIRA_MAKE_REQUEST_EVENT } from '../../../ipc-events.const';
import { SnackService } from '../../core/snack/snack.service';
@Injectable({
providedIn: 'root'
})
export class JiraApiService {
requestsLog = {};
isPreventNextRequestAfterFailedAuth = false;
isExtension = false;
cfg: any = {};
private _requestsLog = {};
private _isPreventNextRequestAfterFailedAuth = false;
private _isExtension = false;
private _cfg: JiraCfg;
constructor(
private _chromeExtensionInterface: ChromeExtensionInterfaceService,
private _projectService: ProjectService,
private _electronService: ElectronService,
private _snackService: SnackService,
) {
this._projectService.currentJiraCfg$.subscribe((cfg) => {
this.cfg = cfg;
this._cfg = cfg;
});
// set up callback listener for electron
@ -37,7 +39,7 @@ export class JiraApiService {
this._chromeExtensionInterface.isReady$
.subscribe(() => {
this.isExtension = true;
this._isExtension = true;
this._chromeExtensionInterface.addEventListener('SP_JIRA_RESPONSE', (ev, data) => {
this._handleResponse(data);
});
@ -50,7 +52,9 @@ export class JiraApiService {
maxResults: maxResults,
fields: isFetchAdditional ? JIRA_ADDITIONAL_ISSUE_FIELDS : JIRA_REDUCED_ISSUE_FIELDS,
};
const searchQuery = `summary ~ "${searchTerm}"${this.cfg.jqlQuery ? ' AND ' + this.cfg.jqlQuery : ''}`;
const searchQuery = `(text ~ "${searchTerm}" OR key = "${searchTerm}")`
+ ' ' + this._cfg.searchJqlQuery ? ` AND ${this._cfg.searchJqlQuery}` : '';
return this._sendRequest({
apiMethod: 'searchJira',
arguments: [searchQuery, options],
@ -148,7 +152,7 @@ export class JiraApiService {
}
// TODO refactor data madness of request and add types for everything
private _sendRequest(request, cfg = this.cfg): Promise<any> {
private _sendRequest(request, cfg = this._cfg): Promise<any> {
if (!this._isMinimalSettings(cfg)) {
console.error('Not enough Jira settings. This should not happen!!!');
return Promise.reject(new Error('Insufficient Settings for Jira'));
@ -170,7 +174,7 @@ export class JiraApiService {
});
// save to request log
this.requestsLog[request.requestId] = {
this._requestsLog[request.requestId] = {
transform: request.transform,
resolve: promiseResolve,
reject: promiseReject,
@ -179,15 +183,16 @@ export class JiraApiService {
timeout: setTimeout(() => {
console.log('ERROR', 'Jira Request timed out for ' + request.apiMethod);
// delete entry for promise
this.requestsLog[request.requestId].reject('Request timed out');
delete this.requestsLog[request.requestId];
this._snackService.open({type: 'ERROR', message: 'Jira timed out'});
this._requestsLog[request.requestId].reject('Request timed out');
delete this._requestsLog[request.requestId];
}, JIRA_REQUEST_TIMEOUT_DURATION)
};
// send to electron
if (this._electronService.isElectronApp) {
this._electronService.ipcRenderer.send(IPC_JIRA_MAKE_REQUEST_EVENT, request);
} else if (this.isExtension) {
} else if (this._isExtension) {
this._chromeExtensionInterface.dispatchEvent('SP_JIRA_REQUEST', {
requestId: request.requestId,
apiMethod: request.apiMethod,
@ -206,8 +211,8 @@ export class JiraApiService {
private _handleResponse(res) {
// check if proper id is given in callback and if exists in requestLog
if (res.requestId && this.requestsLog[res.requestId]) {
const currentRequest = this.requestsLog[res.requestId];
if (res.requestId && this._requestsLog[res.requestId]) {
const currentRequest = this._requestsLog[res.requestId];
// cancel timeout for request
clearTimeout(currentRequest.timeout);
@ -219,22 +224,23 @@ export class JiraApiService {
const errorTxt = (res && res.error && (typeof res.error === 'string' && res.error) || res.error.name);
console.error(errorTxt);
this._snackService.open({type: 'ERROR', message: 'Jira request failed'});
currentRequest.reject(res);
if (res.error.statusCode && res.error.statusCode === 401) {
this.isPreventNextRequestAfterFailedAuth = true;
this._isPreventNextRequestAfterFailedAuth = true;
}
} else {
console.log('JIRA_RESPONSE', res);
if (currentRequest.transform) {
currentRequest.resolve(currentRequest.transform(res, this.cfg));
currentRequest.resolve(currentRequest.transform(res, this._cfg));
} else {
currentRequest.resolve(res);
}
}
// delete entry for promise afterwards
delete this.requestsLog[res.requestId];
delete this._requestsLog[res.requestId];
} else {
console.warn('Jira: Response Request ID not existing');
}