Skip to content

Commit

Permalink
Fixed realoding wire conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJaroschewskiCONPORT committed Jun 25, 2024
1 parent ac0c2a6 commit 97c3053
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 207 deletions.
217 changes: 152 additions & 65 deletions src/calendarIntegration/addMeetingLink.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,171 @@
/* global Office, console */

import { appendToBody, getMailboxItemSubject, getOrganizer, setLocation } from "../utils/mailbox";
import { createMeetingSummary as createMeetingSummary } from "./createMeetingSummary";
import { appendToBody, getBody, getLocation, getMailboxItemSubject, getOrganizer, setLocation } from "../utils/mailbox";
import { createMeetingSummary } from "./createMeetingSummary";
import { setCustomPropertyAsync, getCustomPropertyAsync } from "../utils/customProperties";
import { showNotification, removeNotification } from "../utils/notifications";
import { isOutlookCalIntegrationEnabled } from "./isOutlookCalIntegrationEnabled";
import { createEvent } from "./createEvent";
import { mailboxItem } from "../commands/commands";
import { EventResult } from "../types/EventResult";

const defaultSubjectValue = "New Appointment";
let createdMeeting: EventResult;

export async function addMeetingLink(event: Office.AddinCommands.Event) {
/**
* Checks if the feature is enabled by calling isOutlookCalIntegrationEnabled.
*
* @return {Promise<boolean>} Whether the feature is enabled or not.
*/
async function isFeatureEnabled(): Promise<boolean> {
const isEnabled = await isOutlookCalIntegrationEnabled();

if (!isEnabled) {
console.log("Outlook calendar integration is disabled for this team. Contact your Wire system administrator.");
removeNotification("wire-for-outlook-disabled");
showNotification(
"wire-for-outlook-disabled",
"Wire for Outlook is disabled for your team. Please contact your Wire system administrator.",
Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage
);
return false;
}
return true;
}

/**
* Fetches custom properties from the mailbox item and sets the createdMeeting object if both wireId and wireLink are present.
*
* @return {Promise<void>} A promise that resolves when the custom properties are fetched and the createdMeeting object is set.
*/
async function fetchCustomProperties(): Promise<void> {
const wireId = await getCustomPropertyAsync(mailboxItem, "wireId");
const wireLink = await getCustomPropertyAsync(mailboxItem, "wireLink");

if (wireId && wireLink) {
createdMeeting = { id: wireId, link: wireLink } as EventResult;
}
}

/**
* Creates a new meeting by calling the createEvent function with a subject obtained from the mailboxItem.
* If the eventResult is not null, it sets the createdMeeting object to eventResult, updates the meeting details,
* and sets the custom properties wireId and wireLink on the mailboxItem.
*
* @return {Promise<void>} A promise that resolves when the new meeting is created and the custom properties are set.
*/
async function createNewMeeting(): Promise<void> {
removeNotification("adding-wire-meeting");
showNotification(
"adding-wire-meeting",
"Adding Wire meeting...",
Office.MailboxEnums.ItemNotificationMessageType.ProgressIndicator
);

const subject = await getMailboxItemSubject(mailboxItem);
const eventResult = await createEvent(subject || defaultSubjectValue);

if (eventResult) {
createdMeeting = eventResult;
await updateMeetingDetails(eventResult);
await setCustomPropertyAsync(mailboxItem, "wireId", eventResult.id);
await setCustomPropertyAsync(mailboxItem, "wireLink", eventResult.link);
}

removeNotification("adding-wire-meeting");
}

/**
* Updates the meeting details by setting the location and appending the meeting summary to the body of the mailbox item.
*
* @param {EventResult} eventResult - The event result containing the link for the meeting.
* @return {Promise<void>} A promise that resolves when the meeting details are updated.
*/
async function updateMeetingDetails(eventResult: EventResult): Promise<void> {
getOrganizer(mailboxItem, async (organizer) => {
await setLocation(mailboxItem, eventResult.link, () => {});
const meetingSummary = createMeetingSummary(eventResult.link, organizer);
await appendToBody(mailboxItem, meetingSummary);
});
}

/**
* Handles an existing meeting by updating meeting details and setting custom properties.
*
* @return {Promise<void>} A promise that resolves when the existing meeting is handled.
*/
async function handleExistingMeeting(): Promise<void> {
if (!createdMeeting) {
throw new Error("createdMeeting is undefined");
}

const currentBody = await getBody(mailboxItem);
const currentLocation = await getLocation(mailboxItem);
const normalizedCurrentBody = currentBody.replace(/&amp;/g, "&");
const normalizedMeetingLink = createdMeeting.link?.replace(/&amp;/g, "&");

getOrganizer(mailboxItem, async (organizer) => {
if (!currentLocation) {
await setLocation(mailboxItem, createdMeeting.link, () => {});
}
const meetingSummary = createMeetingSummary(createdMeeting.link, organizer);
if (!normalizedCurrentBody.includes(normalizedMeetingLink)) {
await appendToBody(mailboxItem, meetingSummary);
}
});

await setCustomPropertyAsync(mailboxItem, "wireId", createdMeeting.id);
await setCustomPropertyAsync(mailboxItem, "wireLink", createdMeeting.link);
}

/**
* Adds a meeting link to the Outlook calendar.
*
* @param {Office.AddinCommands.Event} event - The event object.
* @return {Promise<void>} A promise that resolves when the meeting link is added.
*/
async function addMeetingLink(event: Office.AddinCommands.Event): Promise<void> {
try {
const isFeatureEnabled = await isOutlookCalIntegrationEnabled();

if (!isFeatureEnabled) {
console.log(
"There is no Outlook calendar integration enabled for this team. Please contact your Wire system administrator."
);

removeNotification("wire-for-outlook-disabled");
showNotification(
"wire-for-outlook-disabled",
"Wire for Outlook is disabled for your team. Please contact your Wire system administrator.",
Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage
);
const isEnabled = await isFeatureEnabled();
if (!isEnabled) return;

await fetchCustomProperties();
if (!createdMeeting) {
await createNewMeeting();
} else {
console.log("Outlook calendar integration feature is enabled for this team.");

const wireId = await getCustomPropertyAsync(mailboxItem, "wireId");
if (!wireId) {
console.log("There is no Wire meeting for this Outlook meeting, starting process of creating it...");
removeNotification("adding-wire-meeting");
showNotification(
"adding-wire-meeting",
"Adding Wire meeting...",
Office.MailboxEnums.ItemNotificationMessageType.ProgressIndicator
);
const subject = await getMailboxItemSubject(mailboxItem);
const eventResult = await createEvent(subject || defaultSubjectValue);
if (eventResult) {
getOrganizer(mailboxItem, function (organizer) {
setLocation(mailboxItem, eventResult.link, () => {});
const meetingSummary = createMeetingSummary(eventResult.link, organizer);
appendToBody(mailboxItem, meetingSummary);
});
await setCustomPropertyAsync(mailboxItem, "wireId", eventResult.id);
await setCustomPropertyAsync(mailboxItem, "wireLink", eventResult.link);
}
removeNotification("adding-wire-meeting");
} else {
console.log("Wire meeting is already created for this Outlook meeting");
removeNotification("wire-meeting-exists");
showNotification(
"wire-meeting-exists",
"Wire meeting is already created for this Outlook meeting",
Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage
);
}
await handleExistingMeeting();
}
} catch (error) {
console.error("Error during adding Wire meeting link", error);
handleAddMeetingLinkError(error);
} finally {
event.completed();
}
}

removeNotification("adding-wire-meeting");
removeNotification("adding-wire-meeting-error");
/**
* Handles errors that occur when adding a meeting link.
*
* @param {Error} error - The error that occurred.
* @return {void} This function does not return anything.
*/
function handleAddMeetingLinkError(error: Error): void {
removeNotification("adding-wire-meeting");
removeNotification("adding-wire-meeting-error");

if (error.message.includes("authorization failed")) {
showNotification(
"adding-wire-meeting-error",
"Authorization failed.",
Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage
);
} else {
showNotification(
"adding-wire-meeting-error",
"There was error while adding wire meeting",
Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage
);
}
if (error.message.includes("authorization failed")) {
showNotification(
"adding-wire-meeting-error",
"Authorization failed.",
Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage
);
} else {
showNotification(
"adding-wire-meeting-error",
"There was an error while adding the Wire meeting.",
Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage
);
}

event.completed();
}

export { addMeetingLink };
68 changes: 34 additions & 34 deletions src/calendarIntegration/isOutlookCalIntegrationEnabled.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/* global console */

import { FeatureConfigsResponse, Feature } from "../types/FeatureConfigsResponse";
import { config } from "../utils/config";
import { fetchWithAuthorizeDialog } from "../wireAuthorize/wireAuthorize";

export async function isOutlookCalIntegrationEnabled() {
try {
const response = await fetchWithAuthorizeDialog(new URL(`${config.apiVersion}/feature-configs`, config.apiBaseUrl), {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});

if (response.ok) {
const data: FeatureConfigsResponse = await response.json();
const outlookCalIntegration: Feature | undefined = data.outlookCalIntegration;
if (outlookCalIntegration && outlookCalIntegration.status === "enabled") {
return true;
}
} else {
const errorMsg = `Error while fetching outlookCalIntegration feature config. Status code: ${response.status}`;
console.error(errorMsg);
throw new Error(errorMsg);
}
} catch (error) {
const errorMsg = `Error while checking outlookCalIntegration feature config: ${error}`;
console.error(errorMsg);
throw new Error(errorMsg);
}

return false;
}
/* global console */

import { FeatureConfigsResponse, Feature } from "../types/FeatureConfigsResponse";
import { config } from "../utils/config";
import { fetchWithAuthorizeDialog } from "../wireAuthorize/wireAuthorize";

export async function isOutlookCalIntegrationEnabled() {
try {
const response = await fetchWithAuthorizeDialog(new URL(`${config.apiVersion}/feature-configs`, config.apiBaseUrl), {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});

if (response.ok) {
const data: FeatureConfigsResponse = await response.json();
const outlookCalIntegration: Feature | undefined = data.outlookCalIntegration;
if (outlookCalIntegration && outlookCalIntegration.status === "enabled") {
return true;
}
} else {
const errorMsg = `Error while fetching outlookCalIntegration feature config. Status code: ${response.status}`;
console.error(errorMsg);
throw new Error(errorMsg);
}
} catch (error) {
const errorMsg = `Error while checking outlookCalIntegration feature config: ${error}`;
console.error(errorMsg);
throw new Error(errorMsg);
}

return false;
}
Loading

0 comments on commit 97c3053

Please sign in to comment.