@researchdatabox/sails-hook-redbox-servicenow-catalog 0.0.10 → 0.0.12
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/.circleci/config.yml +1 -1
- package/.nvmrc +1 -0
- package/package.json +3 -2
- package/tsconfig.json +2 -1
- package/typescript/api/services/ServicenowCatalogService.ts +74 -5
- package/api/Config.js +0 -42
- package/api/controllers/CatalogController.js +0 -211
- package/api/services/ServicenowCatalogService.js +0 -122
package/.circleci/config.yml
CHANGED
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v20.11.1
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@researchdatabox/sails-hook-redbox-servicenow-catalog",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "ReDBox Workspace provisioner plugin for creating ServiceNow Catalog items.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"sails": {
|
|
@@ -42,7 +42,8 @@
|
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@researchdatabox/redbox-core-types": "^1.0.5-beta",
|
|
45
|
-
"axios": "^
|
|
45
|
+
"@scienta/axios-oauth2": "^1.2.1",
|
|
46
|
+
"axios": "^1.6.7",
|
|
46
47
|
"fs-extra": "^8.1.0",
|
|
47
48
|
"lodash": "4.17.14",
|
|
48
49
|
"lodash-es": "4.17.14",
|
package/tsconfig.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {Sails, Model} from 'sails';
|
|
2
2
|
import {Observable, from, of, throwError} from 'rxjs';
|
|
3
3
|
import * as numeral from 'numeral';
|
|
4
|
-
// import * as got from 'got';
|
|
5
4
|
import moment = require('moment');
|
|
6
5
|
import axios from 'axios';
|
|
7
6
|
const util = require('util');
|
|
8
7
|
import { RecordsService, Services as services} from '@researchdatabox/redbox-core-types';
|
|
8
|
+
import {clientFactory, interceptorFactory} from '@scienta/axios-oauth2';
|
|
9
9
|
|
|
10
10
|
declare var sails: Sails;
|
|
11
11
|
declare var WorkspaceService, _;
|
|
@@ -70,9 +70,72 @@ export module Services {
|
|
|
70
70
|
const axiosOptions = this.getConfig('catalog.axios', options);
|
|
71
71
|
axiosOptions.data = body_template;
|
|
72
72
|
sails.log.verbose(JSON.stringify(axiosOptions));
|
|
73
|
+
|
|
73
74
|
let snResponse = null;
|
|
74
75
|
try {
|
|
76
|
+
const oauthOptions = this.getConfig('catalog.oauth', options);
|
|
77
|
+
sails.log.verbose(JSON.stringify(oauthOptions));
|
|
78
|
+
let oauthEnabled = _.get(oauthOptions, 'enabled', 'false');
|
|
79
|
+
sails.log.verbose('OAuth enabled '+oauthEnabled);
|
|
80
|
+
if(oauthEnabled == 'true') {
|
|
81
|
+
let oauthUrl = _.get(oauthOptions, 'url');
|
|
82
|
+
let oauthClientId = _.get(oauthOptions, 'clientId');
|
|
83
|
+
let oauthClientSecret = _.get(oauthOptions, 'clientSecret');
|
|
84
|
+
let oauthScope = _.get(oauthOptions, 'scope');
|
|
85
|
+
let oauthGrantType = _.get(oauthOptions, 'grantType');
|
|
86
|
+
sails.log.verbose('================================================');
|
|
87
|
+
sails.log.verbose('oauthUrl '+oauthUrl);
|
|
88
|
+
sails.log.verbose('oauthClientId '+oauthClientId);
|
|
89
|
+
sails.log.verbose('oauthClientSecret '+oauthClientSecret);
|
|
90
|
+
sails.log.verbose('oauthScope '+oauthScope);
|
|
91
|
+
sails.log.verbose('oauthGrantType '+oauthGrantType);
|
|
92
|
+
|
|
93
|
+
let oauthConfig = {};
|
|
94
|
+
if(oauthGrantType == 'password') {
|
|
95
|
+
let oauthUsername = _.get(oauthOptions, 'username');
|
|
96
|
+
let oauthPassword = _.get(oauthOptions, 'password');
|
|
97
|
+
sails.log.verbose('oauthUsername '+oauthUsername);
|
|
98
|
+
sails.log.verbose('oauthPassword '+oauthPassword);
|
|
99
|
+
oauthConfig = {
|
|
100
|
+
url: oauthUrl,
|
|
101
|
+
grant_type: oauthGrantType,
|
|
102
|
+
client_id: oauthClientId,
|
|
103
|
+
client_secret: oauthClientSecret,
|
|
104
|
+
scope: oauthScope,
|
|
105
|
+
username: oauthUsername,
|
|
106
|
+
password: oauthPassword
|
|
107
|
+
};
|
|
108
|
+
} else if(oauthGrantType == 'refresh_token') {
|
|
109
|
+
let oauthRefreshToken = _.get(oauthOptions, 'refreshToken');
|
|
110
|
+
oauthConfig = {
|
|
111
|
+
url: oauthUrl,
|
|
112
|
+
grant_type: oauthGrantType,
|
|
113
|
+
client_id: oauthClientId,
|
|
114
|
+
client_secret: oauthClientSecret,
|
|
115
|
+
scope: oauthScope,
|
|
116
|
+
'refresh_token': oauthRefreshToken
|
|
117
|
+
};
|
|
118
|
+
} else {
|
|
119
|
+
oauthConfig = {
|
|
120
|
+
url: oauthUrl,
|
|
121
|
+
grant_type: 'client_credentials',
|
|
122
|
+
client_id: oauthClientId,
|
|
123
|
+
client_secret: oauthClientSecret,
|
|
124
|
+
scope: oauthScope
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
//@ts-ignore
|
|
129
|
+
const client = clientFactory(axios.create(), oauthConfig);
|
|
130
|
+
const auth1 = await client();
|
|
131
|
+
sails.log.verbose(JSON.stringify(auth1));
|
|
132
|
+
sails.log.verbose('================================================');
|
|
133
|
+
|
|
134
|
+
_.set(axiosOptions, 'headers.Authorization', 'Bearer '+ _.get(auth1, 'access_token'));
|
|
135
|
+
}
|
|
136
|
+
|
|
75
137
|
snResponse = await axios(axiosOptions);
|
|
138
|
+
|
|
76
139
|
} catch (err) {
|
|
77
140
|
sails.log.error(`ServiceNowCatalog failed to submit to request for workspace OID: ${oid}, error is:`);
|
|
78
141
|
sails.log.error(JSON.stringify(err));
|
|
@@ -107,9 +170,10 @@ export module Services {
|
|
|
107
170
|
|
|
108
171
|
protected runDataRemap(source: any, target: any, fields: any[]) {
|
|
109
172
|
for (const fieldDef of fields) {
|
|
110
|
-
const source_field = fieldDef
|
|
111
|
-
const dest_field = fieldDef
|
|
112
|
-
const dest_template = fieldDef
|
|
173
|
+
const source_field = _.get(fieldDef,'source_field');
|
|
174
|
+
const dest_field = _.get(fieldDef,'dest_field');
|
|
175
|
+
const dest_template = _.get(fieldDef,'dest_template');
|
|
176
|
+
let parseObject = _.get(fieldDef, 'parseObject', false);
|
|
113
177
|
let src_data = null;
|
|
114
178
|
if (!_.isEmpty(source_field)) {
|
|
115
179
|
src_data = _.get(source, source_field);
|
|
@@ -121,7 +185,12 @@ export module Services {
|
|
|
121
185
|
src_data = template();
|
|
122
186
|
}
|
|
123
187
|
if (!_.isEmpty(dest_field)) {
|
|
124
|
-
|
|
188
|
+
if(parseObject) {
|
|
189
|
+
let obj = JSON.parse(src_data);
|
|
190
|
+
_.set(target, dest_field, obj);
|
|
191
|
+
} else {
|
|
192
|
+
_.set(target, dest_field, src_data);
|
|
193
|
+
}
|
|
125
194
|
}
|
|
126
195
|
}
|
|
127
196
|
return target;
|
package/api/Config.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
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;
|
|
@@ -1,211 +0,0 @@
|
|
|
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();
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Services = void 0;
|
|
13
|
-
const numeral = require("numeral");
|
|
14
|
-
const moment = require("moment");
|
|
15
|
-
const axios_1 = require("axios");
|
|
16
|
-
const util = require('util');
|
|
17
|
-
const redbox_core_types_1 = require("@researchdatabox/redbox-core-types");
|
|
18
|
-
var Services;
|
|
19
|
-
(function (Services) {
|
|
20
|
-
class ServicenowCatalogService extends redbox_core_types_1.Services.Core.Service {
|
|
21
|
-
constructor() {
|
|
22
|
-
super(...arguments);
|
|
23
|
-
this._exportedMethods = [
|
|
24
|
-
'submitRequest',
|
|
25
|
-
];
|
|
26
|
-
}
|
|
27
|
-
submitRequest(oid, workspaceData, options, user, response) {
|
|
28
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
sails.log.verbose(`ServiceNowCatalog processing request for: ${oid}`);
|
|
30
|
-
sails.log.verbose("Global config:");
|
|
31
|
-
sails.log.verbose(JSON.stringify(sails.config.servicenow));
|
|
32
|
-
sails.log.verbose("Hook config:");
|
|
33
|
-
sails.log.verbose(JSON.stringify(options));
|
|
34
|
-
let catalogUrl = this.getConfig('catalog.axios.url', options);
|
|
35
|
-
if (_.isEmpty(catalogUrl)) {
|
|
36
|
-
const errMsg = "Missing ServiceNow Catalog URL configuration.";
|
|
37
|
-
sails.log.error(errMsg);
|
|
38
|
-
response.code = "500";
|
|
39
|
-
response.status = false;
|
|
40
|
-
response.success = false;
|
|
41
|
-
response.message = errMsg;
|
|
42
|
-
return response;
|
|
43
|
-
}
|
|
44
|
-
sails.log.verbose(`ServiceNowCatalog adding workspace to record...`);
|
|
45
|
-
try {
|
|
46
|
-
response = yield WorkspaceService.addWorkspaceToRecord(workspaceData.metadata.rdmpOid, oid);
|
|
47
|
-
}
|
|
48
|
-
catch (err) {
|
|
49
|
-
sails.log.error(`ServiceNowCatalog failed to associate workspace to record: ${workspaceData.metadata.rdmpOid}`);
|
|
50
|
-
sails.log.error(JSON.stringify(err));
|
|
51
|
-
return response;
|
|
52
|
-
}
|
|
53
|
-
let body_template = this.getConfig('catalog.body_template', options);
|
|
54
|
-
sails.log.verbose(`ServiceNowCatalog body template:`);
|
|
55
|
-
sails.log.verbose(JSON.stringify(body_template));
|
|
56
|
-
const rdmpData = yield RecordsService.getMeta(workspaceData.metadata.rdmpOid).toPromise();
|
|
57
|
-
const catalogFields = this.getConfig('catalog.fields', options);
|
|
58
|
-
const outgoingDataSource = { workspace: workspaceData, rdmp: rdmpData, oid: oid, moment: moment, numeral: numeral, translationService: TranslationService };
|
|
59
|
-
body_template = this.runDataRemap(outgoingDataSource, body_template, catalogFields);
|
|
60
|
-
sails.log.verbose(`ServiceNowCatalog sending request via: ${catalogUrl}, options:`);
|
|
61
|
-
const axiosOptions = this.getConfig('catalog.axios', options);
|
|
62
|
-
axiosOptions.data = body_template;
|
|
63
|
-
sails.log.verbose(JSON.stringify(axiosOptions));
|
|
64
|
-
let snResponse = null;
|
|
65
|
-
try {
|
|
66
|
-
snResponse = yield (0, axios_1.default)(axiosOptions);
|
|
67
|
-
}
|
|
68
|
-
catch (err) {
|
|
69
|
-
sails.log.error(`ServiceNowCatalog failed to submit to request for workspace OID: ${oid}, error is:`);
|
|
70
|
-
sails.log.error(JSON.stringify(err));
|
|
71
|
-
response.code = `${err.code}`;
|
|
72
|
-
response.status = false;
|
|
73
|
-
response.success = false;
|
|
74
|
-
response.message = `ServiceNowCatalog failed to submit to request for workspace OID: ${oid}, check server logs.`;
|
|
75
|
-
}
|
|
76
|
-
if (!_.isNull(snResponse)) {
|
|
77
|
-
if (snResponse.status == 200) {
|
|
78
|
-
const updateSource = snResponse.data;
|
|
79
|
-
workspaceData = this.runDataRemap(updateSource, workspaceData, this.getConfig('catalog.update_fields', options));
|
|
80
|
-
yield RecordsService.updateMeta(null, oid, workspaceData).toPromise();
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
sails.log.error(`ServiceNowCatalog submit request failed for workspace OID: ${oid}, check server logs.`);
|
|
84
|
-
sails.log.error(JSON.stringify(snResponse));
|
|
85
|
-
response.code = `${snResponse.status}`;
|
|
86
|
-
response.status = false;
|
|
87
|
-
response.success = false;
|
|
88
|
-
response.message = `ServiceNowCatalog failed to submit to request for workspace OID: ${oid}, check server logs.`;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
sails.log.verbose(`ServiceNowCatalog completed request for ${oid}`);
|
|
92
|
-
return response;
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
getConfig(fieldPath, options) {
|
|
96
|
-
return _.get(options, fieldPath, _.get(sails.config.servicenow, fieldPath));
|
|
97
|
-
}
|
|
98
|
-
runDataRemap(source, target, fields) {
|
|
99
|
-
for (const fieldDef of fields) {
|
|
100
|
-
const source_field = fieldDef.source_field;
|
|
101
|
-
const dest_field = fieldDef.dest_field;
|
|
102
|
-
const dest_template = fieldDef.dest_template;
|
|
103
|
-
let src_data = null;
|
|
104
|
-
if (!_.isEmpty(source_field)) {
|
|
105
|
-
src_data = _.get(source, source_field);
|
|
106
|
-
}
|
|
107
|
-
if (!_.isEmpty(dest_template)) {
|
|
108
|
-
const imports = _.extend({ data: src_data, config: fieldDef, moment: moment, numeral: numeral, translationService: TranslationService }, source);
|
|
109
|
-
const templateData = { imports: imports };
|
|
110
|
-
const template = _.template(dest_template, templateData);
|
|
111
|
-
src_data = template();
|
|
112
|
-
}
|
|
113
|
-
if (!_.isEmpty(dest_field)) {
|
|
114
|
-
_.set(target, dest_field, src_data);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return target;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
Services.ServicenowCatalogService = ServicenowCatalogService;
|
|
121
|
-
})(Services = exports.Services || (exports.Services = {}));
|
|
122
|
-
module.exports = new Services.ServicenowCatalogService().exports();
|