[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / processes / SoftwareProductComponentProcessesActionHelper.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import {actionTypes} from './SoftwareProductComponentProcessesConstants.js';
19
20 function baseUrl(softwareProductId, version, componentId) {
21         const restPrefix = Configuration.get('restPrefix');
22         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/components/${componentId}/processes`;
23 }
24
25 function fetchProcessesList({softwareProductId, version, componentId}) {
26         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}`);
27 }
28
29 function deleteProcess({softwareProductId, version, componentId, processId}) {
30         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, version, componentId)}/${processId}`);
31 }
32
33 function putProcess({softwareProductId, version, componentId, process}) {
34         return RestAPIUtil.put(`${baseUrl(softwareProductId, version, componentId)}/${process.id}`, {
35                 name: process.name,
36                 description: process.description,
37                 type: process.type === '' ? null : process.type
38         });
39 }
40
41 function postProcess({softwareProductId, version, componentId, process}) {
42         return RestAPIUtil.post(`${baseUrl(softwareProductId, version, componentId)}`, {
43                 name: process.name,
44                 description: process.description,
45                 type: process.type === '' ? null : process.type
46         });
47 }
48
49 function uploadFileToProcess({softwareProductId, version, processId, componentId, formData}) {
50         return RestAPIUtil.post(`${baseUrl(softwareProductId, version, componentId)}/${processId}/upload`, formData);
51 }
52
53
54
55 const SoftwareProductComponentProcessesActionHelper = {
56         fetchProcessesList(dispatch, {softwareProductId, version, componentId}) {
57                 dispatch({
58                         type: actionTypes.FETCH_SOFTWARE_PRODUCT_COMPONENTS_PROCESSES,
59                         processesList: []
60                 });
61
62                 return fetchProcessesList({softwareProductId, version, componentId}).then(response => {
63                         dispatch({
64                                 type: actionTypes.FETCH_SOFTWARE_PRODUCT_COMPONENTS_PROCESSES,
65                                 processesList: response.results
66                         });
67                 });
68         },
69
70         deleteProcess(dispatch, {process, softwareProductId, version, componentId}) {
71                 return deleteProcess({softwareProductId, version, processId:process.id, componentId}).then(() => {
72                         dispatch({
73                                 type: actionTypes.DELETE_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
74                                 processId: process.id
75                         });
76                 });
77
78         },
79
80         saveProcess(dispatch, {softwareProductId, version, componentId, previousProcess, process}) {
81                 if (previousProcess) {
82                         return putProcess({softwareProductId, version, componentId,  process}).then(() => {
83                                 if (process.formData && process.formData.name !== previousProcess.artifactName){
84                                         uploadFileToProcess({softwareProductId, version, processId: process.id, formData: process.formData, componentId});
85                                 }
86                                 dispatch({
87                                         type: actionTypes.EDIT_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
88                                         process
89                                 });
90                         });
91                 }
92                 else {
93                         return postProcess({softwareProductId, version, componentId, process}).then(response => {
94                                 if (process.formData) {
95                                         uploadFileToProcess({softwareProductId, version, processId: response.value, formData: process.formData, componentId});
96                                 }
97                                 dispatch({
98                                         type: actionTypes.ADD_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
99                                         process: {
100                                                 ...process,
101                                                 id: response.value
102                                         }
103                                 });
104                         });
105                 }
106         },
107
108         hideDeleteConfirm(dispatch) {
109                 dispatch({
110                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_COMPONENTS_CONFIRM,
111                         processToDelete: false
112                 });
113         },
114
115         openDeleteProcessesConfirm(dispatch, {process} ) {
116                 dispatch({
117                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_COMPONENTS_CONFIRM,
118                         processToDelete: process
119                 });
120         },
121
122         openEditor(dispatch, process = {}) {
123                 dispatch({
124                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_COMPONENTS_EDITOR_OPEN,
125                         process
126                 });
127         },
128         closeEditor(dispatch) {
129                 dispatch({
130                         type:actionTypes.SOFTWARE_PRODUCT_PROCESS_COMPONENTS_EDITOR_CLOSE
131                 });
132         }
133 };
134
135 export default SoftwareProductComponentProcessesActionHelper;