codeapp-js 0.1.0 → 0.2.1

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