7c72c5b892622ac17907937d8c8be085931d79eb
[sdc.git] /
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(
28         `${baseUrl(softwareProductId, version)}/${process.id}`,
29         {
30             name: process.name,
31             description: process.description,
32             type: process.type === '' ? null : process.type
33         }
34     );
35 }
36
37 function postProcess(softwareProductId, version, process) {
38     return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, {
39         name: process.name,
40         description: process.description,
41         type: process.type === '' ? null : process.type
42     });
43 }
44
45 function deleteProcess(softwareProductId, version, processId) {
46     return RestAPIUtil.destroy(
47         `${baseUrl(softwareProductId, version)}/${processId}`
48     );
49 }
50
51 function uploadFileToProcess(softwareProductId, version, processId, formData) {
52     return RestAPIUtil.post(
53         `${baseUrl(softwareProductId, version)}/${processId}/upload`,
54         formData
55     );
56 }
57
58 function fetchProcesses(softwareProductId, version) {
59     return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
60 }
61
62 const SoftwareProductActionHelper = {
63     fetchProcessesList(dispatch, { softwareProductId, version }) {
64         dispatch({
65             type: actionTypes.FETCH_SOFTWARE_PRODUCT_PROCESSES,
66             processesList: []
67         });
68
69         return fetchProcesses(softwareProductId, version).then(response => {
70             dispatch({
71                 type: actionTypes.FETCH_SOFTWARE_PRODUCT_PROCESSES,
72                 processesList: response.results
73             });
74         });
75     },
76     openEditor(dispatch, process = {}) {
77         dispatch({
78             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_EDITOR_OPEN,
79             process
80         });
81     },
82
83     deleteProcess(dispatch, { process, softwareProductId, version }) {
84         return deleteProcess(softwareProductId, version, process.id).then(
85             () => {
86                 dispatch({
87                     type: actionTypes.DELETE_SOFTWARE_PRODUCT_PROCESS,
88                     processId: process.id
89                 });
90             }
91         );
92     },
93
94     closeEditor(dispatch) {
95         dispatch({
96             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_EDITOR_CLOSE
97         });
98     },
99
100     saveProcess(
101         dispatch,
102         { softwareProductId, version, previousProcess, process }
103     ) {
104         if (previousProcess) {
105             return putProcess(softwareProductId, version, process).then(() => {
106                 if (process.formData) {
107                     uploadFileToProcess(
108                         softwareProductId,
109                         version,
110                         process.id,
111                         process.formData
112                     );
113                 }
114                 dispatch({
115                     type: actionTypes.EDIT_SOFTWARE_PRODUCT_PROCESS,
116                     process
117                 });
118             });
119         } else {
120             return postProcess(softwareProductId, version, process).then(
121                 response => {
122                     if (process.formData) {
123                         uploadFileToProcess(
124                             softwareProductId,
125                             version,
126                             response.value,
127                             process.formData
128                         );
129                     }
130                     dispatch({
131                         type: actionTypes.ADD_SOFTWARE_PRODUCT_PROCESS,
132                         process: {
133                             ...process,
134                             id: response.value
135                         }
136                     });
137                 }
138             );
139         }
140     },
141
142     hideDeleteConfirm(dispatch) {
143         dispatch({
144             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_CONFIRM,
145             processToDelete: false
146         });
147     },
148
149     openDeleteProcessesConfirm(dispatch, { process }) {
150         dispatch({
151             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_CONFIRM,
152             processToDelete: process
153         });
154     }
155 };
156
157 export default SoftwareProductActionHelper;