added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / services / bpmn-validate / bpmn-validate.class.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \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
25 \r
26 class Service {\r
27         constructor (options) {\r
28                 this.options = options || {};\r
29         }\r
30 \r
31         async find (params) {\r
32                 return [];\r
33         }\r
34 \r
35         // Check process definition key to see if unique\r
36         async get (id, params) {\r
37                 let errors = {};\r
38                 // Get List of Definition keys from Camunda\r
39                 let options = {\r
40                         url: this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,\r
41                         headers: {\r
42                                 'Authorization': 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')\r
43                         },\r
44                         rejectUnauthorized: false\r
45                 };\r
46 \r
47                 return await new Promise(async (resolve, reject) => {\r
48                         request.get(options, (err, response, body) => {\r
49                                 if(err){\r
50                                         reject(err);\r
51                                 }\r
52                                 resolve(response);\r
53                         });\r
54                 })\r
55                 .then(\r
56                         result => {\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
62                                                 res => {\r
63                                                         if(res.length > 0){\r
64                                                                 return new Response(200, {}, res);\r
65                                                         }else{\r
66                                                                 let resp = new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}});\r
67                                                                 return resp;\r
68                                                         }\r
69                                                 }\r
70                                         );\r
71                                 }else{\r
72                                         return new Response(200, {});\r
73                                 }\r
74                         }\r
75                 ).catch(err => {\r
76                         return new Response(400, {});\r
77                 });\r
78 \r
79                 // return await axios.get(\r
80                 //      this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,\r
81                 //      {\r
82                 //              headers: {\r
83                 //                      Authorization: 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')\r
84                 //              }\r
85                 //      })\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
93                 //                              res => {\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
98                 //                                      }else{\r
99 \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
103                 //                                              return resp;\r
104                 //                                      }\r
105                 //                              }\r
106                 //                      );\r
107                 //              }else{\r
108                 //                      console.log('not 200')\r
109                 //                      return new Response(400, {}, {errors: errors});\r
110                 //              }\r
111                 //      })\r
112                 //      .catch(err => {\r
113                 //              return new Response(200, {});\r
114                 //      });\r
115         }\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
120 \r
121         //      // let options = {\r
122         //      //      url: this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,\r
123         //      //      headers: {\r
124         //      //              'Authorization': 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')\r
125         //      //      },\r
126         //      //      rejectUnauthorized: false\r
127         //      // }\r
128 \r
129         //      // return await new Promise((resolve, reject) => {\r
130         //      //      request.post(options, (err, res, body) => {\r
131         //      //              if(err){\r
132         //      //                      reject(err);\r
133         //      //              }\r
134         //      //              resolve(res);\r
135         //      //      });\r
136         //      // }).then(\r
137         //      //      result => {\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
144         //      //                              res => {\r
145         //      //                                      return new Response(200, {}, res);\r
146         //      //                              },\r
147         //      //                              err => {\r
148         //      //                                      return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})\r
149         //      //                              }\r
150         //      //                      );\r
151         //      //              }else if(result.statusCode == 404){\r
152         //      //                      return new Response(400, {}, {errors: errors});\r
153         //      //              }else{\r
154         //      //                      return new Response(result.statusCode, {}, {errors: errors});\r
155         //      //              }\r
156         //      //      }\r
157         //      // ).catch(\r
158         //      //      err => {\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
166         //      //                      res => {\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
169         //      //                      },\r
170         //      //                      err => {\r
171         //      //                              return new Response(400, {}, {errors: {processDefinitionKey: 'You do not have access to this process definition key'}})\r
172         //      //                      }\r
173         //      //              );\r
174         //      //      }\r
175         //      // );\r
176 \r
177         //      return await axios.get(\r
178         //              this.options.app.get('camundaApi').url + 'otf/tcu/testDefinition/v1/processDefinitionKey/' + id,\r
179         //              {\r
180         //                      headers: {\r
181         //                              Authorization: 'Basic ' + new Buffer(this.options.app.get('serviceApi').aafId + ':' + this.options.app.get('serviceApi').aafPassword).toString('base64')\r
182         //                      }\r
183         //              })\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
192         //                                      res => {\r
193         //                                              return new Response(200, {}, res);\r
194         //                                      }\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
198         //                              });\r
199         //                      }else if(result.status === 404){\r
200         //                              console.log('or here')\r
201         //                              return new Response(400, {}, {errors: errors});\r
202         //                      }else{\r
203         //                              return new Response(result.status, {}, {errors: errors});\r
204         //                      }\r
205         //              })\r
206         //              .catch(err => {\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
214         //                              res => {\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
217         //                              }\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
221         //                      });\r
222         //              });\r
223         // }\r
224 \r
225         async create (data, params) {\r
226                 let bpmn = new Bpmn(this.options.app, data, params);\r
227                 return await bpmn.validate();\r
228         }\r
229 \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
235                         return res;\r
236                 }\r
237                 \r
238                 let b = new Buffer(res.body.bpmnXml);\r
239                 let r = new Readable();\r
240                 r.push(b);\r
241                 r.push(null);\r
242                 //save new bpmn file and return\r
243                 let formData = {\r
244                         'file': {\r
245                                 value: r.read(),\r
246                                 options: {\r
247                                         filename: res.body.processDefinitionKey + '.bpmn'\r
248                                 }\r
249                         }\r
250                 };\r
251                 let options = {\r
252                         url: 'https://localhost/' + this.options.app.get('base-path') + 'file-transfer',\r
253                         headers: {\r
254                                 'Authorization': params.headers.Authorization,\r
255                                 'Content-Type': "multipart/form-data"\r
256                         },\r
257                         rejectUnauthorized: false,\r
258                         formData: formData\r
259                 }\r
260 \r
261                 return await new Promise((resolve, reject) => {\r
262                         request.post(options, (err, res, body) => {\r
263                                 if(err){\r
264                                         reject(err);\r
265                                 }\r
266                                 resolve(body);\r
267                         });\r
268                 }).then(\r
269                         result => {\r
270                                 return result;\r
271                         }\r
272                 ).catch(\r
273                         err => {\r
274                                 return err;\r
275                         }\r
276                 );\r
277 \r
278         }\r
279 \r
280         async patch (id, data, params) {\r
281                 return data;\r
282         }\r
283 \r
284         async remove (id, params) {\r
285                 return { id };\r
286         }\r
287 \r
288         async parseAndUpload (data, params, method) {\r
289 \r
290         }\r
291 }\r
292 \r
293 module.exports = function (options) {\r
294         return new Service(options);\r
295 };\r
296 \r
297 module.exports.Service = Service;\r