@researchdatabox/sails-hook-redbox-servicenow-catalog 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/angular/catalog/e2e/app.e2e-spec.ts +14 -0
- package/angular/catalog/e2e/app.po.ts +11 -0
- package/angular/catalog/e2e/tsconfig.e2e.json +14 -0
- package/api/Config.js +42 -0
- package/api/controllers/CatalogController.js +211 -0
- package/api/services/ServicenowCatalogService.js +218 -0
- package/package.json +1 -1
- package/typescript/api/services/ServicenowCatalogService.ts +6 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AppPage } from './app.po';
|
|
2
|
+
|
|
3
|
+
describe('catalog App', () => {
|
|
4
|
+
let page: AppPage;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
page = new AppPage();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should display welcome message', () => {
|
|
11
|
+
page.navigateTo();
|
|
12
|
+
expect(page.getParagraphText()).toEqual('Welcome to app!');
|
|
13
|
+
});
|
|
14
|
+
});
|
package/api/Config.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Config = void 0;
|
|
4
|
+
class Config {
|
|
5
|
+
constructor(workspaces) {
|
|
6
|
+
this.catalogItems = [];
|
|
7
|
+
const workspaceConfig = workspaces;
|
|
8
|
+
const config = workspaceConfig.catalog;
|
|
9
|
+
this.recordType = config.recordType;
|
|
10
|
+
this.workflowStage = config.workflowStage;
|
|
11
|
+
this.formName = config.formName;
|
|
12
|
+
this.appName = config.appName;
|
|
13
|
+
this.domain = config.domain;
|
|
14
|
+
this.taskURL = config.taskURL;
|
|
15
|
+
this.requestTable = config.requestTable;
|
|
16
|
+
this.user = config.user;
|
|
17
|
+
this.password = config.password;
|
|
18
|
+
this.parentRecord = workspaceConfig.parentRecord;
|
|
19
|
+
this.provisionerUser = workspaceConfig.provisionerUser;
|
|
20
|
+
this.serverId = config.serverId;
|
|
21
|
+
this.appId = config.appId;
|
|
22
|
+
this.brandingAndPortalUrl = '';
|
|
23
|
+
this.redboxHeaders = {
|
|
24
|
+
'Cache-Control': 'no-cache',
|
|
25
|
+
'Content-Type': 'application/json',
|
|
26
|
+
'Authorization': workspaceConfig.portal.authorization,
|
|
27
|
+
};
|
|
28
|
+
this.servicenowHeaders = {
|
|
29
|
+
'Cache-Control': 'no-cache',
|
|
30
|
+
'Content-Type': 'application/json',
|
|
31
|
+
'Authorization': 'Basic ' + Buffer.from(this.user + ':' + this.password).toString('base64')
|
|
32
|
+
};
|
|
33
|
+
this.openedById = config.openedById;
|
|
34
|
+
this.assignedToEmail = config.assignedToEmail;
|
|
35
|
+
this.testRequestorId = config.testRequestorId;
|
|
36
|
+
this.defaultGroupId = config.defaultGroupId;
|
|
37
|
+
this.catalogItems = config.items;
|
|
38
|
+
this.types = config.types;
|
|
39
|
+
this.workspaceFileName = config.workspaceFileName;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.Config = Config;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Controllers = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
require("rxjs/add/operator/map");
|
|
6
|
+
const redbox_core_types_1 = require("@researchdatabox/redbox-core-types");
|
|
7
|
+
const Config_1 = require("../Config");
|
|
8
|
+
var Controllers;
|
|
9
|
+
(function (Controllers) {
|
|
10
|
+
class CatalogController extends redbox_core_types_1.Controllers.Core.Controller {
|
|
11
|
+
constructor() {
|
|
12
|
+
super();
|
|
13
|
+
this._exportedMethods = [
|
|
14
|
+
'info',
|
|
15
|
+
'rdmpInfo',
|
|
16
|
+
'request'
|
|
17
|
+
];
|
|
18
|
+
this.config = new Config_1.Config(sails.config.workspaces);
|
|
19
|
+
}
|
|
20
|
+
info(req, res) {
|
|
21
|
+
this.config.brandingAndPortalUrl = BrandingService.getFullPath(req);
|
|
22
|
+
this.ajaxOk(req, res, null, { status: true });
|
|
23
|
+
}
|
|
24
|
+
rdmpInfo(req, res) {
|
|
25
|
+
sails.log.debug('rdmpInfo');
|
|
26
|
+
const userId = req.user.id;
|
|
27
|
+
const rdmp = req.param('rdmp');
|
|
28
|
+
let recordMetadata = {};
|
|
29
|
+
this.config.brandingAndPortalUrl = BrandingService.getFullPath(req);
|
|
30
|
+
return WorkspaceService.getRecordMeta(this.config, rdmp)
|
|
31
|
+
.subscribe(response => {
|
|
32
|
+
sails.log.debug('recordMetadata');
|
|
33
|
+
recordMetadata = response;
|
|
34
|
+
this.ajaxOk(req, res, null, { status: true, recordMetadata: recordMetadata });
|
|
35
|
+
}, error => {
|
|
36
|
+
sails.log.error('recordMetadata: error');
|
|
37
|
+
sails.log.error(error);
|
|
38
|
+
this.ajaxFail(req, res, error.message, { status: false, message: error.message });
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
request(req, res) {
|
|
42
|
+
sails.log.debug('request');
|
|
43
|
+
this.config.brandingAndPortalUrl = BrandingService.getFullPath(req);
|
|
44
|
+
const userId = req.user.id;
|
|
45
|
+
const rdmp = req.param('rdmp');
|
|
46
|
+
const catalogName = req.param('catalogName');
|
|
47
|
+
const request = req.param('request');
|
|
48
|
+
const username = req.user.username;
|
|
49
|
+
const workspaceInfo = req.param('workspaceInfo');
|
|
50
|
+
const workspaceType = req.param('workspaceType');
|
|
51
|
+
const workspaceTitle = workspaceInfo['workspaceTitle'];
|
|
52
|
+
const workspaceDescription = workspaceInfo['workspaceDescription'];
|
|
53
|
+
let createTicket = null;
|
|
54
|
+
const reqInfo = {
|
|
55
|
+
"assigned_to": this.config.assignedToEmail,
|
|
56
|
+
"opened_by": this.config.openedById,
|
|
57
|
+
"requested_by": '',
|
|
58
|
+
"affected_contact": ''
|
|
59
|
+
};
|
|
60
|
+
let recordMetadata = {};
|
|
61
|
+
let rdmpTitle = '';
|
|
62
|
+
let workspaceLocation = '';
|
|
63
|
+
let requestorName = '';
|
|
64
|
+
let emailPermissions = [];
|
|
65
|
+
let request_number = '';
|
|
66
|
+
let catalogId;
|
|
67
|
+
let catalogItem;
|
|
68
|
+
if (this.config.catalogItems) {
|
|
69
|
+
catalogItem = this.config.catalogItems.find((item) => {
|
|
70
|
+
return item['name'] === catalogName;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const errorMessage = 'No service-now catalog items were configured, please contact your system administrator';
|
|
75
|
+
this.ajaxFail(req, res, errorMessage, { status: false, message: errorMessage });
|
|
76
|
+
}
|
|
77
|
+
if (catalogItem) {
|
|
78
|
+
catalogId = catalogItem['id'];
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
const errorMessage = 'No service-now catalog was found, please contact your system administrator';
|
|
82
|
+
this.ajaxFail(req, res, errorMessage, { status: false, message: errorMessage });
|
|
83
|
+
}
|
|
84
|
+
if (request['data_manager'] && request['data_manager']['value']) {
|
|
85
|
+
reqInfo.requested_by = request['data_manager']['value'];
|
|
86
|
+
try {
|
|
87
|
+
requestorName = reqInfo.requested_by.substr(0, reqInfo.requested_by.indexOf('.'));
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
sails.log.error('Requested By name Error');
|
|
91
|
+
sails.log.error(e);
|
|
92
|
+
requestorName = 'Stash User';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
const errorMessage = `No data manager in plan to assign ticket`;
|
|
97
|
+
this.ajaxFail(req, res, errorMessage, { status: false, message: errorMessage });
|
|
98
|
+
}
|
|
99
|
+
if (request['data_supervisor'] && request['data_supervisor']['value']) {
|
|
100
|
+
reqInfo.affected_contact = request['data_supervisor']['value'];
|
|
101
|
+
emailPermissions.push(reqInfo.affected_contact);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
const errorMessage = `No supervisor in plan to assign ticket`;
|
|
105
|
+
this.ajaxFail(req, res, errorMessage, { status: false, message: errorMessage });
|
|
106
|
+
}
|
|
107
|
+
if (reqInfo.requested_by && reqInfo.assigned_to && catalogId) {
|
|
108
|
+
sails.log.debug('----------');
|
|
109
|
+
sails.log.debug(`Catalog ${catalogName} id: ${catalogId}`);
|
|
110
|
+
return CatalogService.sendGetToTable('sys_user', { email: reqInfo.assigned_to })
|
|
111
|
+
.flatMap(response => {
|
|
112
|
+
if (response && response['result']) {
|
|
113
|
+
const result = _.first(response['result']);
|
|
114
|
+
reqInfo.assigned_to = result['sys_id'];
|
|
115
|
+
sails.log.debug(`assigned_to ${reqInfo.assigned_to}`);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const errorMessage = 'Cannot find user to assign ticket on Service Now';
|
|
119
|
+
sails.log.error(errorMessage);
|
|
120
|
+
sails.log.error(response);
|
|
121
|
+
throw (0, rxjs_1.throwError)(errorMessage);
|
|
122
|
+
}
|
|
123
|
+
return CatalogService.sendGetToTable('sys_user', { email: reqInfo.requested_by });
|
|
124
|
+
}).flatMap(response => {
|
|
125
|
+
sails.log.debug('requestToVariables');
|
|
126
|
+
const variables = this.requestToVariables(request);
|
|
127
|
+
variables['rdmp'] = `${this.config.brandingAndPortalUrl}/record/view/${rdmp}`;
|
|
128
|
+
variables['requestor'] = requestorName;
|
|
129
|
+
if (response && response['result']) {
|
|
130
|
+
const result = _.first(response['result']);
|
|
131
|
+
sails.log.debug(`requestedByEmail ${reqInfo.requested_by}`);
|
|
132
|
+
sails.log.debug(result['sys_id']);
|
|
133
|
+
reqInfo.requested_by = result['sys_id'];
|
|
134
|
+
variables['user_id'] = reqInfo.requested_by;
|
|
135
|
+
variables['opened_by'] = reqInfo.requested_by;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
sails.log.error(response);
|
|
139
|
+
throw (0, rxjs_1.throwError)('Cannot find requested_by Id on Service Now');
|
|
140
|
+
}
|
|
141
|
+
sails.log.debug(variables);
|
|
142
|
+
sails.log.debug('----------');
|
|
143
|
+
return CatalogService.serviceCatalogPost('/api/sn_sc/servicecatalog/items/', catalogId, 'order_now', '1', variables, reqInfo.assigned_to, reqInfo.opened_by, reqInfo.requested_by);
|
|
144
|
+
}).flatMap(response => {
|
|
145
|
+
sails.log.debug('create catalog item');
|
|
146
|
+
if (response && response['result']) {
|
|
147
|
+
const result = response['result'];
|
|
148
|
+
request_number = result['request_number'];
|
|
149
|
+
workspaceLocation = `${this.config.domain}${this.config.taskURL}${result['sys_id']}`;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
workspaceLocation = `${this.config.domain}`;
|
|
153
|
+
}
|
|
154
|
+
return WorkspaceService.getRecordMeta(this.config, rdmp);
|
|
155
|
+
}).flatMap(response => {
|
|
156
|
+
sails.log.debug('get recordMetadata');
|
|
157
|
+
recordMetadata = response;
|
|
158
|
+
rdmpTitle = recordMetadata['title'];
|
|
159
|
+
const record = {
|
|
160
|
+
rdmpOid: rdmp,
|
|
161
|
+
rdmpTitle: rdmpTitle,
|
|
162
|
+
title: workspaceTitle,
|
|
163
|
+
location: workspaceLocation,
|
|
164
|
+
description: request_number + ' : ' + workspaceType + ' ' + workspaceDescription,
|
|
165
|
+
type: this.config.recordType
|
|
166
|
+
};
|
|
167
|
+
return WorkspaceService.createWorkspaceRecord(this.config, username, record, this.config.recordType, this.config.workflowStage, emailPermissions);
|
|
168
|
+
})
|
|
169
|
+
.flatMap(workspace => {
|
|
170
|
+
sails.log.debug('create WorkspaceRecord');
|
|
171
|
+
if (recordMetadata['workspaces']) {
|
|
172
|
+
const wss = recordMetadata['workspaces'].find(id => workspace.oid === id);
|
|
173
|
+
if (!wss) {
|
|
174
|
+
recordMetadata['workspaces'].push({ id: workspace.oid });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return WorkspaceService.updateRecordMeta(this.config, recordMetadata, rdmp);
|
|
178
|
+
})
|
|
179
|
+
.subscribe(response => {
|
|
180
|
+
sails.log.debug('createTicket, linkWorkspace');
|
|
181
|
+
createTicket = response;
|
|
182
|
+
this.ajaxOk(req, res, null, {
|
|
183
|
+
status: true,
|
|
184
|
+
createTicket: createTicket,
|
|
185
|
+
request_number: request_number,
|
|
186
|
+
workspaceLocation: workspaceLocation
|
|
187
|
+
});
|
|
188
|
+
}, error => {
|
|
189
|
+
sails.log.error('request: error');
|
|
190
|
+
const errorMessage = 'There was an error submitting your request. Please contact support team';
|
|
191
|
+
sails.log.error(`${errorMessage} ${error.message}`);
|
|
192
|
+
this.ajaxFail(req, res, error.message, { status: false, message: errorMessage });
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
requestToVariables(request) {
|
|
197
|
+
const variables = {};
|
|
198
|
+
_.forOwn(request, (val) => {
|
|
199
|
+
const v = val['variable'];
|
|
200
|
+
const vv = val['value'];
|
|
201
|
+
variables[v] = vv['name'] || vv;
|
|
202
|
+
if (Array.isArray(variables[v]) && variables[v].length === 1) {
|
|
203
|
+
variables[v] = _.first(variables[v]);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
return variables;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
Controllers.CatalogController = CatalogController;
|
|
210
|
+
})(Controllers = exports.Controllers || (exports.Controllers = {}));
|
|
211
|
+
module.exports = new Controllers.CatalogController().exports();
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.Services = void 0;
|
|
39
|
+
const numeral = __importStar(require("numeral"));
|
|
40
|
+
const moment = require("moment");
|
|
41
|
+
const axios_1 = __importDefault(require("axios"));
|
|
42
|
+
const util = require('util');
|
|
43
|
+
const redbox_core_types_1 = require("@researchdatabox/redbox-core-types");
|
|
44
|
+
const axios_oauth2_1 = require("@scienta/axios-oauth2");
|
|
45
|
+
var Services;
|
|
46
|
+
(function (Services) {
|
|
47
|
+
class ServicenowCatalogService extends redbox_core_types_1.Services.Core.Service {
|
|
48
|
+
constructor() {
|
|
49
|
+
super(...arguments);
|
|
50
|
+
this._exportedMethods = [
|
|
51
|
+
'submitRequest',
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
submitRequest(oid, workspaceData, options, user, response) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
sails.log.verbose(`ServiceNowCatalog processing request for: ${oid}`);
|
|
57
|
+
sails.log.verbose("Global config:");
|
|
58
|
+
sails.log.verbose(JSON.stringify(sails.config.servicenow));
|
|
59
|
+
sails.log.verbose("Hook config:");
|
|
60
|
+
sails.log.verbose(JSON.stringify(options));
|
|
61
|
+
let catalogUrl = this.getConfig('catalog.axios.url', options);
|
|
62
|
+
if (_.isEmpty(catalogUrl)) {
|
|
63
|
+
const errMsg = "Missing ServiceNow Catalog URL configuration.";
|
|
64
|
+
sails.log.error(errMsg);
|
|
65
|
+
response.code = "500";
|
|
66
|
+
response.status = false;
|
|
67
|
+
response.success = false;
|
|
68
|
+
response.message = errMsg;
|
|
69
|
+
return response;
|
|
70
|
+
}
|
|
71
|
+
sails.log.verbose(`ServiceNowCatalog adding workspace to record...`);
|
|
72
|
+
try {
|
|
73
|
+
response = yield WorkspaceService.addWorkspaceToRecord(workspaceData.metadata.rdmpOid, oid);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
sails.log.error(`ServiceNowCatalog failed to associate workspace to record: ${workspaceData.metadata.rdmpOid}`);
|
|
77
|
+
sails.log.error(JSON.stringify(err));
|
|
78
|
+
return response;
|
|
79
|
+
}
|
|
80
|
+
let body_template = this.getConfig('catalog.body_template', options);
|
|
81
|
+
sails.log.verbose(`ServiceNowCatalog body template:`);
|
|
82
|
+
sails.log.verbose(JSON.stringify(body_template));
|
|
83
|
+
const rdmpData = yield RecordsService.getMeta(workspaceData.metadata.rdmpOid);
|
|
84
|
+
const catalogFields = this.getConfig('catalog.fields', options);
|
|
85
|
+
const outgoingDataSource = { workspace: workspaceData, rdmp: rdmpData, oid: oid, moment: moment, numeral: numeral, translationService: TranslationService };
|
|
86
|
+
body_template = this.runDataRemap(outgoingDataSource, body_template, catalogFields);
|
|
87
|
+
sails.log.verbose(`ServiceNowCatalog sending request via: ${catalogUrl}, options:`);
|
|
88
|
+
const axiosOptions = this.getConfig('catalog.axios', options);
|
|
89
|
+
axiosOptions.data = body_template;
|
|
90
|
+
sails.log.verbose(JSON.stringify(axiosOptions));
|
|
91
|
+
let snResponse = null;
|
|
92
|
+
try {
|
|
93
|
+
const oauthOptions = this.getConfig('catalog.oauth', options);
|
|
94
|
+
sails.log.verbose(JSON.stringify(oauthOptions));
|
|
95
|
+
let oauthEnabled = _.get(oauthOptions, 'enabled', 'false');
|
|
96
|
+
sails.log.verbose('OAuth enabled ' + oauthEnabled);
|
|
97
|
+
if (oauthEnabled == 'true') {
|
|
98
|
+
let oauthUrl = _.get(oauthOptions, 'url');
|
|
99
|
+
let oauthClientId = _.get(oauthOptions, 'clientId');
|
|
100
|
+
let oauthClientSecret = _.get(oauthOptions, 'clientSecret');
|
|
101
|
+
let oauthScope = _.get(oauthOptions, 'scope');
|
|
102
|
+
let oauthGrantType = _.get(oauthOptions, 'grantType');
|
|
103
|
+
sails.log.verbose('================================================');
|
|
104
|
+
sails.log.verbose('oauthUrl ' + oauthUrl);
|
|
105
|
+
sails.log.verbose('oauthClientId ' + oauthClientId);
|
|
106
|
+
sails.log.verbose('oauthClientSecret ' + oauthClientSecret);
|
|
107
|
+
sails.log.verbose('oauthScope ' + oauthScope);
|
|
108
|
+
sails.log.verbose('oauthGrantType ' + oauthGrantType);
|
|
109
|
+
let oauthConfig = {};
|
|
110
|
+
if (oauthGrantType == 'password') {
|
|
111
|
+
let oauthUsername = _.get(oauthOptions, 'username');
|
|
112
|
+
let oauthPassword = _.get(oauthOptions, 'password');
|
|
113
|
+
sails.log.verbose('oauthUsername ' + oauthUsername);
|
|
114
|
+
sails.log.verbose('oauthPassword ' + oauthPassword);
|
|
115
|
+
oauthConfig = {
|
|
116
|
+
url: oauthUrl,
|
|
117
|
+
grant_type: oauthGrantType,
|
|
118
|
+
client_id: oauthClientId,
|
|
119
|
+
client_secret: oauthClientSecret,
|
|
120
|
+
scope: oauthScope,
|
|
121
|
+
username: oauthUsername,
|
|
122
|
+
password: oauthPassword
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
else if (oauthGrantType == 'refresh_token') {
|
|
126
|
+
let oauthRefreshToken = _.get(oauthOptions, 'refreshToken');
|
|
127
|
+
oauthConfig = {
|
|
128
|
+
url: oauthUrl,
|
|
129
|
+
grant_type: oauthGrantType,
|
|
130
|
+
client_id: oauthClientId,
|
|
131
|
+
client_secret: oauthClientSecret,
|
|
132
|
+
scope: oauthScope,
|
|
133
|
+
'refresh_token': oauthRefreshToken
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
oauthConfig = {
|
|
138
|
+
url: oauthUrl,
|
|
139
|
+
grant_type: 'client_credentials',
|
|
140
|
+
client_id: oauthClientId,
|
|
141
|
+
client_secret: oauthClientSecret,
|
|
142
|
+
scope: oauthScope
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const client = (0, axios_oauth2_1.clientFactory)(axios_1.default.create(), oauthConfig);
|
|
146
|
+
const auth1 = yield client();
|
|
147
|
+
sails.log.verbose(JSON.stringify(auth1));
|
|
148
|
+
sails.log.verbose('================================================');
|
|
149
|
+
_.set(axiosOptions, 'headers.Authorization', 'Bearer ' + _.get(auth1, 'access_token'));
|
|
150
|
+
}
|
|
151
|
+
snResponse = yield (0, axios_1.default)(axiosOptions);
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
sails.log.error(`ServiceNowCatalog failed to submit to request for workspace OID: ${oid}, error is:`);
|
|
155
|
+
sails.log.error(JSON.stringify(err));
|
|
156
|
+
response.code = `${err.code}`;
|
|
157
|
+
response.status = false;
|
|
158
|
+
response.success = false;
|
|
159
|
+
response.message = `ServiceNowCatalog failed to submit to request for workspace OID: ${oid}, check server logs.`;
|
|
160
|
+
}
|
|
161
|
+
if (!_.isNull(snResponse)) {
|
|
162
|
+
if (snResponse.status == 200) {
|
|
163
|
+
const updateSource = snResponse.data;
|
|
164
|
+
updateSource.workspace = workspaceData;
|
|
165
|
+
workspaceData = this.runDataRemap(updateSource, workspaceData, this.getConfig('catalog.update_fields', options));
|
|
166
|
+
const updateResp = yield RecordsService.updateMeta(null, oid, workspaceData);
|
|
167
|
+
if (this.getConfig('returnWorkspaceUpdate', options)) {
|
|
168
|
+
response = updateResp;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
sails.log.error(`ServiceNowCatalog submit request failed for workspace OID: ${oid}, check server logs.`);
|
|
173
|
+
sails.log.error(JSON.stringify(snResponse));
|
|
174
|
+
response.code = `${snResponse.status}`;
|
|
175
|
+
response.status = false;
|
|
176
|
+
response.success = false;
|
|
177
|
+
response.message = `ServiceNowCatalog failed to submit to request for workspace OID: ${oid}, check server logs.`;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
sails.log.verbose(`ServiceNowCatalog completed request for ${oid}`);
|
|
181
|
+
return response;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
getConfig(fieldPath, options) {
|
|
185
|
+
return _.get(options, fieldPath, _.get(sails.config.servicenow, fieldPath));
|
|
186
|
+
}
|
|
187
|
+
runDataRemap(source, target, fields) {
|
|
188
|
+
for (const fieldDef of fields) {
|
|
189
|
+
const source_field = _.get(fieldDef, 'source_field');
|
|
190
|
+
const dest_field = _.get(fieldDef, 'dest_field');
|
|
191
|
+
const dest_template = _.get(fieldDef, 'dest_template');
|
|
192
|
+
let parseObject = _.get(fieldDef, 'parseObject', false);
|
|
193
|
+
let src_data = null;
|
|
194
|
+
if (!_.isEmpty(source_field)) {
|
|
195
|
+
src_data = _.get(source, source_field);
|
|
196
|
+
}
|
|
197
|
+
if (!_.isEmpty(dest_template)) {
|
|
198
|
+
const imports = _.extend({ data: src_data, config: fieldDef, moment: moment, numeral: numeral, translationService: TranslationService }, source);
|
|
199
|
+
const templateData = { imports: imports };
|
|
200
|
+
const template = _.template(dest_template, templateData);
|
|
201
|
+
src_data = template();
|
|
202
|
+
}
|
|
203
|
+
if (!_.isEmpty(dest_field)) {
|
|
204
|
+
if (parseObject) {
|
|
205
|
+
let obj = JSON.parse(src_data);
|
|
206
|
+
_.set(target, dest_field, obj);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
_.set(target, dest_field, src_data);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return target;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
Services.ServicenowCatalogService = ServicenowCatalogService;
|
|
217
|
+
})(Services = exports.Services || (exports.Services = {}));
|
|
218
|
+
module.exports = new Services.ServicenowCatalogService().exports();
|
package/package.json
CHANGED
|
@@ -148,8 +148,13 @@ export module Services {
|
|
|
148
148
|
if (snResponse.status == 200) {
|
|
149
149
|
const updateSource = snResponse.data;
|
|
150
150
|
// perform any updates to the workspace record ...
|
|
151
|
+
updateSource.workspace = workspaceData;
|
|
151
152
|
workspaceData = this.runDataRemap(updateSource, workspaceData, this.getConfig('catalog.update_fields', options));
|
|
152
|
-
await RecordsService.updateMeta(null, oid, workspaceData);
|
|
153
|
+
const updateResp = await RecordsService.updateMeta(null, oid, workspaceData);
|
|
154
|
+
// As of writing, the `response` object holds the state of the `addWorkspaceToRecord` call. For backwards-compat adding an attribute to return the workspace update instead
|
|
155
|
+
if (this.getConfig('returnWorkspaceUpdate', options)) {
|
|
156
|
+
response = updateResp;
|
|
157
|
+
}
|
|
153
158
|
} else {
|
|
154
159
|
sails.log.error(`ServiceNowCatalog submit request failed for workspace OID: ${oid}, check server logs.`);
|
|
155
160
|
sails.log.error(JSON.stringify(snResponse));
|