--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+const util = require('../../lib/otf-util');\r
+const request = require('request');\r
+const error = require('@feathersjs/errors');\r
+module.exports = function (options = {}) { // eslint-disable-line no-unused-vars\r
+ return async context => {\r
+\r
+ //Get test-definition to compare\r
+ let original = null;\r
+ await context.app.services[context.app.get('base-path') + 'test-definitions'].get(context.id, context.params).then(result => {\r
+ original = result;\r
+ });\r
+\r
+ //If there is a bpmn instance that is deployed and is no longer there, delete with service api\r
+ if (context.data.bpmnInstances && original) {\r
+ original.bpmnInstances.forEach(async (elem, val) => {\r
+ let found = false;\r
+ context.data.bpmnInstances.forEach((e, v) => {\r
+ if (elem.version == e.version) {\r
+ found = true;\r
+ }\r
+ });\r
+ if (!found && elem.isDeployed) {\r
+ let options = {\r
+ url: context.app.get('serviceApi').url + 'testStrategy/delete/v1/deploymentId/' + elem.deploymentId,\r
+ headers: {\r
+ 'Authorization': 'Basic ' + util.base64Encode(context.app.get('serviceApi').aafId + ':' + context.app.get('serviceApi').aafPassword)\r
+ },\r
+ rejectUnauthorized: false,\r
+ }\r
+ \r
+ await new Promise((resolve, reject) => {\r
+ request.delete(options, (err, res, body) => {\r
+ if(err){\r
+ reject(err);\r
+ }\r
+ if(res && res.statusCode == 200){\r
+ resolve(res);\r
+ }else{\r
+ reject(res);\r
+ }\r
+ });\r
+ }).then(result => {\r
+ if(result.statusCode != 200){\r
+ context.error = new error(result.statusCode);\r
+ return Promise.reject(context.error);\r
+ }\r
+ }).catch(err => {\r
+ \r
+ });\r
+ }\r
+ });\r
+ }\r
+ };\r
+};\r