-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_extension_input.js
647 lines (572 loc) · 22 KB
/
ui_extension_input.js
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) 4me inc. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* https://github.com/code4me/4me-uix-office365-meeting/blob/master/LICENSE.txt
* -------------------------------------------------------------------------------------------
*/
var $ = ITRP.$; // jQuery
var $extension = $(this); // The UI Extension container with custom HTML
console.log( "MSAL TEST 3.0" );
console.log(JSON.stringify(ITRP.context));
console.log(JSON.stringify(ITRP.record));
var msalInstance;
var username;
var authenticationID;
var userEmail;
var accessToken;
// Config ------------------------------------------------------------------------
var useRooms = false;
var startDate = new Date();
startDate.setDate(new Date().getDate() + 1); // Tomorrow
var endDate = new Date();
endDate.setDate(new Date().getDate() + 11); // One week timeframe
var startDateISO = startDate.toISOString().slice(0, -1);
var endDateISO = endDate.toISOString().slice(0, -1);
// -------------------------------------------------------------------------------
//const msal_src = "https://alcdn.msauth.net/browser/2.1.0/js/msal-browser.min.js";
//const msal_sha = "sha384-EmYPwkfj+VVmL1brMS1h6jUztl4QMS8Qq8xlZNgIT/luzg7MAzDVrRa2JxbNmk/e";
const msal_src = "https://alcdn.msauth.net/browser/2.1.0/js/msal-browser.js";
const msal_sha = "sha384-M9bRB06LdiYadS+F9rPQnntFCYR3UJvtb2Vr4Tmhw9WBwWUfxH8VDRAFKNn3VTc/";
const useSSO = true;
const msalConfig = {
auth: {
clientId: "7ac3ce20-7a5c-400f-9618-6b0f25a433b8",
authority: "https://login.microsoftonline.com/hdt-software.com",
redirectUri: window.location.origin + "/404.html",
navigateToLoginRequestUrl: false,
},
cache: {
cacheLocation: "localStorage", // This configures where your cache will be stored - sessionStorage or localStorage
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
}
};
const graphConfig = {
meEndpoint: "https://graph.microsoft.com/v1.0/me",
findRoomsEndpoint: "https://graph.microsoft.com/beta/me/findRooms",
findMeetingTimesEndpoint: "https://graph.microsoft.com/beta/me/findMeetingTimes",
eventEndpoint: "https://graph.microsoft.com/beta/me/events",
onlineMeetings: "https://graph.microsoft.com/beta/me/onlineMeetings",
};
const loginRequest = {
loginHint: null,
scopes: ["User.Read"],
};
const tokenRequest = {
account: null,
scopes: ["User.Read"],
forceRefresh: true, // Set this to "true" to skip a cached token and go to the server to get a new token
};
const meetingTimeSuggestionsRequest = {
attendees: [],
locationConstraint: {
isRequired: "false",
suggestLocation: "false",
locations: []
},
timeConstraint: {
activityDomain:"work",
timeslots: [
{
start: {
dateTime: startDateISO,
timeZone: "UTC"
},
end: {
dateTime: endDateISO,
timeZone: "UTC"
}
}
]
},
isOrganizerOptional: "false",
meetingDuration: "PT1H",
returnSuggestionReasons: "true",
minimumAttendeePercentage: "100"
};
const onlineMeetingsRequest = {
startDateTime:"",
endDateTime:"",
subject:"Meeting"
};
const createEventRequest = {
subject: null,
body: {
contentType: "HTML",
content: null
},
start: {
dateTime: null,
timeZone: null
},
end: {
dateTime: null,
timeZone: null
},
location: {
displayName:null
},
attendees: [
/*{
emailAddress: {
address:"[email protected]",
name: "Samantha Booth"
},
"type": "required"
}*/
],
"allowNewTimeProposals": true,
//"transactionId":"7E163156-7762-4BEB-A1C6-729EA81755A7"
};
function addAttendee ( email ) {
if (email != null) {
var attendee = {
type: "required",
emailAddress: {
address: email
}
};
meetingTimeSuggestionsRequest.attendees.push(attendee);
}
}
function callMSGraph (graphEndpoint, accessToken, callback, failureCallback) {
var headers = new Headers({
'Content-Type': 'application/json'
});
var bearer = "Bearer " + accessToken;
headers.append("Authorization", bearer);
var options = {
method: "GET",
headers: headers
};
fetch(graphEndpoint, options).then(function (response) {
if (response.ok) return response.json();
failureCallback(response.json());
}).then(function (json) {
//do something with response
callback(json, accessToken);
}).catch(failureCallback);
}
function postMSGraph (graphEndpoint, accessToken, payload, callback, failureCallback) {
var headers = new Headers({
'Content-Type': 'application/json'
});
var bearer = "Bearer " + accessToken;
headers.append("Authorization", bearer);
var options = {
method: "POST",
headers: headers,
body: JSON.stringify(payload)
};
fetch(graphEndpoint, options).then(function (response) {
return response.text();
}).then(function (text) {
if (text.length) {
var json = JSON.parse(text);
if (json.error) {
failureCallback(json.error);
} else {
callback(json, accessToken);
}
} else {
callback(null, accessToken);
}
}).catch(failureCallback);
}
function ValidateCachedMSALToken() {
var timestamp = Math.floor((new Date()).getTime() / 1000);
for (var _i = 0, _a = Object.keys(localStorage); _i < _a.length; _i++) {
var key = _a[_i];
if (key.includes('accesstoken')) {
var val = JSON.parse(localStorage.getItem(key));
if (val.expiresOn) {
console.log("Access Token Expiration Dates: " + new Date(val.expiresOn * 1000) + " - " + new Date(val.extendedExpiresOn * 1000));
// We have a (possibly expired) token
if (val.expiresOn < timestamp) {
console.warn("Access Token has expired!");
}
return;
}
}
}
throw new Error('No valid token found');
}
function msalCallback(successCallback) {
console.log( "msal-browser.js Loaded" );
// Create the main msal instance
// configuration parameters are located at msalConfig.js
msalInstance = new msal.PublicClientApplication(msalConfig);
/*
// Handle the redirect flows
msalInstance.handleRedirectPromise().then(function (tokenResponse) {
// Handle redirect response
console.log("handleRedirectPromise tokenResponse: " + JSON.stringify(tokenResponse));
}).catch(function (error) {
// Handle redirect error
console.error(error);
onError(error);
});
*/
const currentAccounts = msalInstance.getAllAccounts();
if (currentAccounts === null) {
console.warn("No Account registered yet");
} else if (currentAccounts.length > 1) {
// Add choose account code here
console.warn("Multiple accounts detected.");
} else if (currentAccounts.length === 1) {
console.log("Using Account: " + currentAccounts[0].username);
//console.log(JSON.stringify(currentAccounts[0]));
username = currentAccounts[0].username;
loginRequest.loginHint = currentAccounts[0].username;
tokenRequest.account = currentAccounts[0];
try {
var token = ValidateCachedMSALToken();
} catch (error) {
console.warn("MSALToken: " + error);
}
acquireToken(successCallback);
return;
}
doLogin(successCallback);
}
function doLogin(successCallback) {
if (loginRequest.loginHint == null) {
getCurrentUser(function() { doLoginPopup(successCallback); });
} else {
doLoginPopup(successCallback);
}
}
function doLoginPopup(successCallback) {
if (useSSO == false) {
console.log("SSO is Off");
console.log("loginHint: " + loginRequest.loginHint);
msalInstance.loginPopup(loginRequest).then(function(loginResponse) { acquireTokenFromLoginResponse(loginResponse, successCallback); })
.catch(function (error) {
console.error("login error: " + error);
});
} else {
console.log("ssoSilent: " + loginRequest.loginHint);
msalInstance.ssoSilent(loginRequest).then(function(loginResponse) { acquireTokenFromLoginResponse(loginResponse, successCallback); })
.catch(function (error) {
console.warn("ssoSilent: " + error);
if (error instanceof msal.InteractionRequiredAuthError) {
msalInstance.loginPopup(loginRequest)
.then(function(loginResponse) { acquireTokenFromLoginResponse(loginResponse, successCallback); })
.catch(function (error) {
console.warn("ssoSilent: " + error);
onError(error);
});
} else {
onError(error);
}
}).catch(function (error) {
console.error("loginPopup: " + error);
onError(error);
});
}
}
function acquireTokenFromLoginResponse( loginResponse, successCallback ) {
console.log("Getting Account from Login Response");
//console.log(JSON.stringify(loginResponse));
tokenRequest.account = loginResponse.account;
acquireToken(successCallback);
}
function acquireToken(successCallback) {
console.log("Acquiring Token");
msalInstance.acquireTokenSilent(tokenRequest).then(function(tokenResponse) { handleTokenResponse(tokenResponse, successCallback); })
.catch(function (error) {
console.warn("silent token acquisition fails. acquiring token using redirect");
console.log("acquireTokenSilent: " + error);
if (error instanceof msal.InteractionRequiredAuthError) {
// fallback to interaction when silent call fails
msalInstance.acquireTokenPopup(tokenRequest).then(function(tokenResponse) { handleTokenResponse(tokenResponse, successCallback); });
} else {
doLogin();
}
}).catch(function (error) {
console.log("acquireToken: " + error);
onError(error);
});
}
function handleTokenResponse ( tokenResponse, successCallback ) {
//console.log(tokenResponse.accessToken);
accessToken = tokenResponse.accessToken;
// Check if the tokenResponse is null
// If the tokenResponse !== null, then you are coming back from a successful authentication redirect.
// If the tokenResponse === null, you are not coming back from an auth redirect.
//}.catch(function (error) {
// handle error, either in the library or coming back from the server
//getUser(ITRP.record, function(ITRP.record.member.id);
successCallback(accessToken);
}
function findMeetingTimes(rooms, accessToken) {
if (rooms) {
$.each(rooms.value, function( i, r ) {
var loc = {
"resolveAvailability": true,
"displayName": r.name,
"locationEmailAddress": r.address
};
meetingTimeSuggestionsRequest.locationConstraint.locations.push(loc);
});
}
postMSGraph(graphConfig.findMeetingTimesEndpoint, accessToken, meetingTimeSuggestionsRequest, graphAPICallback, onError);
}
function onDemandScript( url, nonce, callback, successCallback ) {
callback = (typeof callback != 'undefined') ? callback : {};
$.ajax({
type: "GET",
url: url,
attrs: { nonce: nonce, crossorigin: 'anonymous' }, // Important: crossorigin needed for ssoSilent to work
success: function() { callback(successCallback); },
dataType: "script",
cache: true
});
}
function getCurrentUser( callback ) {
var data = {
url: '/v1/me',
type: 'GET',
dataType: 'json',
success: function(jsondata) {
userEmail = jsondata.primary_email;
authenticationID = jsondata.authenticationID;
loginRequest.loginHint = userEmail || authenticationID;
callback();
},
error: onError,
};
$.ajax(data);
}
function getUserReq(ids, callback) {
var filtered = ids.filter(function (el) {
return el != null && el != '';
});
var data = {
url: '/v1/people?ids=' + filtered.join(',') + '&fields=primary_email',
type: 'GET',
dataType: 'json',
success : callback,
error: onError
};
$.ajax(data);
}
function onError(context)
{
console.warn("onError: " + context);
if (context.code == "ErrorItemNotFound") {
cancelMeeting();
} else if (context.message) {
errorMsg.html(context.message);
} else {
errorMsg.html(context);
}
interactionOn(false);
}
const dateDisplayOptions = { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' };
function isSameDay(d1, d2) {
return d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate();
}
function isPreferredTimeframe(d, ptf) {
return (ptf === 'morning' && d.getHours() <= 13) || (ptf === 'afternoon' && d.getHours() > 13);
}
function meetingSlotTemplate( i, slot ) {
var room = slot.locations[0];
var roomDisplayName = '';
var slotDate = new Date(slot.meetingTimeSlot.start.dateTime + 'Z');
var preferedDate = $('#preferred_date').dateEntry('getDate');
var preferredTimeframe = $('#preferred_timeframe').val();
if (room) { roomDisplayName = room.displayName; }
var html = '<div class="list-group-item meetingTimeSuggestion row">';
html = html + '<input type="radio" name="slot" value="' + i + '">';
html = html + '<div class="slotDate">';
html = html + '<p class="list-group-item-text">' + slotDate.toLocaleString(undefined, dateDisplayOptions) + '</p>';
if (isSameDay(slotDate, preferedDate)) {
html = html + '<span class="preferedDate">Prefered Date</span>';
}
if (isPreferredTimeframe(slotDate, preferredTimeframe)) {
html = html + '<span class="preferedTime">Prefered Time</span>';
}
html = html + '</div>';
html = html + '<div class="room">' + roomDisplayName + '</div>';
html = html + '</div>';
return html;
}
var scheduleButton = $extension.find('div.schedule-appointment');
var confirmAppointmentButton = $extension.find('div.confirm-appointment');
var cancelAppointmentButton = $extension.find('#cancel_appointment');
var loading_spinner = $extension.find('#loading_spinner');
var meetingSlots = $extension.find('#meetingSlots');
var errorMsg = $extension.find('#errorMsg');
var EventHtml = $extension.find('#EventHtml');
var meetingDurationHolder = $extension.find('#meetingDurationHolder');
var meetingTimeSuggestions;
var selectedSlot;
function graphAPICallback(data) {
//alert(JSON.stringify(data, null, 2));
$('#json').append(JSON.stringify(data, null, 2));
var $meetingSlots = $extension.find('#meetingSlots');
if (data.meetingTimeSuggestions.length === 0) {
if (data.emptySuggestionsReason == 'AttendeesUnavailable') {
errorMsg.html("Some attendees are not available and we couldn’t find any suitable meeting suggestions.<br>We apology for the inconvenience and kindly ask you to proceed with scheduling the appointment manually.");
} else if (data.emptySuggestionsReason == 'AttendeesUnavailableOrUnknown') {
errorMsg.html("Some attendees have unknown availability. Attendee availability can become unknown if the attendee is outside of the organization, or if there is an error obtaining free/busy information.<br> We apology for the inconvenience and kindly ask you to proceed with scheduling the appointment manually.");
} else if (data.emptySuggestionsReason == 'LocationsUnavailable') {
errorMsg.html("There are no suitable locations available at the calculated time slots. You could try to select a diferent location or proceed with scheduling the appointment manually.");
} else if (data.emptySuggestionsReason == 'OrganizerUnavailable') {
errorMsg.html("The organizer is not available during the requested time window.");
} else {
errorMsg.html("An unknown error prevented us from retrieving any suitable suggestions.<br>We apology for the inconvenience and kindly ask you to proceed with scheduling the appointment manually.");
}
interactionOn(false);
} else {
meetingDurationHolder.hide();
meetingTimeSuggestions = data.meetingTimeSuggestions;
$.each(data.meetingTimeSuggestions, function( i, slot ) {
$meetingSlots.append(meetingSlotTemplate(i, slot));
});
interactionOn(true);
}
}
scheduleButton.on('click', function() {
console.log('Calling meetingTimeSuggestions API');
interactionOff();
var detail_content = JSON.parse($('#edit_content > .detail_content').attr('data'));
//console.log(detail_content.customLinkParameters['person.primary_email']);
var ids = [$('#req_requested_by_id').val(), $('#req_requested_for_id').val(), $('#req_member_id').val()];
meetingTimeSuggestionsRequest.attendees = [];
meetingTimeSuggestionsRequest.meetingDuration = $("input[type='radio'][name='meetingDuration']:checked").val();
getUserReq(ids, function (jsondata) {
//console.log(JSON.stringify(jsondata));
$.each(jsondata, function( i, people ) {
addAttendee(people.primary_email);
});
addAttendee(detail_content.customLinkParameters['person.primary_email']);
console.log(JSON.stringify(meetingTimeSuggestionsRequest.attendees));
onDemandScript(msal_src, msal_sha, msalCallback, function(accessToken){
if (meetingTimeSuggestionsRequest.attendees.length >= 2) {
if (useRooms) {
callMSGraph(graphConfig.findRoomsEndpoint, accessToken, findMeetingTimes, onError);
} else {
postMSGraph(graphConfig.findMeetingTimesEndpoint, accessToken, meetingTimeSuggestionsRequest, graphAPICallback, onError);
}
} else {
onError("Some attendees information is not available.");
}
});
});
});
meetingSlots.on('change', function() {
selectedSlot = $('input[name=slot]:checked', '#meetingSlots').val();
$('#json').append(JSON.stringify(selectedSlot, null, 2));
if (selectedSlot != null) {
confirmAppointmentButton.css("display", "inline-block");
}
});
confirmAppointmentButton.on('click', function() {
interactionOff();
var slot = meetingTimeSuggestions[selectedSlot];
//console.log(JSON.stringify(slot));
createEventRequest.subject = 'Request #' + ITRP.record.initialValues.id + ' ' + $('#req_subject').val();
createEventRequest.start = slot.meetingTimeSlot.start;
createEventRequest.end = slot.meetingTimeSlot.end;
$.each(slot.attendeeAvailability, function( i, attendeeAvailability ) {
createEventRequest.attendees.push(
{
emailAddress: attendeeAvailability.attendee.emailAddress,
type: "required"
}
);
});
if ($('#modality').val() == "ms_teams") {
createEventRequest.isOnlineMeeting = true;
createEventRequest.onlineMeetingProvider = "teamsForBusiness";
}
createEventRequest.body.content = $extension.find('#invite_message').val();
postMSGraph(graphConfig.eventEndpoint, accessToken, createEventRequest, function (data)
{
console.log(JSON.stringify(data));
$("#meetingId").val(data.id);
//ITRP.field('note').val({ html: data.body.content });
// Meeting Details
var body = '<p><b>Start Time:</b> ' + new Date(data.start.dateTime + 'Z').toLocaleString(undefined, dateDisplayOptions) + '<br>';
body = body + '<b>End Time:</b> ' + new Date(data.end.dateTime + 'Z').toLocaleString(undefined, dateDisplayOptions) + '<br>';
body = body + '<b>Organizer:</b> ' + data.organizer.emailAddress.name + '<br>';
$.each(data.attendees, function( i, attendee ) {
if (i == 1) {
body = body + '<b>Attendees:</b> ' + attendee.emailAddress.name + '<br>';
} else {
body = body + ' ' + attendee.emailAddress.name + '<br>';
}
});
if (data.onlineMeeting) {
body = body + '<a href="' + data.onlineMeeting.joinUrl + '">Click here to join the meeting</a><br>';
}
body = body + '</p>';
$("#meeting").val({ html: body });
EventHtml.html(body + data.body.content);
$("#meetingHtml").val(body + data.body.content);
meetingSlots.hide();
interactionOn(true);
confirmAppointmentButton.hide();
$("#meeting-scheduler-wrapper").css("display", "none");
}, onError);
});
cancelAppointmentButton.on('click', function() {
interactionOff();
var mid = $("#meetingId").val();
if (mid) {
onDemandScript(msal_src, msal_sha, msalCallback, function(accessToken) {
postMSGraph(graphConfig.eventEndpoint + '/' + mid + '/cancel', accessToken, { Comment: "Appointment canceled." }, function (data) {
cancelMeeting();
}, onError);
});
}
});
function cancelMeeting() {
$("#meetingId").val("");
$("#meeting").val("");
$("#meetingHtml").val("");
EventHtml.html(null);
meetingSlots.hide();
interactionOn(true);
confirmAppointmentButton.hide();
$("#meeting-scheduler-wrapper").css("display", "none");
cancelAppointmentButton.css("display", "none");
}
function interactionOff()
{
errorMsg.hide();
scheduleButton.disabled = true;
confirmAppointmentButton.disabled = true;
loading_spinner.show();
}
function interactionOn(success)
{
loading_spinner.hide();
if (success) {
scheduleButton.hide();
} else {
errorMsg.show();
}
scheduleButton.disabled = false;
confirmAppointmentButton.disabled = false;
}
ITRP.hooks.register('after-prefill', function() {
//console.log($("#req_member_id").val());
//console.log($("#meetingId").val());
EventHtml.html($("#meetingHtml").val());
if ($("#req_member_id").val() && !($("#meetingId").val())) {
$("#meeting-scheduler-wrapper").css("display", "block");
scheduleButton.css("display", "inline-block");
}
// if a meeting ID is set we display the cancel button
if ($("#meetingId").val()) {
cancelAppointmentButton.css("display", "inline-block");
}
// Always hide in edit mode
errorMsg.hide();
$("#scheduled-meeting-details").css("display", "none");
});