1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 const axios = require('axios');
\r
18 const Response = require('http-response-object');
\r
19 const logger = require('../../../lib/logger');
\r
20 const util = require('../../../lib/otf-util');
\r
21 const beautify = require('json-beautify');
\r
22 const Bpmn = require('./bpmn.class');
\r
23 const Readable = require('stream').Readable;
\r
24 const request = require('request');
\r
27 constructor (options) {
\r
28 this.options = options || {};
\r
31 async find (params) {
\r
35 // Check process definition key to see if unique
\r
36 async get (id, params) {
\r
38 // Get List of Definition keys from Camunda
\r
40 url: this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,
\r
42 'Authorization': 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')
\r
44 rejectUnauthorized: false
\r
47 return await new Promise(async (resolve, reject) => {
\r
48 request.get(options, (err, response, body) => {
\r
57 if (result.statusCode == 200) {
\r
58 //check to make sure they have access
\r
59 params.query.$limit = '-1';
\r
60 params.query.processDefinitionKey = id;
\r
61 return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(params).then(
\r
64 return new Response(200, {}, res);
\r
66 let resp = new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}});
\r
72 return new Response(200, {});
\r
76 return new Response(400, {});
\r
79 // return await axios.get(
\r
80 // this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,
\r
83 // Authorization: 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')
\r
86 // .then(result => {
\r
87 // console.log(result);
\r
88 // if (result.status === 200) {
\r
89 // //check to make sure they have access
\r
90 // params.query.$limit = '-1';
\r
91 // params.query.processDefinitionKey = id;
\r
92 // return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(params).then(
\r
94 // console.log('res 1');
\r
95 // console.log(res);
\r
96 // if(res.length > 0){
\r
97 // return new Response(200, {}, res);
\r
100 // console.log('err 1');
\r
101 // let resp = new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}});
\r
102 // console.log(resp);
\r
108 // console.log('not 200')
\r
109 // return new Response(400, {}, {errors: errors});
\r
113 // return new Response(200, {});
\r
116 // async get (id, params) {
\r
117 // console.log("bpmn-upload: get")
\r
118 // let errors = {};
\r
119 // // Get List of Definition keys from Camunda
\r
121 // // let options = {
\r
122 // // url: this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,
\r
124 // // 'Authorization': 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')
\r
126 // // rejectUnauthorized: false
\r
129 // // return await new Promise((resolve, reject) => {
\r
130 // // request.post(options, (err, res, body) => {
\r
134 // // resolve(res);
\r
138 // // console.log(result);
\r
139 // // if (result.statusCode === 200) {
\r
140 // // //check to make sure they have access
\r
141 // // params.query.$limit = '-1';
\r
142 // // params.query.processDefinitionKey = id;
\r
143 // // return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(params).then(
\r
145 // // return new Response(200, {}, res);
\r
148 // // return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})
\r
151 // // }else if(result.statusCode == 404){
\r
152 // // return new Response(400, {}, {errors: errors});
\r
154 // // return new Response(result.statusCode, {}, {errors: errors});
\r
159 // // console.log("Err: " + err)
\r
160 // // //return new Response(200, {});
\r
161 // // let newParams = Object.assign({}, params);
\r
162 // // newParams.query.$limit = -1;
\r
163 // // newParams.query.processDefinitionKey = id;
\r
164 // // //console.log(params);
\r
165 // // return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(newParams).then(
\r
167 // // //return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})
\r
168 // // return new Response(200, {}, res);
\r
171 // // return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})
\r
177 // return await axios.get(
\r
178 // this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,
\r
181 // Authorization: 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')
\r
184 // .then(result => {
\r
185 // console.log(result);
\r
186 // if (result.status === 200) {
\r
187 // console.log('in here')
\r
188 // //check to make sure they have access
\r
189 // params.query.$limit = '-1';
\r
190 // params.query.processDefinitionKey = id;
\r
191 // return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(params).then(
\r
193 // return new Response(200, {}, res);
\r
195 // ).catch(err => {
\r
196 // console.log('err')
\r
197 // return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})
\r
199 // }else if(result.status === 404){
\r
200 // console.log('or here')
\r
201 // return new Response(400, {}, {errors: errors});
\r
203 // return new Response(result.status, {}, {errors: errors});
\r
207 // console.log("Err: " + err)
\r
208 // //return new Response(200, {});
\r
209 // let newParams = Object.assign({}, params);
\r
210 // newParams.query.$limit = -1;
\r
211 // newParams.query.processDefinitionKey = id;
\r
212 // //console.log(params);
\r
213 // return this.options.app.services[this.options.app.get('base-path') + 'test-definitions'].find(newParams).then(
\r
215 // //return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})
\r
216 // return new Response(200, {}, res);
\r
218 // ).catch(err => {
\r
219 // console.log('err 2')
\r
220 // return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})
\r
225 async create (data, params) {
\r
226 let bpmn = new Bpmn(this.options.app, data, params);
\r
227 return await bpmn.validate();
\r
230 //validates then saves bpmn file and returns file meta data
\r
231 async update (id, data, params) {
\r
232 let bpmn = new Bpmn(this.options.app, data, params);
\r
233 let res = await bpmn.validate();
\r
234 if(res.statusCode != 200){
\r
238 let b = new Buffer(res.body.bpmnXml);
\r
239 let r = new Readable();
\r
242 //save new bpmn file and return
\r
247 filename: res.body.processDefinitionKey + '.bpmn'
\r
252 url: this.options.app.get('otf').url + this.options.app.get('base-path') + 'file-transfer',
\r
254 'Authorization': params.headers.Authorization,
\r
255 'Content-Type': "multipart/form-data"
\r
257 rejectUnauthorized: false,
\r
261 return await new Promise((resolve, reject) => {
\r
262 request.post(options, (err, res, body) => {
\r
280 async patch (id, data, params) {
\r
284 async remove (id, params) {
\r
288 async parseAndUpload (data, params, method) {
\r
293 module.exports = function (options) {
\r
294 return new Service(options);
\r
297 module.exports.Service = Service;
\r