[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / processes / SoftwareProductProcessesActionHelper.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 {actionTypes} from './SoftwareProductProcessesConstants.js';
17 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
18 import Configuration from 'sdc-app/config/Configuration.js';
19
20 function baseUrl(vspId, version) {
21         let {id: versionId} = version;
22         const restPrefix = Configuration.get('restPrefix');
23         return `${restPrefix}/v1.0/vendor-software-products/${vspId}/versions/${versionId}/processes`;
24 }
25
26 function putProcess(softwareProductId, version, process) {
27         return RestAPIUtil.put(`${baseUrl(softwareProductId, version)}/${process.id}`, {
28                 name: process.name,
29                 description: process.description,
30                 type: process.type === '' ? null : process.type
31         });
32 }
33
34 function postProcess(softwareProductId, version, process) {
35         return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, {
36                 name: process.name,
37                 description: process.description,
38                 type: process.type === '' ? null : process.type
39         });
40 }
41
42 function deleteProcess(softwareProductId, version, processId) {
43         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, version)}/${processId}`);
44 }
45
46 function uploadFileToProcess(softwareProductId, version, processId, formData)
47 {
48         return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}/${processId}/upload`, formData);
49 }
50
51 function fetchProcesses(softwareProductId, version) {
52         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
53 }
54
55 const SoftwareProductActionHelper = {
56
57         fetchProcessesList(dispatch, {softwareProductId, version}) {
58
59                 dispatch({
60                         type: actionTypes.FETCH_SOFTWARE_PRODUCT_PROCESSES,
61                         processesList: []
62                 });
63
64                 return fetchProcesses(softwareProductId, version).then(response => {
65                         dispatch({
66                                 type: actionTypes.FETCH_SOFTWARE_PRODUCT_PROCESSES,
67                                 processesList: response.results
68                         });
69                 });
70         },
71         openEditor(dispatch, process = {}) {
72                 dispatch({
73                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_EDITOR_OPEN,
74                         process
75                 });
76         },
77
78         deleteProcess(dispatch, {process, softwareProductId, version}) {
79                 return deleteProcess(softwareProductId, version, process.id).then(() => {
80                         dispatch({
81                                 type: actionTypes.DELETE_SOFTWARE_PRODUCT_PROCESS,
82                                 processId: process.id
83                         });
84                 });
85
86         },
87
88         closeEditor(dispatch) {
89                 dispatch({
90                         type:actionTypes.SOFTWARE_PRODUCT_PROCESS_EDITOR_CLOSE
91                 });
92         },
93
94         saveProcess(dispatch, {softwareProductId, version, previousProcess, process}) {
95                 if (previousProcess) {
96                         return putProcess(softwareProductId, version, process).then(() => {
97                                 if (process.formData){
98                                         uploadFileToProcess(softwareProductId, version, process.id, process.formData);
99                                 }
100                                 dispatch({
101                                         type: actionTypes.EDIT_SOFTWARE_PRODUCT_PROCESS,
102                                         process
103                                 });
104                         });
105                 }
106                 else {
107                         return postProcess(softwareProductId, version, process).then(response => {
108                                 if (process.formData) {
109                                         uploadFileToProcess(softwareProductId, version, response.value, process.formData);
110                                 }
111                                 dispatch({
112                                         type: actionTypes.ADD_SOFTWARE_PRODUCT_PROCESS,
113                                         process: {
114                                                 ...process,
115                                                 id: response.value
116                                         }
117                                 });
118                         });
119                 }
120         },
121
122         hideDeleteConfirm(dispatch) {
123                 dispatch({
124                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_CONFIRM,
125                         processToDelete: false
126                 });
127         },
128
129         openDeleteProcessesConfirm(dispatch, {process} ) {
130                 dispatch({
131                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_CONFIRM,
132                         processToDelete: process
133                 });
134         }
135
136 };
137
138 export default SoftwareProductActionHelper;
139