-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
625 lines (576 loc) · 24.4 KB
/
script.js
File metadata and controls
625 lines (576 loc) · 24.4 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*By David wyatt, https://www.linkedin.com/in/wyattdave/ , under license: https://github.com/wyattdave/my-power-platform/blob/main/LICENSE*/
let sApiUrlFlow = 'https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/';
let sApiUrlApp="https://<tenantID>.tenant.api.powerplatform.com/powerapps/environments?api-version=1";
let dDate = new Date(new Date().getFullYear()+"-01-01");
let sDate= dDate.getFullYear()+"-01-01";
let dDateTo = new Date(new Date().getFullYear()+"-12-31");
let sDateTo= dDateTo.getFullYear()+"-12-31";
let sFilter="";
let bFirst=true;
let oDataAPI;
let aAllData=[];
let iAPICount=0;
let aEnvironmentsMaster=[];
let aEnvironmentData=[];
let iNonGeo=0;
let oUser;
let sCurrentGeo="";
/** Show a themed Bootstrap modal instead of a native alert */
function showAlert(message) {
document.getElementById("alertModalBody").innerHTML = message;
$("#alertModal").modal("show");
}
// All known Dynamics 365 region URL suffixes
const DYNAMICS_REGIONS = [
{ region: "USA", name: "North America", url: "crm.dynamics.com" },
{ region: "DEU", name: "Germany (Microsoft Cloud)", url: "crm.microsoftdynamics.de" },
{ region: "SAM", name: "South America", url: "crm2.dynamics.com" },
{ region: "CAN", name: "Canada", url: "crm3.dynamics.com" },
{ region: "EUR", name: "Europe", url: "crm4.dynamics.com" },
{ region: "APJ", name: "Asia Pacific", url: "crm5.dynamics.com" },
{ region: "OCE", name: "Australia", url: "crm6.dynamics.com" },
{ region: "JPN", name: "Japan", url: "crm7.dynamics.com" },
{ region: "IND", name: "India", url: "crm8.dynamics.com" },
{ region: "GCC", name: "US Government (GCC)", url: "crm9.dynamics.com" },
{ region: "GCC High", name: "US Government (GCC High)", url: "crm.microsoftdynamics.us" },
{ region: "GBR", name: "United Kingdom", url: "crm11.dynamics.com" },
{ region: "FRA", name: "France", url: "crm12.dynamics.com" },
{ region: "ZAF", name: "South Africa", url: "crm14.dynamics.com" },
{ region: "UAE", name: "United Arab Emirates", url: "crm15.dynamics.com" },
{ region: "GER", name: "Germany", url: "crm16.dynamics.com" },
{ region: "CHE", name: "Switzerland", url: "crm17.dynamics.com" },
{ region: "CHN", name: "China", url: "crm.dynamics.cn" },
{ region: "NOR", name: "Norway", url: "crm19.dynamics.com" },
{ region: "SGP", name: "Singapore", url: "crm20.dynamics.com" },
{ region: "KOR", name: "South Korea", url: "crm21.dynamics.com" }
];
/** Look up the correct Dataverse token for a given environment API URL */
function getTokenForUrl(envirUrl) {
if (!oDataAPI || !oDataAPI.regionTokens) return null;
for (const r of DYNAMICS_REGIONS) {
if (envirUrl && envirUrl.includes(r.url)) {
return oDataAPI.regionTokens[r.url] || null;
}
}
return null;
}
/** Render region badges into the #region-badges container.
* Only shows regions that have a token (green) or that appear in the environment list without a token (red). */
function renderRegionBadges(aEnvironments) {
const container = document.getElementById("region-badges");
if (!container || !oDataAPI || !oDataAPI.regionTokens) return;
const regionTokens = oDataAPI.regionTokens;
// Build a set of region URLs that have at least one environment
const envRegionUrls = new Set();
if (aEnvironments && aEnvironments.length) {
aEnvironments.forEach(envir => {
if (!envir.url) return;
for (const r of DYNAMICS_REGIONS) {
if (envir.url.includes(r.url)) { envRegionUrls.add(r.url); break; }
}
});
}
let html = "";
DYNAMICS_REGIONS.forEach(r => {
const hasToken = !!regionTokens[r.url];
const inEnvList = envRegionUrls.has(r.url);
if (!hasToken && !inEnvList) return; // hide regions with no token and no environments
const color = hasToken ? "#28a745" : "#dc3545"; // green if token, red if env-only
const icon = hasToken ? "fa-check" : "fa-xmark";
html += "<span title='"+r.name+"' style='display:inline-block;margin:2px 3px;padding:2px 7px;border-radius:10px;font-size:11px;color:#fff;background:"+color+";cursor:default'>" +
"<i class='fa-solid "+icon+"'></i> "+r.region+"</span>";
});
container.innerHTML = html;
}
const eData=document.getElementById("data");
const eDate=document.getElementById("input-date");
const eDateTo=document.getElementById("input-dateTo");
const eFilter=document.getElementById("input-filter");
const eUpdate=document.getElementById("button-update");
const eLoad=document.getElementById("sideTitle");
const eTotal=document.getElementById("span-total");
const eCount=document.getElementById("span-count");
const eDiv=document.getElementById("div-count");
const eDataDownload=document.getElementById("button-data");
const eEnvirDownload=document.getElementById("button-envir");
const eCsvDownload=document.getElementById("button-csv");
eUpdate.addEventListener("click", setDate);
eDataDownload.addEventListener("click", downloadData);
eEnvirDownload.addEventListener("click", downloadEnvironment);
eCsvDownload.addEventListener("click", downloadCsv);
eDate.value=sDate;
eDateTo.value=sDateTo;
document.addEventListener("DOMContentLoaded", () => {
if(localStorage.getItem("solutionFilter")){
eFilter.value=sFilter=localStorage.getItem("solutionFilter");
}
chrome.runtime.sendMessage({ type: "REQUEST_DATA" }, (response) => {
if (chrome.runtime.lastError) {
console.error("Message error:", chrome.runtime.lastError.message);
}
console.log(response)
if (response) {
if(response.flow!="" && response.envirs!=""){
oDataAPI=response;
renderRegionBadges();
eLoad.innerHTML="Data Loading..";
load();
} else if(localStorage.getItem("data")){
aAllData=JSON.parse(localStorage.getItem("data"));
if( localStorage.getItem("geo")){sCurrentGeo=localStorage.getItem("geo")}
aEnvironmentsMaster=JSON.parse(localStorage.getItem("environments"));
loadCharts(aAllData,aEnvironmentsMaster,false);
sDate = localStorage.getItem("date");
sDateTo = localStorage.getItem("dateTo");
eDate.value=sDate;
eDateTo.value=sDateTo;
eDiv.style.display="none";
eLoad.innerHTML="Key Data Reloaded";
showAlert("Access expired, reloaded previous data. If a new update is required please go to <a href='https://make.powerautomate.com' target='_blank'>make.powerautomate.com</a> or refresh the page.");
}else{
showAlert("Access expired, please go to <a href='https://make.powerautomate.com' target='_blank'>make.powerautomate.com</a> or refresh the page.");
}
} else {
console.error("Failed to receive data");
}
});
});
function downloadData(){
downloadJSON(aAllData,"MyPowerPlatform ("+sDate+" to "+sDateTo+").json");
}
function downloadEnvironment(){
downloadJSON(aEnvironmentData,"MyEnvironments ("+sDate+" to "+sDateTo+").json");
}
function downloadCsv(){
let aCsv=[];
aAllData.forEach(item =>{
let sDataverseId="Not solution aware";
if(item.dataverseId){sDataverseId= item.dataverseId};
aCsv.push({
displayName:item.displayName,
id:item.id,
dataverseId: sDataverseId,
created:item.createdTime,
type:item.type,
environment:item.environment.displayName,
environmentId:item.environment.id,
environmentsUrl:item.environment.url
})
})
const aHeaders=["Display_Name","Id","Dataverse_Id","Created","Type","Environment","Environment_Id","Environments_Url"];
exportCSVFile(aHeaders,aCsv,"MyPowerPlatform ("+sDate+" to "+sDateTo+")");
}
function setDate(){
sDate= eDate.value;
dDate= new Date(sDate);
sDateTo= eDateTo.value;
dDateTo= new Date(sDateTo);
eData.innerHTML="";
eLoad.innerHTML="Data Loading..";
eDiv.style.display=null;
aAllData.length=0;
iAPICount=0;
eCount.innerText=0;
console.log(aAllData);
aEnvironmentsMaster.forEach(envir =>{
getData(envir,true)
})
}
async function load(){
eDiv.style.display=null;
const aEnvironmentsAllGeos=await getEnvironments(oDataAPI.envirs,oDataAPI.url,"")
if(!aEnvironmentsAllGeos || aEnvironmentsAllGeos.length===0){
eData.innerHTML="<b style='color:red;'>No environments found. Check your access.</b><br>";
return;
}
// Find the first environment that has a region token so we can discover the user
let sDiscoveryEnvir = null;
if(oDataAPI.user){
// If we already have a captured user URL, use that environment
sDiscoveryEnvir = aEnvironmentsAllGeos.find(envir => envir.url && oDataAPI.user.includes(envir.url.replace("https://","").split("/")[0]));
}
if(!sDiscoveryEnvir){
// Otherwise pick the first environment that has a region token
sDiscoveryEnvir = aEnvironmentsAllGeos.find(envir => envir.url && getTokenForUrl(envir.url));
}
if(!sDiscoveryEnvir){
// Last resort: use the first environment with a linked URL
sDiscoveryEnvir = aEnvironmentsAllGeos.find(envir => envir.url);
}
if(!sDiscoveryEnvir){
eData.innerHTML="<b style='color:red;'>No Dataverse environments found. Visit a Dataverse environment first.</b><br>";
return;
}
// Determine geo from the discovery environment
sCurrentGeo = sDiscoveryEnvir.location;
aEnvironmentsMaster=aEnvironmentsAllGeos.filter(envir =>{return envir.location==sCurrentGeo})
iNonGeo=aEnvironmentsAllGeos.length-aEnvironmentsMaster.length;
eTotal.innerText=aEnvironmentsMaster.length;
// Re-render badges now that environments are known
renderRegionBadges(aEnvironmentsAllGeos);
// Discover user identity from the first accessible environment
const sDiscoveryToken = getTokenForUrl(sDiscoveryEnvir.url);
if(!sDiscoveryToken){
// No region token yet — try using the first available region token for the WhoAmI call
const anyToken = Object.values(oDataAPI.regionTokens || {})[0];
if(!anyToken){
eData.innerHTML="<b style='color:red;'>No Dataverse token captured yet. Visit any environment in make.powerautomate.com, then reopen.</b><br>";
return;
}
oUser = await fetchAPIData(sDiscoveryEnvir.url+"/api/data/v9.2/WhoAmI",anyToken,sDiscoveryEnvir.displayName);
} else {
oUser = await fetchAPIData(sDiscoveryEnvir.url+"/api/data/v9.2/WhoAmI",sDiscoveryToken,sDiscoveryEnvir.displayName);
}
if(!oUser || !oUser.UserId){
eData.innerHTML="<b style='color:red;'>Could not identify user. Visit an environment in make.powerautomate.com, then reopen.</b><br>";
return;
}
// Now fetch full user record using the UserId from WhoAmI
const sUserToken2 = sDiscoveryToken || Object.values(oDataAPI.regionTokens || {})[0];
const oUserFull = await fetchAPIData(sDiscoveryEnvir.url+"/api/data/v9.2/systemusers("+oUser.UserId+")",sUserToken2,sDiscoveryEnvir.displayName);
if(oUserFull){
oUser = oUserFull;
}
eData.innerHTML="Hello "+oUser.fullname+", all environments in "+sCurrentGeo+" selected<br>";
aEnvironmentsMaster.forEach(envir =>{
getData(envir,false)
})
}
async function getData(oEnvir,bDestroy){
let oWhoAmI;
localStorage.setItem("solutionFilter",eFilter.value);
sFilter=eFilter.value;
const envirToken = getTokenForUrl(oEnvir.url);
if(!envirToken){
iAPICount++;
eCount.innerText=iAPICount;
eData.innerHTML+="<i class='fa-solid fa-globe' style='color:orange' aria-hidden='true'></i> "+oEnvir.displayName+" skipped (no token for region)<br>";
if(iAPICount==aEnvironmentsMaster.length){ finalizeData(bDestroy); }
return;
}
try{
const aWhoAmI=await fetchAPIData(oEnvir.url+"/api/data/v9.2/systemusers?$filter=internalemailaddress eq '"+oUser.internalemailaddress+"'",envirToken,oEnvir.displayName);
if(aWhoAmI){
oWhoAmI=aWhoAmI.value[0].systemuserid
}else{
oWhoAmI=null;
}
if(!oWhoAmI){
iAPICount++;
eData.innerHTML+="<i class='fa-solid fa-globe' style='color:red' aria-hidden='true'></i> "+oEnvir.displayName+" failed, id: cant find<br>";
}else{
eData.innerHTML+="<i class='fa-solid fa-globe' aria-hidden='true'></i> "+oEnvir.displayName+" identified, id:"+oWhoAmI+"<br>";
////flows
const aFlows=await fetchAPIData(sApiUrlFlow+oEnvir.id+"/flows?api-version=2016-11-01",oDataAPI.flow,oEnvir.displayName )
if(aFlows){
aFlows.value.forEach(flow =>{
const dCreated=new Date(flow.properties.createdTime);
if(dCreated>=dDate && dCreated<=dDateTo){
aAllData.push(
{
type:"flow",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:flow.name,
dataverseId:flow.properties.workflowEntityId,
displayName:flow.properties.displayName,
createdTime:flow.properties.createdTime,
isManaged:flow.properties.isManaged,
trigger:flow.properties.definitionSummary.triggers[0].type,
month:dCreated.getMonth()+1
}
)
}
})
eData.innerHTML+="<i class='fa-solid fa-puzzle-piece'></i> "+oEnvir.displayName+" flows found "+aFlows.value.length+"<br>";
}
////apps
const aApps =await fetchAPIData(oEnvir.url+"/api/data/v9.2/canvasapps?$filter=_ownerid_value eq '"+oWhoAmI+"' and createdtime ge "+sDate+" and createdtime le "+sDateTo,envirToken,oEnvir.displayName);
if(aApps){
aApps.value.forEach(app =>{
aAllData.push(
{
type:"app",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:app.uniquecanvasappid,
dataverseId:app.canvasappid,
displayName:app.displayname,
createdTime:app.createdtime,
isManaged:app.ismanaged,
appUrl:app.appopenuri,
month:new Date(app.createdtime).getMonth()+1
}
)
})
eData.innerHTML+="<i class='fa-solid fa-laptop'></i> "+oEnvir.displayName+" apps found "+aApps.value.length+"<br>";
}
////agents
const aBots =await fetchAPIData(oEnvir.url+"/api/data/v9.2/bots?$filter=_ownerid_value eq '"+oWhoAmI+"' and createdon gt "+sDate+" and createdon le "+sDateTo,envirToken,oEnvir.displayName);
if(aBots){
aBots.value.forEach(bot =>{
aAllData.push(
{
type:"agent",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:bot.schemaname,
dataverseId:bot.botid,
displayName:bot.name,
createdTime:bot.createdon,
isManaged:bot.ismanaged,
triggger:bot.authenticationtrigger,
month:new Date(bot.createdon).getMonth()+1
}
)
})
eData.innerHTML+="<i class='fa-solid fa-robot'></i> "+oEnvir.displayName+" copilot agents found "+aBots.value.length+"<br>";
}
///solutions
const aSolutions =await fetchAPIData(oEnvir.url+"/api/data/v9.2/solutions?$filter=_createdby_value eq '"+oWhoAmI+"' and createdon gt "+sDate+" and createdon le "+sDateTo,envirToken,oEnvir.displayName);
if(aSolutions){
aSolutions.value.forEach(sol =>{
if(!sol.uniquename.includes("msdyn")){
getComponents(oEnvir,sol,oWhoAmI)
}
})
eData.innerHTML+="<i class='fa-solid fa-box-open'></i> "+oEnvir.displayName+" solutions found "+aSolutions.value.length+"<br>";
}
////connection refs
const aConnections =await fetchAPIData(oEnvir.url+"/api/data/v9.2/connectionreferences?$filter=_ownerid_value eq '"+oWhoAmI+"' and createdon gt "+sDate+" and createdon le "+sDateTo,envirToken,oEnvir.displayName);
if(aConnections){
aConnections.value.forEach(con =>{
aAllData.push(
{
type:"connection reference",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:con.connectionreferencelogicalname,
dataverseId:con.connectionreferenceid,
displayName:con.connectionreferencedisplayname,
createdTime:con.createdon,
isManaged:con.ismanaged,
connector:con.connectorid.replace("/providers/Microsoft.PowerApps/apis/",""),
month:new Date(con.createdon).getMonth()+1
}
)
})
eData.innerHTML+="<i class='fa-solid fa-plug'></i> "+oEnvir.displayName+" connection references found "+aConnections.value.length+"<br>";
}
////environment variables
const aVaraiables =await fetchAPIData(oEnvir.url+"/api/data/v9.2/environmentvariabledefinitions?$filter=_ownerid_value eq '"+oWhoAmI+"' and createdon gt "+sDate+" and createdon le "+sDateTo,envirToken,oEnvir.displayName);
if(aVaraiables){
aVaraiables.value.forEach(eva =>{
aAllData.push(
{
type:"environment variable",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:eva.schemaname,
dataverseId:eva.environmentvariabledefinitionid,
displayName:eva.displayname,
createdTime:eva.createdon,
isManaged:eva.ismanaged,
variableType:variableType(eva.type),
month:new Date(eva.createdon).getMonth()+1
}
)
})
eData.innerHTML+="<i class='fa-solid fa-database'></i> "+oEnvir.displayName+" environment variables found "+aVaraiables.value.length+"<br>";
}
iAPICount++;
eCount.innerText=iAPICount;
}
}catch(error){
eData.innerHTML+="<i class='fa-solid fa-laptop'></i> "+oEnvir.displayName+" error loading data <br>";
iAPICount++;
eCount.innerText=iAPICount;
}
if(iAPICount==aEnvironmentsMaster.length){
finalizeData(bDestroy);
}
}
function finalizeData(bDestroy){
console.log(aAllData);
eLoad.innerHTML="Key Data";
eDiv.style.display="none";
aEnvironmentData.length=0;
aEnvironmentsMaster.forEach(envir =>{
const aThisEnvironment=aAllData.filter(item => {return item.environment.id==envir.id});
aEnvironmentData.push({
displayName:envir.displayName,
id:envir.id,
flows: aThisEnvironment.filter(item =>{return item.type=="flow"}).length,
apps: aThisEnvironment.filter(item =>{return item.type=="app"}).length,
agents: aThisEnvironment.filter(item =>{return item.type=="agent"}).length,
solutions: aThisEnvironment.filter(item =>{return item.type=="solution"}).length,
connectionReferences: aThisEnvironment.filter(item =>{return item.type=="connectionReferences"}).length,
environmentVariables: aThisEnvironment.filter(item =>{return item.type=="environmentVariable"}).length,
components:aThisEnvironment.length
})
})
loadCharts(aAllData,aEnvironmentData,bDestroy,sCurrentGeo);
localStorage.setItem("geo",sCurrentGeo);
localStorage.setItem("data",JSON.stringify(aAllData));
localStorage.setItem("environments",JSON.stringify(aEnvironmentsMaster));
localStorage.setItem("date",sDate);
localStorage.setItem("dateTo",sDateTo);
}
async function getComponents(oEnvir,sol,sUser){
const envirToken = getTokenForUrl(oEnvir.url);
if(!envirToken) return;
const aComponents =await fetchAPIData(oEnvir.url+"/api/data/v9.2/solutioncomponents?$filter=_createdby_value eq '"+sUser+"' and _solutionid_value eq '"+sol.solutionid+"'",envirToken,oEnvir.displayName);
if(aComponents.value.length>0){
aAllData.push(
{
type:"solution",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:sol.uniquename,
dataverseId:sol.solutionid,
displayName:sol.friendlyname,
createdTime:sol.createdon,
isManaged:sol.ismanaged,
contents:{
flows:aComponents.value.filter(item =>{return item.componenttype == 29}).length,
apps:aComponents.value.filter(item =>{return item.componenttype == 300}).length,
var:aComponents.value.filter(item =>{return item.componenttype == 381}).length,
components:aComponents.value.length
},
month:new Date(sol.createdon).getMonth()+1
}
)
}
}
function downloadJSON(data,sFileName) {
const jsonString = JSON.stringify(data, null, 2);
const blob = new Blob([jsonString], { type: "application/json" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = sFileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
async function getEnvironments(sEnvirToken, sEnvirURL) {
try {
const data = await fetchAPIData(sEnvirURL, sEnvirToken,"");
console.log(data.value)
const aEnvironments = data.value
.sort((a, b) => {
const titleA = a.properties.displayName.toUpperCase();
const titleB = b.properties.displayName.toUpperCase();
return titleA.localeCompare(titleB);
})
.map(item => {
let sUrl = "";
if (item.properties.hasOwnProperty("linkedEnvironmentMetadata")) {
sUrl = item.properties.linkedEnvironmentMetadata.instanceUrl;
sUrlAPI= item.properties.linkedEnvironmentMetadata.instanceApiUrl;
}
return {
displayName: item.properties.displayName,
id: item.name,
url: sUrlAPI,
dyn: sUrl,
location: item.location
};
});
return aEnvironments;
} catch (error) {
console.log("Error Get Environments:", error);
return [];
}
}
function variableType(iVar){
if(iVar==100000000){
return "String"
}
if(iVar==100000001){
return "Number"
}
if(iVar==100000002){
return "Boolean"
}
if(iVar==100000003){
return "JSON"
}
if(iVar==100000004){
return "Data Source"
}
if(iVar==100000005){
return "Secret"
}
}
async function fetchAPIData(url, token, environment) {
try {
const options = {
headers: {
Authorization: token
},
};
const response = await fetch(url, options);
if (!response.ok) {
console.log("Error fetching API data:", response.status+", "+response.statusText);
if (response.status == 401 && !url.includes("WhoAmI")) {
eData.innerHTML+="<br><b style='color:red;'>"+environment+" Invalid token, refresh https://make.powerautomate.com page to update</b><br>";
return null;
}
if (!response.status == 401 && url.includes("WhoAmI")){
eData.innerHTML+="<br><b style='color:red;'>"+environment+" WhoAmI failed</b><br>";
return null;
}
}
const data = await response.json();
return data;
} catch (error) {
console.log("Error fetching API data:", error);
return null
}
}
//// not in use - need to add "https://make.powerapps.com/*" to manifest
async function test(token){
const oRefresh=await fetchAPIDataPost("https://make.powerapps.com/api/exchangeToken", token,"https://eu-uk-dev.api.crm11.dynamics.com");
console.log(oRefresh);
}
async function fetchAPIDataPost(url, token, environment) {
try {
const body={"audience":environment.replace(".api",""),"tenantId":"common","isCaeEnabled":false,"aadClientId":"a8f7a65c-f5ba-4859-b2d6-df772c264e9d"};
const options = {
method:"POST",
body:JSON.stringify(body),
headers: {
Authorization: token
},
};
const response = await fetch(url, options);
console.log(options,body,response)
if (!response.ok) {
console.log("Error fetching API data:", response.status+", "+response.statusText);
return null
}
const data = await response.json();
return data;
} catch (error) {
console.log("Error fetching API data:", error);
return null
}
}