forked from RedHatInsights/curiosity-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatformTransformers.js
More file actions
243 lines (210 loc) · 8.8 KB
/
Copy pathplatformTransformers.js
File metadata and controls
243 lines (210 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import moment from 'moment';
import _snakeCase from 'lodash/snakeCase';
import { rbacConfig } from '../../config';
import {
platformConstants,
PLATFORM_API_EXPORT_STATUS_TYPES,
PLATFORM_API_RESPONSE_USER_PERMISSION_OPERATION_TYPES as OPERATION_TYPES,
PLATFORM_API_RESPONSE_USER_PERMISSION_RESOURCE_TYPES as RESOURCE_TYPES
} from './platformConstants';
import { helpers, dateHelpers } from '../../common';
import { platformHelpers } from './platformHelpers';
/**
* Transform export responses. Combines multiple exports, or a single export,
* into the same response format.
*
* @memberof Platform
* @module PlatformTransformers
*/
/**
* Parse platform export response.
*
* @param {object} response
* @returns {object}
*/
const exports = response => {
const updatedResponse = { data: {}, meta: {} };
const {
[platformConstants.PLATFORM_API_EXPORT_RESPONSE_DATA]: data,
[platformConstants.PLATFORM_API_EXPORT_RESPONSE_TYPES.FORMAT]: format,
[platformConstants.PLATFORM_API_EXPORT_RESPONSE_TYPES.ID]: id,
[platformConstants.PLATFORM_API_EXPORT_RESPONSE_TYPES.NAME]: name,
[platformConstants.PLATFORM_API_EXPORT_RESPONSE_TYPES.STATUS]: status
} = response || {};
updatedResponse.data.isAnything = false;
updatedResponse.data.isAnythingPending = false;
updatedResponse.data.isAnythingCompleted = false;
updatedResponse.data.isAnythingFailed = false;
updatedResponse.data.isPending = false;
updatedResponse.data.isCompleted = false;
updatedResponse.data.isFailed = false;
updatedResponse.data.pending ??= [];
updatedResponse.data.completed ??= [];
updatedResponse.data.failed ??= [];
updatedResponse.data.products = {};
/**
* Restructures export response to allow for product identifiers.
*
* @param {object} params
* @param {string} params.exportName
* @param {string} params.exportStatus
* @param {string} params.exportFormat
* @param {string} params.exportId
*/
const restructureResponse = ({ exportName, exportStatus, exportFormat, exportId } = {}) => {
const productId = platformHelpers.getExportProductId(exportName);
const focusedStatus = platformHelpers.getExportStatus(exportStatus);
const updatedExportData = {
fileName: `${moment.utc(dateHelpers.getCurrentDate()).format('YYYYMMDD_HHmmss')}_${exportFormat}_${helpers.CONFIG_EXPORT_FILENAME.replace('{0}', _snakeCase(productId))}`,
format: exportFormat,
id: exportId,
name: exportName,
productId,
status: focusedStatus
};
updatedResponse.data.products[productId] ??= {};
updatedResponse.data.products[productId].pending ??= [];
updatedResponse.data.products[productId].completed ??= [];
updatedResponse.data.products[productId].failed ??= [];
switch (focusedStatus) {
case PLATFORM_API_EXPORT_STATUS_TYPES.PENDING:
updatedResponse.data.pending.push(updatedExportData);
updatedResponse.data.products[productId].pending.push(updatedExportData);
break;
case PLATFORM_API_EXPORT_STATUS_TYPES.COMPLETE:
updatedResponse.data.completed.push(updatedExportData);
updatedResponse.data.products[productId].completed.push(updatedExportData);
break;
case PLATFORM_API_EXPORT_STATUS_TYPES.FAILED:
updatedResponse.data.failed.push(updatedExportData);
updatedResponse.data.products[productId].failed.push(updatedExportData);
break;
}
};
if (Array.isArray(data)) {
data
.filter(({ [platformConstants.PLATFORM_API_EXPORT_RESPONSE_TYPES.NAME]: exportName }) =>
new RegExp(`^${helpers.CONFIG_EXPORT_SERVICE_NAME_PREFIX}`, 'i').test(exportName)
)
.forEach(
({
[platformConstants.PLATFORM_API_EXPORT_RESPONSE_TYPES.FORMAT]: exportFormat,
[platformConstants.PLATFORM_API_EXPORT_RESPONSE_TYPES.ID]: exportId,
[platformConstants.PLATFORM_API_EXPORT_RESPONSE_TYPES.NAME]: exportName,
[platformConstants.PLATFORM_API_EXPORT_RESPONSE_TYPES.STATUS]: exportStatus
}) => {
restructureResponse({ exportName, exportStatus, exportFormat, exportId });
}
);
} else if (id && status && new RegExp(`^${helpers.CONFIG_EXPORT_SERVICE_NAME_PREFIX}`, 'i').test(name)) {
restructureResponse({ exportName: name, exportStatus: status, exportFormat: format, exportId: id });
}
updatedResponse.data.isAnythingPending = updatedResponse.data.pending.length > 0;
updatedResponse.data.isAnythingCompleted = updatedResponse.data.completed.length > 0;
updatedResponse.data.isAnythingFailed = updatedResponse.data.failed.length > 0;
updatedResponse.data.isAnything =
updatedResponse.data.isAnythingPending ||
updatedResponse.data.isAnythingCompleted ||
updatedResponse.data.isAnythingFailed;
if (updatedResponse.data.isAnything === false) {
return updatedResponse;
}
Object.entries(updatedResponse.data.products).forEach(([productId, { pending, completed, failed } = {}]) => {
// Anything pending still
updatedResponse.data.products[productId].isPending = pending.length > 0;
// Only consider the entire product line as `complete` if there are no `pending` exports.
updatedResponse.data.products[productId].isCompleted =
(completed.length > 0 && !updatedResponse.data.products[productId].isPending) || false;
// Only consider the entire product line as `failed` if there are no `pending` or `completed` exports.
updatedResponse.data.products[productId].isFailed =
(failed.length > 0 &&
!updatedResponse.data.products[productId].isPending &&
!updatedResponse.data.products[productId].isCompleted) ||
false;
});
// Anything pending still
updatedResponse.data.isPending = updatedResponse.data.pending.length > 0;
// Only consider the entire line as `complete` if there are no `pending` exports.
updatedResponse.data.isCompleted =
(updatedResponse.data.completed.length > 0 && !updatedResponse.data.isPending) || false;
// Only consider the entire line as `failed` if there are no `pending` or `completed` exports.
updatedResponse.data.isFailed =
(updatedResponse.data.failed.length > 0 && !updatedResponse.data.isPending && !updatedResponse.data.isCompleted) ||
false;
return updatedResponse;
};
/**
* Parse platform getUser response.
*
* @param {object} response
* @returns {object}
*/
const user = response => {
const updatedResponse = {};
const {
[platformConstants.PLATFORM_API_RESPONSE_USER_IDENTITY]: identity = {},
[platformConstants.PLATFORM_API_RESPONSE_USER_ENTITLEMENTS]: entitlements = {}
} = response || {};
updatedResponse.orgId = identity?.[platformConstants.PLATFORM_API_RESPONSE_USER_IDENTITY_TYPES.ORG_ID];
updatedResponse.isAdmin =
identity?.[platformConstants.PLATFORM_API_RESPONSE_USER_IDENTITY_TYPES.USER]?.[
platformConstants.PLATFORM_API_RESPONSE_USER_IDENTITY_USER_TYPES.ORG_ADMIN
] || false;
updatedResponse.isEntitled =
entitlements?.[helpers.UI_NAME]?.[platformConstants.PLATFORM_API_RESPONSE_USER_ENTITLEMENTS_APP_TYPES.ENTITLED] ||
false;
return updatedResponse;
};
/**
* Parse platform getUserPermissions response.
*
* @param {object} response
* @param {object} options
* @param {object} options.config Pass in a configuration object, RBAC
* @returns {object}
*/
const userPermissions = (response, { config = rbacConfig } = {}) => {
const updatedResponse = {
permissions: {},
authorized: {}
};
response?.forEach(
({
[platformConstants.PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES.PERMISSION]: permission,
[platformConstants.PLATFORM_API_RESPONSE_USER_PERMISSION_TYPES.RESOURCE_DEFS]: definitions = []
} = {}) => {
const [app = '', resource, operation] = permission?.split(':') || [];
if (!updatedResponse.permissions[app]) {
updatedResponse.permissions[app] = {
all: false,
resources: {}
};
}
if (resource === RESOURCE_TYPES.ALL && operation === OPERATION_TYPES.ALL) {
updatedResponse.permissions[app].all = true;
}
if (resource) {
updatedResponse.permissions[app].resources[resource] ??= {};
if (operation) {
updatedResponse.permissions[app].resources[resource][operation] = definitions;
}
}
}
);
// Alias specific app permissions checks
Object.entries(config).forEach(([key, { permissions: resourcePermissions }]) => {
updatedResponse.authorized[key] = updatedResponse.permissions[key]?.all || false;
resourcePermissions.forEach(({ resource: res, operation: op }) => {
if (updatedResponse.permissions[key]?.resources?.[res]?.[op]) {
updatedResponse.authorized[key] = true;
}
});
});
return updatedResponse;
};
const platformTransformers = {
exports,
user,
permissions: userPermissions
};
export { platformTransformers as default, platformTransformers, exports, user, userPermissions };