codeapp-js 0.2.1 → 0.3.0

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.
Files changed (100) hide show
  1. package/codeApp/.power/schemas/appschemas/dataSourcesInfo.ts +6275 -0
  2. package/codeApp/.power/schemas/jira/jira.Schema.json +6903 -0
  3. package/codeApp/.power/schemas/keyvault/keyvault.Schema.json +1600 -0
  4. package/codeApp/.power/schemas/teams/teams.Schema.json +11112 -0
  5. package/codeApp/dist/codeapp.js +992 -4
  6. package/codeApp/dist/power-apps-data.js +145 -31
  7. package/codeApp/src/generated/index.ts +12 -0
  8. package/codeApp/src/generated/models/AzureKeyVaultModel.ts +107 -0
  9. package/codeApp/src/generated/models/JiraModel.ts +501 -0
  10. package/codeApp/src/generated/models/Office365GroupsModel.ts +363 -0
  11. package/codeApp/src/generated/models/Office365OutlookModel.ts +2046 -0
  12. package/codeApp/src/generated/models/Office365UsersModel.ts +254 -0
  13. package/codeApp/src/generated/services/AzureKeyVaultService.ts +257 -0
  14. package/codeApp/src/generated/services/JiraService.ts +1124 -0
  15. package/codeApp/src/generated/services/Office365GroupsService.ts +326 -0
  16. package/codeApp/src/generated/services/Office365OutlookService.ts +2476 -0
  17. package/codeApp/src/generated/services/Office365UsersService.ts +358 -0
  18. package/dev files/outlook.js +218 -9
  19. package/examples/combined demo/dist/codeapp.js +1098 -1098
  20. package/examples/combined demo/dist/index.js +470 -470
  21. package/examples/combined demo/dist/power-apps-data.js +3006 -3006
  22. package/examples/combined demo/power.config.json +42 -42
  23. package/examples/dataverse Demo/dist/codeapp.js +1085 -1085
  24. package/examples/dataverse Demo/dist/index.html +54 -54
  25. package/examples/dataverse Demo/dist/index.js +82 -82
  26. package/examples/dataverse Demo/dist/power-apps-data.js +2911 -2911
  27. package/examples/dataverse Demo/power.config.json +34 -34
  28. package/examples/dataverse Demo/readme.md +79 -79
  29. package/examples/groups Demo/dist/codeapp.js +1085 -1085
  30. package/examples/groups Demo/dist/index.js +113 -113
  31. package/examples/groups Demo/dist/power-apps-data.js +2911 -2911
  32. package/examples/kanban/dist/dataverse.js +94 -94
  33. package/examples/kanban/dist/environmentVar.js +55 -55
  34. package/examples/kanban/dist/office365groups.js +97 -97
  35. package/examples/kanban/dist/office365users.js +169 -169
  36. package/examples/kanban/dist/outlook.js +162 -162
  37. package/examples/kanban/dist/power-apps-data.js +2953 -2953
  38. package/examples/kanban/dist/sharepoint.js +339 -339
  39. package/examples/myProfile/dist/index.html +184 -184
  40. package/examples/myProfile/dist/index.js +141 -141
  41. package/examples/myProfile/dist/office365users.js +169 -169
  42. package/examples/myProfile/dist/power-apps-data.js +2953 -2953
  43. package/examples/myProfile/power.config.json +22 -22
  44. package/examples/myProfile/readme.md +79 -79
  45. package/examples/outlook Demo/dist/codeapp.js +1085 -1085
  46. package/examples/outlook Demo/dist/index.html +35 -35
  47. package/examples/outlook Demo/dist/index.js +170 -170
  48. package/examples/outlook Demo/dist/outlook.js +121 -121
  49. package/examples/outlook Demo/dist/power-apps-data.js +2911 -2911
  50. package/examples/outlook Demo/dist/styles.css +84 -84
  51. package/examples/outlook Demo/readme.md +82 -82
  52. package/examples/outlook Demo2/OutlookDemo_1_0_0_1.zip +0 -0
  53. package/examples/outlook Demo2/agent/decision-log.md +7 -0
  54. package/examples/{solution explorer → outlook Demo2}/dist/codeapp.js +245 -9
  55. package/examples/outlook Demo2/dist/index.html +98 -0
  56. package/examples/outlook Demo2/dist/index.js +272 -0
  57. package/examples/outlook Demo2/dist/styles.css +639 -0
  58. package/examples/outlook Demo2/power.config.json +23 -0
  59. package/examples/outlook Demo2/src/generated/index.ts +14 -0
  60. package/examples/outlook Demo2/src/generated/models/Office365GroupsModel.ts +363 -0
  61. package/examples/outlook Demo2/src/generated/models/Office365OutlookModel.ts +2046 -0
  62. package/examples/outlook Demo2/src/generated/models/Office365UsersModel.ts +254 -0
  63. package/examples/outlook Demo2/src/generated/services/Office365GroupsService.ts +326 -0
  64. package/examples/outlook Demo2/src/generated/services/Office365OutlookService.ts +2476 -0
  65. package/examples/outlook Demo2/src/generated/services/Office365UsersService.ts +358 -0
  66. package/examples/planning Poker/.vscode/settings.json +4 -4
  67. package/examples/planning Poker/additional files/customizations (tables).xml +6428 -6428
  68. package/examples/planning Poker/additional files/dataverse-tables.json +165 -165
  69. package/examples/planning Poker/additional files/readme.md +122 -122
  70. package/examples/planning Poker/dist/dataverse.js +78 -78
  71. package/examples/planning Poker/dist/index.html +198 -198
  72. package/examples/planning Poker/dist/index.js +954 -954
  73. package/examples/planning Poker/dist/power-apps-data.js +2953 -2953
  74. package/examples/planning Poker/dist/styles.css +815 -815
  75. package/examples/sharePoint Demo/agent/decision-log.md +5 -5
  76. package/examples/sharePoint Demo/dist/codeapp.js +1085 -1085
  77. package/examples/sharePoint Demo/dist/index.js +262 -262
  78. package/examples/sharePoint Demo/dist/power-apps-data.js +2911 -2911
  79. package/examples/sharePoint Demo/power.config.json +22 -22
  80. package/examples/todo/dist/dataverse.js +64 -64
  81. package/examples/todo/dist/index.html +75 -75
  82. package/examples/todo/dist/index.js +8 -8
  83. package/examples/todo/dist/power-apps-data.js +2953 -2953
  84. package/examples/todo/dist/renderer.js +375 -375
  85. package/examples/todo/dist/styles.css +691 -691
  86. package/examples/todo/power.config.json +34 -34
  87. package/package.json +1 -1
  88. package/readme.md +33 -4
  89. package/examples/solution explorer/agent/decision-log.md +0 -27
  90. package/examples/solution explorer/agent/mockup-01-swiss-grid.html +0 -452
  91. package/examples/solution explorer/agent/mockup-02-dark-glass.html +0 -496
  92. package/examples/solution explorer/agent/mockup-03-paper-console.html +0 -510
  93. package/examples/solution explorer/agent/mockup-04-neon-noir.html +0 -546
  94. package/examples/solution explorer/agent/mockup-05-zen-garden.html +0 -534
  95. package/examples/solution explorer/dist/index.html +0 -80
  96. package/examples/solution explorer/dist/index.js +0 -735
  97. package/examples/solution explorer/dist/styles.css +0 -571
  98. package/examples/solution explorer/power.config.json +0 -151
  99. /package/examples/{solution explorer → outlook Demo2}/dist/icon-512.png +0 -0
  100. /package/examples/{solution explorer → outlook Demo2}/dist/power-apps-data.js +0 -0
