multicloud_rule_manager 1.1.67 → 1.1.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/commands/retrieveproduct.js +59 -32
- package/package.json +1 -1
|
@@ -7,14 +7,14 @@ export default {
|
|
|
7
7
|
describe: "Recupera struttura prodotto e genera Excel",
|
|
8
8
|
handler: async (argv) => {
|
|
9
9
|
try {
|
|
10
|
-
|
|
10
|
+
console.log("🔹 Retrieve in corso..");
|
|
11
11
|
console.log("🔹 Recupero credenziali per alias:", argv.alias);
|
|
12
12
|
const cred = getAlias(argv.alias);
|
|
13
13
|
|
|
14
14
|
// --- PRIMA CHIAMATA: token ---
|
|
15
15
|
console.log("🔹 Inizio richiesta token...");
|
|
16
16
|
const loginResp = await fetch(`https://login-eon-no-prod.bit2win.cloud/auth/realms/${cred.realm}/protocol/openid-connect/token`, {
|
|
17
|
-
|
|
17
|
+
method: "POST",
|
|
18
18
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
19
19
|
body: new URLSearchParams({
|
|
20
20
|
client_id: cred.clientId,
|
|
@@ -35,14 +35,14 @@ export default {
|
|
|
35
35
|
|
|
36
36
|
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
|
|
39
39
|
// =========================
|
|
40
40
|
// 📦 PRODUCTS
|
|
41
41
|
// =========================
|
|
42
|
-
|
|
42
|
+
console.log(" Chiamo products");
|
|
43
43
|
const productsResp = await fetch(
|
|
44
44
|
`https://${cred.realm}.bit2win.cloud/api/data/v1/products/`,
|
|
45
|
-
|
|
45
|
+
{
|
|
46
46
|
method: "GET",
|
|
47
47
|
headers: {
|
|
48
48
|
"Authorization": `Bearer ${access_token}`,
|
|
@@ -51,8 +51,7 @@ export default {
|
|
|
51
51
|
redirect: "manual"
|
|
52
52
|
}
|
|
53
53
|
);
|
|
54
|
-
|
|
55
|
-
const products = await productsResp.json();
|
|
54
|
+
const products = await safeJson(productsResp, "products");
|
|
56
55
|
|
|
57
56
|
const product = products.find(p => p.name === argv.productName);
|
|
58
57
|
|
|
@@ -62,14 +61,18 @@ export default {
|
|
|
62
61
|
|
|
63
62
|
const rootGuid = product.guid;
|
|
64
63
|
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
65
67
|
// =========================
|
|
66
68
|
// 🌳 STRUCTURE
|
|
67
69
|
// =========================
|
|
68
|
-
|
|
69
|
-
|
|
70
|
+
|
|
71
|
+
console.log("Chiamo products structure");
|
|
72
|
+
|
|
70
73
|
const structResp = await fetch(
|
|
71
74
|
`https://${cred.realm}.bit2win.cloud/api/data/v1/products_structure/`,
|
|
72
|
-
|
|
75
|
+
{
|
|
73
76
|
method: "GET",
|
|
74
77
|
headers: {
|
|
75
78
|
"Authorization": `Bearer ${access_token}`,
|
|
@@ -79,7 +82,7 @@ export default {
|
|
|
79
82
|
}
|
|
80
83
|
);
|
|
81
84
|
|
|
82
|
-
const structure = await structResp
|
|
85
|
+
const structure = await safeJson(structResp, "products_structure");
|
|
83
86
|
|
|
84
87
|
// =========================
|
|
85
88
|
// 📂 HELPERS
|
|
@@ -102,46 +105,47 @@ export default {
|
|
|
102
105
|
// =========================
|
|
103
106
|
// 📡 FAMILIES
|
|
104
107
|
// =========================
|
|
105
|
-
|
|
106
|
-
|
|
108
|
+
|
|
109
|
+
console.log("Chiamo families");
|
|
110
|
+
|
|
107
111
|
const getFamilies = async (productId) => {
|
|
108
112
|
const resp = await fetch(
|
|
109
113
|
`https://${cred.realm}.bit2win.cloud/api/catalog/v1/products/${productId}/families`,
|
|
110
114
|
{
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
115
|
+
method: "GET",
|
|
116
|
+
headers: {
|
|
117
|
+
"Authorization": `Bearer ${access_token}`,
|
|
118
|
+
"Accept": "application/json"
|
|
119
|
+
},
|
|
120
|
+
redirect: "manual"
|
|
121
|
+
}
|
|
122
|
+
);
|
|
119
123
|
|
|
120
|
-
const data = await resp
|
|
124
|
+
const data = await safeJson(resp, `families:${productId}`);
|
|
121
125
|
|
|
122
|
-
return
|
|
123
|
-
(
|
|
126
|
+
return (data || [])
|
|
127
|
+
.sort((a, b) => a.name.localeCompare(b.name))
|
|
128
|
+
.map(f => ({
|
|
124
129
|
name: f.name,
|
|
125
130
|
visible: f.visible,
|
|
126
131
|
visible_web: f.visible_web,
|
|
127
|
-
attributes:
|
|
128
|
-
|
|
132
|
+
attributes: (f.attributes || [])
|
|
133
|
+
.sort((a, b) => a.name.localeCompare(b.name))
|
|
134
|
+
.map(a => ({
|
|
129
135
|
name: a.name,
|
|
130
136
|
value_default: a.value_default,
|
|
131
137
|
read_only: a.read_only,
|
|
132
138
|
required: a.required,
|
|
133
139
|
visible: a.visible,
|
|
134
|
-
domains:
|
|
135
|
-
(a.
|
|
140
|
+
domains: (a.domains || [])
|
|
141
|
+
.sort((a, b) => (a.value || "").localeCompare(b.value || ""))
|
|
142
|
+
.map(d => ({
|
|
136
143
|
value: d.value,
|
|
137
144
|
visible: d.visible,
|
|
138
145
|
default: d.default
|
|
139
146
|
}))
|
|
140
|
-
)
|
|
141
147
|
}))
|
|
142
|
-
|
|
143
|
-
}))
|
|
144
|
-
);
|
|
148
|
+
}));
|
|
145
149
|
};
|
|
146
150
|
|
|
147
151
|
// =========================
|
|
@@ -237,5 +241,28 @@ export default {
|
|
|
237
241
|
} catch (err) {
|
|
238
242
|
console.error("❌ Errore:", err.message);
|
|
239
243
|
}
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
async function safeJson(resp, label) {
|
|
248
|
+
const text = await resp.text();
|
|
249
|
+
console.log("RAW products:", text);
|
|
250
|
+
|
|
251
|
+
if (!resp.ok) {
|
|
252
|
+
throw new Error(`${label} HTTP ${resp.status}: ${text.slice(0, 200)}`);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (!text) {
|
|
256
|
+
throw new Error(`${label} risposta vuota`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
try {
|
|
260
|
+
return JSON.parse(text);
|
|
261
|
+
} catch (e) {
|
|
262
|
+
throw new Error(`${label} NON JSON valido: ${text.slice(0, 200)}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
|
|
240
267
|
}
|
|
241
268
|
};
|