@researchdatabox/sails-hook-redbox-servicenow-catalog 0.0.7 → 0.0.11

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.
@@ -6,7 +6,7 @@ jobs:
6
6
  working_directory: ~/repo
7
7
  steps:
8
8
  - checkout
9
- - run: npm install
9
+ - run: npm install && node_modules/.bin/tsc
10
10
  # run tests!
11
11
  # - run: npm test
12
12
  # # store test results
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();
@@ -1,21 +1,23 @@
1
1
  "use strict";
2
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); }); }
3
4
  return new (P || (P = Promise))(function (resolve, reject) {
4
5
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
6
  function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
9
  });
9
10
  };
10
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Services = void 0;
11
13
  const numeral = require("numeral");
12
14
  const moment = require("moment");
13
15
  const axios_1 = require("axios");
14
16
  const util = require('util');
15
- const services = require("../core/CoreService");
17
+ const redbox_core_types_1 = require("@researchdatabox/redbox-core-types");
16
18
  var Services;
17
19
  (function (Services) {
18
- class ServicenowCatalogService extends services.Services.Core.Service {
20
+ class ServicenowCatalogService extends redbox_core_types_1.Services.Core.Service {
19
21
  constructor() {
20
22
  super(...arguments);
21
23
  this._exportedMethods = [
@@ -51,7 +53,7 @@ var Services;
51
53
  let body_template = this.getConfig('catalog.body_template', options);
52
54
  sails.log.verbose(`ServiceNowCatalog body template:`);
53
55
  sails.log.verbose(JSON.stringify(body_template));
54
- const rdmpData = yield RecordsService.getMeta(workspaceData.metadata.rdmpOid).toPromise();
56
+ const rdmpData = yield RecordsService.getMeta(workspaceData.metadata.rdmpOid);
55
57
  const catalogFields = this.getConfig('catalog.fields', options);
56
58
  const outgoingDataSource = { workspace: workspaceData, rdmp: rdmpData, oid: oid, moment: moment, numeral: numeral, translationService: TranslationService };
57
59
  body_template = this.runDataRemap(outgoingDataSource, body_template, catalogFields);
@@ -61,7 +63,7 @@ var Services;
61
63
  sails.log.verbose(JSON.stringify(axiosOptions));
62
64
  let snResponse = null;
63
65
  try {
64
- snResponse = yield axios_1.default(axiosOptions);
66
+ snResponse = yield (0, axios_1.default)(axiosOptions);
65
67
  }
66
68
  catch (err) {
67
69
  sails.log.error(`ServiceNowCatalog failed to submit to request for workspace OID: ${oid}, error is:`);
@@ -75,7 +77,7 @@ var Services;
75
77
  if (snResponse.status == 200) {
76
78
  const updateSource = snResponse.data;
77
79
  workspaceData = this.runDataRemap(updateSource, workspaceData, this.getConfig('catalog.update_fields', options));
78
- yield RecordsService.updateMeta(null, oid, workspaceData).toPromise();
80
+ yield RecordsService.updateMeta(null, oid, workspaceData);
79
81
  }
80
82
  else {
81
83
  sails.log.error(`ServiceNowCatalog submit request failed for workspace OID: ${oid}, check server logs.`);
@@ -2,6 +2,7 @@ module.exports.recordtype = {
2
2
  "servicenow-catalog": {
3
3
  "packageType": "workspace",
4
4
  "packageName": "servicenow-catalog",
5
+ "searchable": false,
5
6
  "searchFilters": [
6
7
  {
7
8
  name: "text_title",
@@ -0,0 +1,17 @@
1
+ module.exports.routes = {
2
+ 'post /:branding/:portal/ws/catalog/rdmp': {
3
+ controller: 'CatalogController',
4
+ action: 'rdmpInfo',
5
+ csrf: false
6
+ },
7
+ 'post /:branding/:portal/ws/catalog/request': {
8
+ controller: 'CatalogController',
9
+ action: 'request',
10
+ csrf: false
11
+ },
12
+ 'get /:branding/:portal/ws/catalog/info': {
13
+ controller: 'CatalogController',
14
+ action: 'info',
15
+ csrf: false
16
+ }
17
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@researchdatabox/sails-hook-redbox-servicenow-catalog",
3
- "version": "0.0.7",
3
+ "version": "0.0.11",
4
4
  "description": "ReDBox Workspace provisioner plugin for creating ServiceNow Catalog items.",
5
5
  "main": "index.js",
6
6
  "sails": {
@@ -30,13 +30,18 @@
30
30
  "@types/node": "^10.12.18",
31
31
  "chai": "^4.2.0",
32
32
  "mocha": "^7.2.0",
33
+ "moment": "^2.29.1",
34
+ "numeral": "^2.0.6",
35
+ "rxjs": "^6.6.2",
36
+ "rxjs-compat": "^6.6.7",
33
37
  "sails": "^1.2.4",
34
38
  "supertest": "^3.0.0",
35
39
  "ts-node": "^6.2.0",
36
- "typescript": "^2.9.2",
40
+ "typescript": "^4.4.3",
37
41
  "typings": "^2.1.1"
38
42
  },
39
43
  "dependencies": {
44
+ "@researchdatabox/redbox-core-types": "^1.0.5-beta",
40
45
  "axios": "^0.20.0",
41
46
  "fs-extra": "^8.1.0",
42
47
  "lodash": "4.17.14",
@@ -46,8 +51,6 @@
46
51
  "path-exists": "^3.0.0",
47
52
  "qs": "^6.9.0",
48
53
  "request": "^2.87.0",
49
- "request-promise": "^4.2.2",
50
- "rxjs": "6.2.0",
51
- "rxjs-compat": "6.2.0"
54
+ "request-promise": "^4.2.2"
52
55
  }
53
56
  }
@@ -9,16 +9,16 @@ declare var BrandingService, WorkspaceService, CatalogService;
9
9
  /**
10
10
  * Package that contains all Controllers.
11
11
  */
12
- import controller = require('../core/CoreController');
13
- import {Config} from '../Config';
12
+ import { Controllers as controller} from '@researchdatabox/redbox-core-types';
13
+ import {Config} from '../Config';
14
14
 
15
15
  export module Controllers {
16
16
 
17
17
  /**
18
- * Omero related features....
18
+ * Service Now related features....
19
19
  *
20
20
  */
21
- export class CatalogController extends controller.Controllers.Core.Controller {
21
+ export class CatalogController extends controller.Core.Controller {
22
22
 
23
23
  protected _exportedMethods: any = [
24
24
  'info',
@@ -5,11 +5,11 @@ import * as numeral from 'numeral';
5
5
  import moment = require('moment');
6
6
  import axios from 'axios';
7
7
  const util = require('util');
8
- import services = require('../core/CoreService');
8
+ import { RecordsService, Services as services} from '@researchdatabox/redbox-core-types';
9
9
 
10
10
  declare var sails: Sails;
11
11
  declare var WorkspaceService, _;
12
- declare var RecordsService, TranslationService, WorkflowStepsService;
12
+ declare var RecordsService: RecordsService, TranslationService, WorkflowStepsService;
13
13
 
14
14
  export module Services {
15
15
 
@@ -21,7 +21,7 @@ export module Services {
21
21
  // Build date: 06-05-2020_2026
22
22
  // Build tag: glide-newyork-06-26-2019__patch9-05-27-2020
23
23
  // MID buildstamp: newyork-06-26-2019__patch9-05-27-2020_06-05-2020_2026
24
- export class ServicenowCatalogService extends services.Services.Core.Service {
24
+ export class ServicenowCatalogService extends services.Core.Service {
25
25
 
26
26
  protected _exportedMethods: any = [
27
27
  'submitRequest',
@@ -61,7 +61,7 @@ export module Services {
61
61
  sails.log.verbose(`ServiceNowCatalog body template:`);
62
62
  sails.log.verbose(JSON.stringify(body_template));
63
63
  // get the RDMP data
64
- const rdmpData = await RecordsService.getMeta(workspaceData.metadata.rdmpOid).toPromise();
64
+ const rdmpData = await RecordsService.getMeta(workspaceData.metadata.rdmpOid);
65
65
  const catalogFields = this.getConfig('catalog.fields', options);
66
66
  // prepare the request to ServiceNow
67
67
  const outgoingDataSource = {workspace: workspaceData, rdmp: rdmpData, oid:oid, moment: moment, numeral:numeral, translationService: TranslationService };
@@ -86,7 +86,7 @@ export module Services {
86
86
  const updateSource = snResponse.data;
87
87
  // perform any updates to the workspace record ...
88
88
  workspaceData = this.runDataRemap(updateSource, workspaceData, this.getConfig('catalog.update_fields', options));
89
- await RecordsService.updateMeta(null, oid, workspaceData).toPromise();
89
+ await RecordsService.updateMeta(null, oid, workspaceData);
90
90
  } else {
91
91
  sails.log.error(`ServiceNowCatalog submit request failed for workspace OID: ${oid}, check server logs.`);
92
92
  sails.log.error(JSON.stringify(snResponse));