@@ -1,470 +1,470 @@
1
-
2
- import { getMyProfile, listEmails, listMyGroups } from './codeapp.js';
3
- import { getClient } from './power-apps-data.js';
4
-
5
- // ── SendEmailV2 connector (not yet in codeapp.js) ─────────────
6
- const SEND_EMAIL_CANDIDATES = ['office365outlook', 'Office365Outlook', 'office365'];
7
- const SEND_EMAIL_APIS = {
8
- SendEmailV2: {
9
- path: '/{connectionId}/v2/Mail',
10
- method: 'POST',
11
- parameters: [
12
- { name: 'connectionId', in: 'path', required: true },
13
- { name: 'emailMessage', in: 'body', required: true }
14
- ]
15
- }
16
- };
17
-
18
- let oSendClient = null;
19
-
20
- function getSendClient() {
21
- if (!oSendClient) {
22
- let oSources = {};
23
- SEND_EMAIL_CANDIDATES.forEach((sName) => {
24
- oSources[sName] = {
25
- tableId: '',
26
- version: '',
27
- primaryKey: '',
28
- dataSourceType: 'Connector',
29
- apis: SEND_EMAIL_APIS
30
- };
31
- });
32
- oSendClient = getClient(oSources);
33
- }
34
- return oSendClient;
35
- }
36
-
37
- let eStatusMessage = null;
38
- let eProfileCard = null;
39
- let eProfileStatus = null;
40
- let eEmailList = null;
41
- let eGroupList = null;
42
- let eEmailCount = null;
43
- let eGroupCount = null;
44
- let eRefreshButton = null;
45
- let eScrollComposeButton = null;
46
- let eComposeForm = null;
47
- let eComposeTo = null;
48
- let eComposeSubject = null;
49
- let eComposeBody = null;
50
- let eSendButton = null;
51
-
52
- let oProfile = null;
53
- let aEmails = [];
54
- let aGroups = [];
55
-
56
- function getElement(sId) {
57
- return document.getElementById(sId);
58
- }
59
-
60
- function cacheDomElements() {
61
- eStatusMessage = getElement('statusMessage');
62
- eProfileCard = getElement('profileCard');
63
- eProfileStatus = getElement('profileStatus');
64
- eEmailList = getElement('emailList');
65
- eGroupList = getElement('groupList');
66
- eEmailCount = getElement('emailCount');
67
- eGroupCount = getElement('groupCount');
68
- eRefreshButton = getElement('refreshButton');
69
- eScrollComposeButton = getElement('scrollComposeButton');
70
- eComposeForm = getElement('composeForm');
71
- eComposeTo = getElement('composeTo');
72
- eComposeSubject = getElement('composeSubject');
73
- eComposeBody = getElement('composeBody');
74
- eSendButton = getElement('sendButton');
75
- }
76
-
77
- function escapeHtml(sValue) {
78
- let sText = String(sValue == null ? '' : sValue);
79
- return sText
80
- .replace(new RegExp('&', 'g'), '&')
81
- .replace(new RegExp('<', 'g'), '&lt;')
82
- .replace(new RegExp('>', 'g'), '&gt;')
83
- .replace(new RegExp('"', 'g'), '&quot;')
84
- .replace(new RegExp("'", 'g'), '&#39;');
85
- }
86
-
87
- function setStatus(sMessage, sTone = 'info') {
88
- if (!eStatusMessage) {
89
- return;
90
- }
91
- eStatusMessage.textContent = sMessage;
92
- eStatusMessage.className = 'statusBar ' + sTone;
93
- }
94
-
95
- function getInitials(sName) {
96
- let aParts = String(sName || 'U').trim().split(new RegExp('\\s+', 'g')).filter(Boolean);
97
- return aParts.slice(0, 2).map((sPart) => sPart.charAt(0).toUpperCase()).join('');
98
- }
99
-
100
- function normalizePayload(oValue) {
101
- if (oValue == null) {
102
- return null;
103
- }
104
-
105
- if (typeof oValue === 'string') {
106
- try {
107
- return JSON.parse(oValue);
108
- } catch (oErr) {
109
- return oValue;
110
- }
111
- }
112
-
113
- if (typeof oValue === 'object' && Object.prototype.hasOwnProperty.call(oValue, 'body')) {
114
- return normalizePayload(oValue.body);
115
- }
116
-
117
- if (typeof oValue === 'object' && Object.prototype.hasOwnProperty.call(oValue, 'value') && Array.isArray(oValue.value)) {
118
- return oValue;
119
- }
120
-
121
- return oValue;
122
- }
123
-
124
- function getArrayFromPayload(oValue) {
125
- let oPayload = normalizePayload(oValue);
126
-
127
- if (Array.isArray(oPayload)) {
128
- return oPayload;
129
- }
130
-
131
- if (oPayload && Array.isArray(oPayload.value)) {
132
- return oPayload.value;
133
- }
134
-
135
- if (oPayload && Array.isArray(oPayload.messages)) {
136
- return oPayload.messages;
137
- }
138
-
139
- if (oPayload && Array.isArray(oPayload.items)) {
140
- return oPayload.items;
141
- }
142
-
143
- return [];
144
- }
145
-
146
- function getProfileFromPayload(oValue) {
147
- let oPayload = normalizePayload(oValue);
148
- if (!oPayload || typeof oPayload !== 'object') {
149
- return {};
150
- }
151
- return oPayload.user && typeof oPayload.user === 'object' ? oPayload.user : oPayload;
152
- }
153
-
154
- function formatEmailDate(sValue) {
155
- if (!sValue) {
156
- return 'No date';
157
- }
158
-
159
- let oDate = new Date(sValue);
160
- if (Number.isNaN(oDate.getTime())) {
161
- return String(sValue);
162
- }
163
-
164
- return oDate.toLocaleString();
165
- }
166
-
167
- function getEmailFromValue(oValue) {
168
- if (!oValue || typeof oValue !== 'object') {
169
- return '';
170
- }
171
-
172
- if (typeof oValue.address === 'string') {
173
- return oValue.address;
174
- }
175
-
176
- if (typeof oValue.email === 'string') {
177
- return oValue.email;
178
- }
179
-
180
- if (oValue.emailAddress && typeof oValue.emailAddress.address === 'string') {
181
- return oValue.emailAddress.address;
182
- }
183
-
184
- if (oValue.EmailAddress && typeof oValue.EmailAddress.Address === 'string') {
185
- return oValue.EmailAddress.Address;
186
- }
187
-
188
- return '';
189
- }
190
-
191
- function getSenderLabel(oEmail) {
192
- let oFrom = oEmail.from || oEmail.From || oEmail.sender || oEmail.Sender || {};
193
- if (oFrom.emailAddress && typeof oFrom.emailAddress === 'object') {
194
- return oFrom.emailAddress.name || oFrom.emailAddress.address || 'Unknown sender';
195
- }
196
- if (oFrom.EmailAddress && typeof oFrom.EmailAddress === 'object') {
197
- return oFrom.EmailAddress.Name || oFrom.EmailAddress.Address || 'Unknown sender';
198
- }
199
- if (typeof oFrom.displayName === 'string') {
200
- return oFrom.displayName;
201
- }
202
- return getEmailFromValue(oFrom) || 'Unknown sender';
203
- }
204
-
205
- function getGroupTagsMarkup(oGroup) {
206
- let aTags = Array.isArray(oGroup.groupTypes) ? oGroup.groupTypes : [];
207
- return aTags.map((sTag) => '<span class="pill">' + escapeHtml(sTag) + '</span>').join('');
208
- }
209
-
210
- function renderProfile() {
211
- if (!eProfileCard) {
212
- return;
213
- }
214
-
215
- if (!oProfile || Object.keys(oProfile).length === 0) {
216
- eProfileCard.innerHTML = '<div class="emptyState">Profile information was not returned.</div>';
217
- if (eProfileStatus) {
218
- eProfileStatus.textContent = 'Unavailable';
219
- }
220
- return;
221
- }
222
-
223
- let sName = oProfile.displayName || oProfile.DisplayName || oProfile.name || 'Unknown user';
224
- let sEmail = oProfile.mail || oProfile.Mail || oProfile.userPrincipalName || oProfile.UserPrincipalName || '';
225
- let sJobTitle = oProfile.jobTitle || oProfile.JobTitle || 'No title';
226
- let sDepartment = oProfile.department || oProfile.Department || 'No department';
227
- let sPhone = oProfile.mobilePhone || oProfile.MobilePhone || '';
228
-
229
- eProfileCard.innerHTML = `
230
- <div class="profileBadge">${escapeHtml(getInitials(sName))}</div>
231
- <div>
232
- <h3 class="profileName">${escapeHtml(sName)}</h3>
233
- <p class="profileMeta">${escapeHtml(sJobTitle)} · ${escapeHtml(sDepartment)}</p>
234
- <p class="profileSubMeta">${escapeHtml(sEmail || 'No email')}</p>
235
- <p class="profileSubMeta">${escapeHtml(sPhone || 'No phone')}</p>
236
- </div>
237
- `;
238
-
239
- if (eProfileStatus) {
240
- eProfileStatus.textContent = 'Ready';
241
- }
242
- }
243
-
244
- function renderEmails() {
245
- if (!eEmailList) {
246
- return;
247
- }
248
-
249
- if (eEmailCount) {
250
- eEmailCount.textContent = String(aEmails.length);
251
- }
252
-
253
- if (!Array.isArray(aEmails) || aEmails.length === 0) {
254
- eEmailList.innerHTML = '<div class="emptyState">No emails were returned.</div>';
255
- return;
256
- }
257
-
258
- eEmailList.innerHTML = aEmails.map((oEmail) => {
259
- let sSubject = oEmail.subject || oEmail.Subject || '(No subject)';
260
- let sPreview = oEmail.bodyPreview || oEmail.BodyPreview || oEmail.body || oEmail.Body || '';
261
- let sReceived = oEmail.receivedDateTime || oEmail.DateTimeReceived || oEmail.createdDateTime || '';
262
- let sSender = getSenderLabel(oEmail);
263
-
264
- return `
265
- <article class="listCard">
266
- <h3 class="listTitle">${escapeHtml(sSubject)}</h3>
267
- <p class="listMeta">From: ${escapeHtml(sSender)} · ${escapeHtml(formatEmailDate(sReceived))}</p>
268
- <p class="listBody">${escapeHtml(String(sPreview).slice(0, 220) || 'No preview available.')}</p>
269
- </article>
270
- `;
271
- }).join('');
272
- }
273
-
274
- function renderGroups() {
275
- if (!eGroupList) {
276
- return;
277
- }
278
-
279
- if (eGroupCount) {
280
- eGroupCount.textContent = String(aGroups.length);
281
- }
282
-
283
- if (!Array.isArray(aGroups) || aGroups.length === 0) {
284
- eGroupList.innerHTML = '<div class="emptyState">No group memberships were returned.</div>';
285
- return;
286
- }
287
-
288
- eGroupList.innerHTML = aGroups.map((oGroup) => {
289
- let sName = oGroup.displayName || oGroup.DisplayName || 'Unnamed group';
290
- let sDescription = oGroup.description || oGroup.Description || 'No description';
291
- let sMail = oGroup.mail || oGroup.Mail || 'No group mailbox';
292
-
293
- return `
294
- <article class="listCard">
295
- <h3 class="listTitle">${escapeHtml(sName)}</h3>
296
- <p class="listMeta">${escapeHtml(sMail)}</p>
297
- <p class="listBody">${escapeHtml(sDescription)}</p>
298
- ${getGroupTagsMarkup(oGroup)}
299
- </article>
300
- `;
301
- }).join('');
302
- }
303
-
304
- async function loadProfile() {
305
- oProfile = getProfileFromPayload(await getMyProfile());
306
- renderProfile();
307
- }
308
-
309
- async function loadEmails() {
310
- aEmails = getArrayFromPayload(await listEmails({ folderId: 'Inbox', top: 15 }));
311
- renderEmails();
312
- }
313
-
314
- async function loadGroups() {
315
- aGroups = getArrayFromPayload(await listMyGroups());
316
- renderGroups();
317
- }
318
-
319
- async function sendEmailMessage(sTo, sSubject, sBody) {
320
- let client = getSendClient();
321
- let aErrors = [];
322
-
323
- for (let iIndex = 0; iIndex < SEND_EMAIL_CANDIDATES.length; iIndex += 1) {
324
- let sName = SEND_EMAIL_CANDIDATES[iIndex];
325
- try {
326
- let oResult = await client.executeAsync({
327
- connectorOperation: {
328
- tableName: sName,
329
- operationName: 'SendEmailV2',
330
- parameters: {
331
- emailMessage: {
332
- To: sTo,
333
- Subject: sSubject,
334
- Body: sBody,
335
- Importance: 'Normal',
336
- IsHtml: true
337
- }
338
- }
339
- }
340
- });
341
- if (oResult && oResult.success === false) {
342
- throw new Error(oResult.error ? (oResult.error.message || JSON.stringify(oResult.error)) : 'Send failed');
343
- }
344
- return oResult && Object.prototype.hasOwnProperty.call(oResult, 'data') ? oResult.data : oResult;
345
- } catch (oErr) {
346
- let sMessage = oErr.message || String(oErr);
347
- aErrors.push(sName + ': ' + sMessage);
348
- if (sMessage.indexOf('Connection reference not found') === -1) {
349
- throw oErr;
350
- }
351
- }
352
- }
353
-
354
- throw new Error('No Outlook connection reference matched. Tried: ' + aErrors.join(' || '));
355
- }
356
-
357
- async function loadDashboard() {
358
- setStatus('Loading your dashboard…', 'info');
359
-
360
- let aResults = await Promise.allSettled([
361
- loadProfile(),
362
- loadEmails(),
363
- loadGroups()
364
- ]);
365
-
366
- let aErrors = aResults
367
- .filter((oResult) => oResult.status === 'rejected')
368
- .map((oResult) => oResult.reason?.message || String(oResult.reason));
369
-
370
- renderProfile();
371
- renderEmails();
372
- renderGroups();
373
-
374
- if (aErrors.length > 0) {
375
- setStatus('Loaded with connector issues: ' + aErrors.join(' | '), 'error');
376
- return;
377
- }
378
-
379
- setStatus('Dashboard loaded successfully.', 'success');
380
- }
381
-
382
- async function handleRefreshClick() {
383
- await loadDashboard();
384
- }
385
-
386
- function handleScrollComposeClick() {
387
- let eComposeSection = getElement('composeSection');
388
- if (eComposeSection) {
389
- eComposeSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
390
- }
391
- }
392
-
393
- async function handleComposeSubmit(oEvent) {
394
- oEvent.preventDefault();
395
-
396
- let sTo = String(eComposeTo?.value || '').trim();
397
- let sSubject = String(eComposeSubject?.value || '').trim();
398
- let sBody = String(eComposeBody?.value || '').trim();
399
-
400
- if (!sTo || !sSubject || !sBody) {
401
- setStatus('Complete the To, Subject, and Message fields before sending.', 'error');
402
- return;
403
- }
404
-
405
- try {
406
- if (eSendButton) {
407
- eSendButton.disabled = true;
408
- eSendButton.textContent = 'Sending...';
409
- }
410
-
411
- setStatus('Sending email…', 'info');
412
- await sendEmailMessage(sTo, sSubject, sBody);
413
-
414
- if (eComposeForm) {
415
- eComposeForm.reset();
416
- }
417
-
418
- setStatus('Email sent successfully.', 'success');
419
- await loadEmails();
420
- } catch (oErr) {
421
- setStatus('Send email failed: ' + (oErr.message || oErr), 'error');
422
- } finally {
423
- if (eSendButton) {
424
- eSendButton.disabled = false;
425
- eSendButton.textContent = 'Send email';
426
- }
427
- }
428
- }
429
-
430
- function attachEvents() {
431
- if (eRefreshButton) {
432
- eRefreshButton.addEventListener('click', () => {
433
- handleRefreshClick().catch((oErr) => {
434
- setStatus('Refresh failed: ' + (oErr.message || oErr), 'error');
435
- });
436
- });
437
- }
438
-
439
- if (eScrollComposeButton) {
440
- eScrollComposeButton.addEventListener('click', handleScrollComposeClick);
441
- }
442
-
443
- if (eComposeForm) {
444
- eComposeForm.addEventListener('submit', (oEvent) => {
445
- handleComposeSubmit(oEvent).catch((oErr) => {
446
- setStatus('Send email failed: ' + (oErr.message || oErr), 'error');
447
- });
448
- });
449
- }
450
- }
451
-
452
- async function boot() {
453
- cacheDomElements();
454
- attachEvents();
455
- renderProfile();
456
- renderEmails();
457
- renderGroups();
458
-
459
- try {
460
- await loadDashboard();
461
- } catch (oErr) {
462
- setStatus('App failed to start: ' + (oErr.message || oErr), 'error');
463
- }
464
- }
465
-
466
- window.addEventListener('DOMContentLoaded', () => {
467
- boot().catch((oErr) => {
468
- setStatus('App failed to start: ' + (oErr.message || oErr), 'error');
469
- });
470
- });
1
+
2
+ import { getMyProfile, listEmails, listMyGroups } from './codeapp.js';
3
+ import { getClient } from './power-apps-data.js';
4
+
5
+ // ── SendEmailV2 connector (not yet in codeapp.js) ─────────────
6
+ const SEND_EMAIL_CANDIDATES = ['office365outlook', 'Office365Outlook', 'office365'];
7
+ const SEND_EMAIL_APIS = {
8
+ SendEmailV2: {
9
+ path: '/{connectionId}/v2/Mail',
10
+ method: 'POST',
11
+ parameters: [
12
+ { name: 'connectionId', in: 'path', required: true },
13
+ { name: 'emailMessage', in: 'body', required: true }
14
+ ]
15
+ }
16
+ };
17
+
18
+ let oSendClient = null;
19
+
20
+ function getSendClient() {
21
+ if (!oSendClient) {
22
+ let oSources = {};
23
+ SEND_EMAIL_CANDIDATES.forEach((sName) => {
24
+ oSources[sName] = {
25
+ tableId: '',
26
+ version: '',
27
+ primaryKey: '',
28
+ dataSourceType: 'Connector',
29
+ apis: SEND_EMAIL_APIS
30
+ };
31
+ });
32
+ oSendClient = getClient(oSources);
33
+ }
34
+ return oSendClient;
35
+ }
36
+
37
+ let eStatusMessage = null;
38
+ let eProfileCard = null;
39
+ let eProfileStatus = null;
40
+ let eEmailList = null;
41
+ let eGroupList = null;
42
+ let eEmailCount = null;
43
+ let eGroupCount = null;
44
+ let eRefreshButton = null;
45
+ let eScrollComposeButton = null;
46
+ let eComposeForm = null;
47
+ let eComposeTo = null;
48
+ let eComposeSubject = null;
49
+ let eComposeBody = null;
50
+ let eSendButton = null;
51
+
52
+ let oProfile = null;
53
+ let aEmails = [];
54
+ let aGroups = [];
55
+
56
+ function getElement(sId) {
57
+ return document.getElementById(sId);
58
+ }
59
+
60
+ function cacheDomElements() {
61
+ eStatusMessage = getElement('statusMessage');
62
+ eProfileCard = getElement('profileCard');
63
+ eProfileStatus = getElement('profileStatus');
64
+ eEmailList = getElement('emailList');
65
+ eGroupList = getElement('groupList');
66
+ eEmailCount = getElement('emailCount');
67
+ eGroupCount = getElement('groupCount');
68
+ eRefreshButton = getElement('refreshButton');
69
+ eScrollComposeButton = getElement('scrollComposeButton');
70
+ eComposeForm = getElement('composeForm');
71
+ eComposeTo = getElement('composeTo');
72
+ eComposeSubject = getElement('composeSubject');
73
+ eComposeBody = getElement('composeBody');
74
+ eSendButton = getElement('sendButton');
75
+ }
76
+
77
+ function escapeHtml(sValue) {
78
+ let sText = String(sValue == null ? '' : sValue);
79
+ return sText
80
+ .replace(new RegExp('&', 'g'), '&amp;')
81
+ .replace(new RegExp('<', 'g'), '&lt;')
82
+ .replace(new RegExp('>', 'g'), '&gt;')
83
+ .replace(new RegExp('"', 'g'), '&quot;')
84
+ .replace(new RegExp("'", 'g'), '&#39;');
85
+ }
86
+
87
+ function setStatus(sMessage, sTone = 'info') {
88
+ if (!eStatusMessage) {
89
+ return;
90
+ }
91
+ eStatusMessage.textContent = sMessage;
92
+ eStatusMessage.className = 'statusBar ' + sTone;
93
+ }
94
+
95
+ function getInitials(sName) {
96
+ let aParts = String(sName || 'U').trim().split(new RegExp('\\s+', 'g')).filter(Boolean);
97
+ return aParts.slice(0, 2).map((sPart) => sPart.charAt(0).toUpperCase()).join('');
98
+ }
99
+
100
+ function normalizePayload(oValue) {
101
+ if (oValue == null) {
102
+ return null;
103
+ }
104
+
105
+ if (typeof oValue === 'string') {
106
+ try {
107
+ return JSON.parse(oValue);
108
+ } catch (oErr) {
109
+ return oValue;
110
+ }
111
+ }
112
+
113
+ if (typeof oValue === 'object' && Object.prototype.hasOwnProperty.call(oValue, 'body')) {
114
+ return normalizePayload(oValue.body);
115
+ }
116
+
117
+ if (typeof oValue === 'object' && Object.prototype.hasOwnProperty.call(oValue, 'value') && Array.isArray(oValue.value)) {
118
+ return oValue;
119
+ }
120
+
121
+ return oValue;
122
+ }
123
+
124
+ function getArrayFromPayload(oValue) {
125
+ let oPayload = normalizePayload(oValue);
126
+
127
+ if (Array.isArray(oPayload)) {
128
+ return oPayload;
129
+ }
130
+
131
+ if (oPayload && Array.isArray(oPayload.value)) {
132
+ return oPayload.value;
133
+ }
134
+
135
+ if (oPayload && Array.isArray(oPayload.messages)) {
136
+ return oPayload.messages;
137
+ }
138
+
139
+ if (oPayload && Array.isArray(oPayload.items)) {
140
+ return oPayload.items;
141
+ }
142
+
143
+ return [];
144
+ }
145
+
146
+ function getProfileFromPayload(oValue) {
147
+ let oPayload = normalizePayload(oValue);
148
+ if (!oPayload || typeof oPayload !== 'object') {
149
+ return {};
150
+ }
151
+ return oPayload.user && typeof oPayload.user === 'object' ? oPayload.user : oPayload;
152
+ }
153
+
154
+ function formatEmailDate(sValue) {
155
+ if (!sValue) {
156
+ return 'No date';
157
+ }
158
+
159
+ let oDate = new Date(sValue);
160
+ if (Number.isNaN(oDate.getTime())) {
161
+ return String(sValue);
162
+ }
163
+
164
+ return oDate.toLocaleString();
165
+ }
166
+
167
+ function getEmailFromValue(oValue) {
168
+ if (!oValue || typeof oValue !== 'object') {
169
+ return '';
170
+ }
171
+
172
+ if (typeof oValue.address === 'string') {
173
+ return oValue.address;
174
+ }
175
+
176
+ if (typeof oValue.email === 'string') {
177
+ return oValue.email;
178
+ }
179
+
180
+ if (oValue.emailAddress && typeof oValue.emailAddress.address === 'string') {
181
+ return oValue.emailAddress.address;
182
+ }
183
+
184
+ if (oValue.EmailAddress && typeof oValue.EmailAddress.Address === 'string') {
185
+ return oValue.EmailAddress.Address;
186
+ }
187
+
188
+ return '';
189
+ }
190
+
191
+ function getSenderLabel(oEmail) {
192
+ let oFrom = oEmail.from || oEmail.From || oEmail.sender || oEmail.Sender || {};
193
+ if (oFrom.emailAddress && typeof oFrom.emailAddress === 'object') {
194
+ return oFrom.emailAddress.name || oFrom.emailAddress.address || 'Unknown sender';
195
+ }
196
+ if (oFrom.EmailAddress && typeof oFrom.EmailAddress === 'object') {
197
+ return oFrom.EmailAddress.Name || oFrom.EmailAddress.Address || 'Unknown sender';
198
+ }
199
+ if (typeof oFrom.displayName === 'string') {
200
+ return oFrom.displayName;
201
+ }
202
+ return getEmailFromValue(oFrom) || 'Unknown sender';
203
+ }
204
+
205
+ function getGroupTagsMarkup(oGroup) {
206
+ let aTags = Array.isArray(oGroup.groupTypes) ? oGroup.groupTypes : [];
207
+ return aTags.map((sTag) => '<span class="pill">' + escapeHtml(sTag) + '</span>').join('');
208
+ }
209
+
210
+ function renderProfile() {
211
+ if (!eProfileCard) {
212
+ return;
213
+ }
214
+
215
+ if (!oProfile || Object.keys(oProfile).length === 0) {
216
+ eProfileCard.innerHTML = '<div class="emptyState">Profile information was not returned.</div>';
217
+ if (eProfileStatus) {
218
+ eProfileStatus.textContent = 'Unavailable';
219
+ }
220
+ return;
221
+ }
222
+
223
+ let sName = oProfile.displayName || oProfile.DisplayName || oProfile.name || 'Unknown user';
224
+ let sEmail = oProfile.mail || oProfile.Mail || oProfile.userPrincipalName || oProfile.UserPrincipalName || '';
225
+ let sJobTitle = oProfile.jobTitle || oProfile.JobTitle || 'No title';
226
+ let sDepartment = oProfile.department || oProfile.Department || 'No department';
227
+ let sPhone = oProfile.mobilePhone || oProfile.MobilePhone || '';
228
+
229
+ eProfileCard.innerHTML = `
230
+ <div class="profileBadge">${escapeHtml(getInitials(sName))}</div>
231
+ <div>
232
+ <h3 class="profileName">${escapeHtml(sName)}</h3>
233
+ <p class="profileMeta">${escapeHtml(sJobTitle)} · ${escapeHtml(sDepartment)}</p>
234
+ <p class="profileSubMeta">${escapeHtml(sEmail || 'No email')}</p>
235
+ <p class="profileSubMeta">${escapeHtml(sPhone || 'No phone')}</p>
236
+ </div>
237
+ `;
238
+
239
+ if (eProfileStatus) {
240
+ eProfileStatus.textContent = 'Ready';
241
+ }
242
+ }
243
+
244
+ function renderEmails() {
245
+ if (!eEmailList) {
246
+ return;
247
+ }
248
+
249
+ if (eEmailCount) {
250
+ eEmailCount.textContent = String(aEmails.length);
251
+ }
252
+
253
+ if (!Array.isArray(aEmails) || aEmails.length === 0) {
254
+ eEmailList.innerHTML = '<div class="emptyState">No emails were returned.</div>';
255
+ return;
256
+ }
257
+
258
+ eEmailList.innerHTML = aEmails.map((oEmail) => {
259
+ let sSubject = oEmail.subject || oEmail.Subject || '(No subject)';
260
+ let sPreview = oEmail.bodyPreview || oEmail.BodyPreview || oEmail.body || oEmail.Body || '';
261
+ let sReceived = oEmail.receivedDateTime || oEmail.DateTimeReceived || oEmail.createdDateTime || '';
262
+ let sSender = getSenderLabel(oEmail);
263
+
264
+ return `
265
+ <article class="listCard">
266
+ <h3 class="listTitle">${escapeHtml(sSubject)}</h3>
267
+ <p class="listMeta">From: ${escapeHtml(sSender)} · ${escapeHtml(formatEmailDate(sReceived))}</p>
268
+ <p class="listBody">${escapeHtml(String(sPreview).slice(0, 220) || 'No preview available.')}</p>
269
+ </article>
270
+ `;
271
+ }).join('');
272
+ }
273
+
274
+ function renderGroups() {
275
+ if (!eGroupList) {
276
+ return;
277
+ }
278
+
279
+ if (eGroupCount) {
280
+ eGroupCount.textContent = String(aGroups.length);
281
+ }
282
+
283
+ if (!Array.isArray(aGroups) || aGroups.length === 0) {
284
+ eGroupList.innerHTML = '<div class="emptyState">No group memberships were returned.</div>';
285
+ return;
286
+ }
287
+
288
+ eGroupList.innerHTML = aGroups.map((oGroup) => {
289
+ let sName = oGroup.displayName || oGroup.DisplayName || 'Unnamed group';
290
+ let sDescription = oGroup.description || oGroup.Description || 'No description';
291
+ let sMail = oGroup.mail || oGroup.Mail || 'No group mailbox';
292
+
293
+ return `
294
+ <article class="listCard">
295
+ <h3 class="listTitle">${escapeHtml(sName)}</h3>
296
+ <p class="listMeta">${escapeHtml(sMail)}</p>
297
+ <p class="listBody">${escapeHtml(sDescription)}</p>
298
+ ${getGroupTagsMarkup(oGroup)}
299
+ </article>
300
+ `;
301
+ }).join('');
302
+ }
303
+
304
+ async function loadProfile() {
305
+ oProfile = getProfileFromPayload(await getMyProfile());
306
+ renderProfile();
307
+ }
308
+
309
+ async function loadEmails() {
310
+ aEmails = getArrayFromPayload(await listEmails({ folderId: 'Inbox', top: 15 }));
311
+ renderEmails();
312
+ }
313
+
314
+ async function loadGroups() {
315
+ aGroups = getArrayFromPayload(await listMyGroups());
316
+ renderGroups();
317
+ }
318
+
319
+ async function sendEmailMessage(sTo, sSubject, sBody) {
320
+ let client = getSendClient();
321
+ let aErrors = [];
322
+
323
+ for (let iIndex = 0; iIndex < SEND_EMAIL_CANDIDATES.length; iIndex += 1) {
324
+ let sName = SEND_EMAIL_CANDIDATES[iIndex];
325
+ try {
326
+ let oResult = await client.executeAsync({
327
+ connectorOperation: {
328
+ tableName: sName,
329
+ operationName: 'SendEmailV2',
330
+ parameters: {
331
+ emailMessage: {
332
+ To: sTo,
333
+ Subject: sSubject,
334
+ Body: sBody,
335
+ Importance: 'Normal',
336
+ IsHtml: true
337
+ }
338
+ }
339
+ }
340
+ });
341
+ if (oResult && oResult.success === false) {
342
+ throw new Error(oResult.error ? (oResult.error.message || JSON.stringify(oResult.error)) : 'Send failed');
343
+ }
344
+ return oResult && Object.prototype.hasOwnProperty.call(oResult, 'data') ? oResult.data : oResult;
345
+ } catch (oErr) {
346
+ let sMessage = oErr.message || String(oErr);
347
+ aErrors.push(sName + ': ' + sMessage);
348
+ if (sMessage.indexOf('Connection reference not found') === -1) {
349
+ throw oErr;
350
+ }
351
+ }
352
+ }
353
+
354
+ throw new Error('No Outlook connection reference matched. Tried: ' + aErrors.join(' || '));
355
+ }
356
+
357
+ async function loadDashboard() {
358
+ setStatus('Loading your dashboard…', 'info');
359
+
360
+ let aResults = await Promise.allSettled([
361
+ loadProfile(),
362
+ loadEmails(),
363
+ loadGroups()
364
+ ]);
365
+
366
+ let aErrors = aResults
367
+ .filter((oResult) => oResult.status === 'rejected')
368
+ .map((oResult) => oResult.reason?.message || String(oResult.reason));
369
+
370
+ renderProfile();
371
+ renderEmails();
372
+ renderGroups();
373
+
374
+ if (aErrors.length > 0) {
375
+ setStatus('Loaded with connector issues: ' + aErrors.join(' | '), 'error');
376
+ return;
377
+ }
378
+
379
+ setStatus('Dashboard loaded successfully.', 'success');
380
+ }
381
+
382
+ async function handleRefreshClick() {
383
+ await loadDashboard();
384
+ }
385
+
386
+ function handleScrollComposeClick() {
387
+ let eComposeSection = getElement('composeSection');
388
+ if (eComposeSection) {
389
+ eComposeSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
390
+ }
391
+ }
392
+
393
+ async function handleComposeSubmit(oEvent) {
394
+ oEvent.preventDefault();
395
+
396
+ let sTo = String(eComposeTo?.value || '').trim();
397
+ let sSubject = String(eComposeSubject?.value || '').trim();
398
+ let sBody = String(eComposeBody?.value || '').trim();
399
+
400
+ if (!sTo || !sSubject || !sBody) {
401
+ setStatus('Complete the To, Subject, and Message fields before sending.', 'error');
402
+ return;
403
+ }
404
+
405
+ try {
406
+ if (eSendButton) {
407
+ eSendButton.disabled = true;
408
+ eSendButton.textContent = 'Sending...';
409
+ }
410
+
411
+ setStatus('Sending email…', 'info');
412
+ await sendEmailMessage(sTo, sSubject, sBody);
413
+
414
+ if (eComposeForm) {
415
+ eComposeForm.reset();
416
+ }
417
+
418
+ setStatus('Email sent successfully.', 'success');
419
+ await loadEmails();
420
+ } catch (oErr) {
421
+ setStatus('Send email failed: ' + (oErr.message || oErr), 'error');
422
+ } finally {
423
+ if (eSendButton) {
424
+ eSendButton.disabled = false;
425
+ eSendButton.textContent = 'Send email';
426
+ }
427
+ }
428
+ }
429
+
430
+ function attachEvents() {
431
+ if (eRefreshButton) {
432
+ eRefreshButton.addEventListener('click', () => {
433
+ handleRefreshClick().catch((oErr) => {
434
+ setStatus('Refresh failed: ' + (oErr.message || oErr), 'error');
435
+ });
436
+ });
437
+ }
438
+
439
+ if (eScrollComposeButton) {
440
+ eScrollComposeButton.addEventListener('click', handleScrollComposeClick);
441
+ }
442
+
443
+ if (eComposeForm) {
444
+ eComposeForm.addEventListener('submit', (oEvent) => {
445
+ handleComposeSubmit(oEvent).catch((oErr) => {
446
+ setStatus('Send email failed: ' + (oErr.message || oErr), 'error');
447
+ });
448
+ });
449
+ }
450
+ }
451
+
452
+ async function boot() {
453
+ cacheDomElements();
454
+ attachEvents();
455
+ renderProfile();
456
+ renderEmails();
457
+ renderGroups();
458
+
459
+ try {
460
+ await loadDashboard();
461
+ } catch (oErr) {
462
+ setStatus('App failed to start: ' + (oErr.message || oErr), 'error');
463
+ }
464
+ }
465
+
466
+ window.addEventListener('DOMContentLoaded', () => {
467
+ boot().catch((oErr) => {
468
+ setStatus('App failed to start: ' + (oErr.message || oErr), 'error');
469
+ });
470
+ });