From 54827cc322619ee762b9c9b6e9b6039982734980 Mon Sep 17 00:00:00 2001 From: Kevin Jaroschewski Date: Wed, 26 Jun 2024 08:25:11 +0200 Subject: [PATCH 1/4] Point 1 & 2 --- src/taskpane/components/LoggedIn.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/taskpane/components/LoggedIn.tsx b/src/taskpane/components/LoggedIn.tsx index f9f96d5..e2f77f8 100644 --- a/src/taskpane/components/LoggedIn.tsx +++ b/src/taskpane/components/LoggedIn.tsx @@ -15,7 +15,7 @@ const LoggedIn: React.FC = ({ user, onLogout }) => ( Username: {user.handle}
E-mail: {user.email}

- Log out + Disconnect Add-in ); From ea461654d6d36bca3789c77d4379be3fc24a9af3 Mon Sep 17 00:00:00 2001 From: "kevin.jaroschewski" Date: Wed, 26 Jun 2024 08:37:16 +0200 Subject: [PATCH 2/4] renamed logout, refactored and added subject handler --- src/calendarIntegration/addMeetingLink.ts | 46 +++++++++++++++++-- src/utils/mailbox.ts | 56 +++++++++++++++++------ 2 files changed, 84 insertions(+), 18 deletions(-) diff --git a/src/calendarIntegration/addMeetingLink.ts b/src/calendarIntegration/addMeetingLink.ts index f8bef16..85d5ab2 100644 --- a/src/calendarIntegration/addMeetingLink.ts +++ b/src/calendarIntegration/addMeetingLink.ts @@ -1,6 +1,6 @@ /* global Office, console */ -import { appendToBody, getBody, getLocation, getMailboxItemSubject, getOrganizer, setLocation } from "../utils/mailbox"; +import { appendToBody, getBody, getLocation, getMailboxItemSubject, getMeetingTime, getOrganizer, setLocation, setSubject } from "../utils/mailbox"; import { createMeetingSummary } from "./createMeetingSummary"; import { setCustomPropertyAsync, getCustomPropertyAsync } from "../utils/customProperties"; import { showNotification, removeNotification } from "../utils/notifications"; @@ -47,6 +47,31 @@ async function fetchCustomProperties(): Promise { } } +/** + * Creates a new subject for the mailbox item by getting the meeting date and appending it to the existing subject. + * + * @return {Promise} A promise that resolves when the new subject is set on the mailbox item. + */ +async function createSubject() { + const getMeetingDate = await getMeetingTime(mailboxItem); + const subject = `CLDR::${getMeetingDate}`; + + await setSubject(mailboxItem, subject); +} + +/** + * Appends the meeting date to the current subject of the mailbox item. + * + * @param {string} currentSubject - The current subject of the mailbox item. + * @return {Promise} A promise that resolves when the subject is appended. + */ +async function appendSubject(currentSubject:string){ + const getMeetingDate = await getMeetingTime(mailboxItem); + const newSubject = `${currentSubject} - CLDR::${getMeetingDate}`; + + await setSubject(mailboxItem, newSubject); +} + /** * 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, @@ -63,10 +88,12 @@ async function createNewMeeting(): Promise { ); const subject = await getMailboxItemSubject(mailboxItem); + const eventResult = await createEvent(subject || defaultSubjectValue); if (eventResult) { createdMeeting = eventResult; + subject ? appendSubject(subject) : createSubject(); await updateMeetingDetails(eventResult); await setCustomPropertyAsync(mailboxItem, "wireId", eventResult.id); await setCustomPropertyAsync(mailboxItem, "wireLink", eventResult.link); @@ -75,6 +102,7 @@ async function createNewMeeting(): Promise { removeNotification("adding-wire-meeting"); } + /** * Updates the meeting details by setting the location and appending the meeting summary to the body of the mailbox item. * @@ -83,7 +111,7 @@ async function createNewMeeting(): Promise { */ async function updateMeetingDetails(eventResult: EventResult): Promise { getOrganizer(mailboxItem, async (organizer) => { - await setLocation(mailboxItem, eventResult.link, () => {}); + await setLocation(mailboxItem, eventResult.link); const meetingSummary = createMeetingSummary(eventResult.link, organizer); await appendToBody(mailboxItem, meetingSummary); }); @@ -100,18 +128,30 @@ async function handleExistingMeeting(): Promise { } const currentBody = await getBody(mailboxItem); + const currentSubject = await getMailboxItemSubject(mailboxItem); const currentLocation = await getLocation(mailboxItem); const normalizedCurrentBody = currentBody.replace(/&/g, "&"); const normalizedMeetingLink = createdMeeting.link?.replace(/&/g, "&"); getOrganizer(mailboxItem, async (organizer) => { + + //Check if subject is empty + if(!currentSubject.includes("CLDR::")){ + currentSubject ? appendSubject(currentSubject) : createSubject(); + } + + //Check if location is empty if (!currentLocation) { - await setLocation(mailboxItem, createdMeeting.link, () => {}); + await setLocation(mailboxItem, createdMeeting.link); } + const meetingSummary = createMeetingSummary(createdMeeting.link, organizer); + + //Check if body is empty if (!normalizedCurrentBody.includes(normalizedMeetingLink)) { await appendToBody(mailboxItem, meetingSummary); } + }); await setCustomPropertyAsync(mailboxItem, "wireId", createdMeeting.id); diff --git a/src/utils/mailbox.ts b/src/utils/mailbox.ts index e3a0ac3..792a02e 100644 --- a/src/utils/mailbox.ts +++ b/src/utils/mailbox.ts @@ -1,11 +1,16 @@ -/* global Office, console */ +export async function getMailboxItemSubject(item) : Promise { + return new Promise((resolve,reject) => { + const { subject } = item; -export async function getMailboxItemSubject(item): Promise { - return new Promise((resolve) => { - getSubject(item, (result) => { - resolve(result); - }); + subject.getAsync(function (asyncResult) { + if (asyncResult.status == Office.AsyncResultStatus.Failed) { + console.error("Failed to get item subject"); + reject(new Error("Failed to get item subject")); + } else { + resolve(asyncResult.value); + } }); +}); } export async function getOrganizer(item, callback) { @@ -20,18 +25,40 @@ export async function getOrganizer(item, callback) { }); } -export async function getSubject(item, callback) { +export async function setSubject(item, newSubject:string){ + const { subject } = item; - await subject.getAsync(function (asyncResult) { - if (asyncResult.status == Office.AsyncResultStatus.Failed) { - console.error("Failed to get item subject"); - } else { - callback(asyncResult.value); + subject.setAsync(newSubject, function (asyncResult) { + if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { + console.error(`Action failed with message ${asyncResult.error.message}`); + return; } }); } +export async function getMeetingTime(item): Promise { + return new Promise((resolve, reject) => { + const { start } = item; + + start.getAsync(function (asyncResult) { + if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { + console.error(`Action failed with message ${asyncResult.error.message}`); + reject(new Error(`Failed to get start time: ${asyncResult.error.message}`)); + return; + } + const locale = Office.context.displayLanguage; + const formattedDate = new Date(asyncResult.value).toLocaleString(locale, { + year: 'numeric', + month: 'numeric', + day: 'numeric', + }); + resolve(formattedDate); + }); + }); +} + + export function getBody(item): Promise { return new Promise((resolve, reject) => { const { body } = item; @@ -68,7 +95,7 @@ export async function appendToBody(item, contentToAppend) { } } -export async function getLocation(item): Promise { +export async function getLocation(item) { return new Promise((resolve, reject) => { const { location } = item; @@ -84,7 +111,7 @@ export async function getLocation(item): Promise { } -export async function setLocation(item, meetlingLink, callback) { +export async function setLocation(item, meetlingLink) { const { location } = item; location.setAsync(meetlingLink, function (asyncResult) { @@ -92,6 +119,5 @@ export async function setLocation(item, meetlingLink, callback) { console.error(`Action failed with message ${asyncResult.error.message}`); return; } - callback(); }); } From 72a878ba6f7cd8b11ac512b08df492e89931fc26 Mon Sep 17 00:00:00 2001 From: "kevin.jaroschewski" Date: Wed, 3 Jul 2024 09:03:31 +0200 Subject: [PATCH 3/4] style: changed style of Settings Tab --- src/calendarIntegration/addMeetingLink.ts | 422 +++++++++++----------- src/taskpane/components/App.tsx | 2 +- src/taskpane/components/LoggedIn.tsx | 44 +-- src/utils/mailbox.ts | 246 ++++++------- 4 files changed, 357 insertions(+), 357 deletions(-) diff --git a/src/calendarIntegration/addMeetingLink.ts b/src/calendarIntegration/addMeetingLink.ts index 85d5ab2..ffdfe34 100644 --- a/src/calendarIntegration/addMeetingLink.ts +++ b/src/calendarIntegration/addMeetingLink.ts @@ -1,211 +1,211 @@ -/* global Office, console */ - -import { appendToBody, getBody, getLocation, getMailboxItemSubject, getMeetingTime, getOrganizer, setLocation, setSubject } 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; - -/** - * Checks if the feature is enabled by calling isOutlookCalIntegrationEnabled. - * - * @return {Promise} Whether the feature is enabled or not. - */ -async function isFeatureEnabled(): Promise { - 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} A promise that resolves when the custom properties are fetched and the createdMeeting object is set. - */ -async function fetchCustomProperties(): Promise { - 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 subject for the mailbox item by getting the meeting date and appending it to the existing subject. - * - * @return {Promise} A promise that resolves when the new subject is set on the mailbox item. - */ -async function createSubject() { - const getMeetingDate = await getMeetingTime(mailboxItem); - const subject = `CLDR::${getMeetingDate}`; - - await setSubject(mailboxItem, subject); -} - -/** - * Appends the meeting date to the current subject of the mailbox item. - * - * @param {string} currentSubject - The current subject of the mailbox item. - * @return {Promise} A promise that resolves when the subject is appended. - */ -async function appendSubject(currentSubject:string){ - const getMeetingDate = await getMeetingTime(mailboxItem); - const newSubject = `${currentSubject} - CLDR::${getMeetingDate}`; - - await setSubject(mailboxItem, newSubject); -} - -/** - * 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} A promise that resolves when the new meeting is created and the custom properties are set. - */ -async function createNewMeeting(): Promise { - 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; - subject ? appendSubject(subject) : createSubject(); - 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} A promise that resolves when the meeting details are updated. - */ -async function updateMeetingDetails(eventResult: EventResult): Promise { - 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} A promise that resolves when the existing meeting is handled. - */ -async function handleExistingMeeting(): Promise { - if (!createdMeeting) { - throw new Error("createdMeeting is undefined"); - } - - const currentBody = await getBody(mailboxItem); - const currentSubject = await getMailboxItemSubject(mailboxItem); - const currentLocation = await getLocation(mailboxItem); - const normalizedCurrentBody = currentBody.replace(/&/g, "&"); - const normalizedMeetingLink = createdMeeting.link?.replace(/&/g, "&"); - - getOrganizer(mailboxItem, async (organizer) => { - - //Check if subject is empty - if(!currentSubject.includes("CLDR::")){ - currentSubject ? appendSubject(currentSubject) : createSubject(); - } - - //Check if location is empty - if (!currentLocation) { - await setLocation(mailboxItem, createdMeeting.link); - } - - const meetingSummary = createMeetingSummary(createdMeeting.link, organizer); - - //Check if body is empty - 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} A promise that resolves when the meeting link is added. - */ -async function addMeetingLink(event: Office.AddinCommands.Event): Promise { - try { - const isEnabled = await isFeatureEnabled(); - if (!isEnabled) return; - - await fetchCustomProperties(); - if (!createdMeeting) { - await createNewMeeting(); - } else { - await handleExistingMeeting(); - } - } catch (error) { - console.error("Error during adding Wire meeting link", error); - handleAddMeetingLinkError(error); - } finally { - event.completed(); - } -} - -/** - * 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 an error while adding the Wire meeting.", - Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage - ); - } -} - -export { addMeetingLink }; +/* global Office, console */ + +import { appendToBody, getBody, getLocation, getMailboxItemSubject, getMeetingTime, getOrganizer, setLocation, setSubject } 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; + +/** + * Checks if the feature is enabled by calling isOutlookCalIntegrationEnabled. + * + * @return {Promise} Whether the feature is enabled or not. + */ +async function isFeatureEnabled(): Promise { + 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} A promise that resolves when the custom properties are fetched and the createdMeeting object is set. + */ +async function fetchCustomProperties(): Promise { + 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 subject for the mailbox item by getting the meeting date and appending it to the existing subject. + * + * @return {Promise} A promise that resolves when the new subject is set on the mailbox item. + */ +async function createSubject() { + const getMeetingDate = await getMeetingTime(mailboxItem); + const subject = `CLDR::${getMeetingDate}`; + + await setSubject(mailboxItem, subject); +} + +/** + * Appends the meeting date to the current subject of the mailbox item. + * + * @param {string} currentSubject - The current subject of the mailbox item. + * @return {Promise} A promise that resolves when the subject is appended. + */ +async function appendSubject(currentSubject:string){ + const getMeetingDate = await getMeetingTime(mailboxItem); + const newSubject = `${currentSubject} - CLDR::${getMeetingDate}`; + + await setSubject(mailboxItem, newSubject); +} + +/** + * 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} A promise that resolves when the new meeting is created and the custom properties are set. + */ +async function createNewMeeting(): Promise { + 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; + subject ? appendSubject(subject) : createSubject(); + 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} A promise that resolves when the meeting details are updated. + */ +async function updateMeetingDetails(eventResult: EventResult): Promise { + 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} A promise that resolves when the existing meeting is handled. + */ +async function handleExistingMeeting(): Promise { + if (!createdMeeting) { + throw new Error("createdMeeting is undefined"); + } + + const currentBody = await getBody(mailboxItem); + const currentSubject = await getMailboxItemSubject(mailboxItem); + const currentLocation = await getLocation(mailboxItem); + const normalizedCurrentBody = currentBody.replace(/&/g, "&"); + const normalizedMeetingLink = createdMeeting.link?.replace(/&/g, "&"); + + getOrganizer(mailboxItem, async (organizer) => { + + //Check if subject is empty + if(!currentSubject.includes("CLDR::")){ + currentSubject ? appendSubject(currentSubject) : createSubject(); + } + + //Check if location is empty + if (!currentLocation) { + await setLocation(mailboxItem, createdMeeting.link); + } + + const meetingSummary = createMeetingSummary(createdMeeting.link, organizer); + + //Check if body is empty + 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} A promise that resolves when the meeting link is added. + */ +async function addMeetingLink(event: Office.AddinCommands.Event): Promise { + try { + const isEnabled = await isFeatureEnabled(); + if (!isEnabled) return; + + await fetchCustomProperties(); + if (!createdMeeting) { + await createNewMeeting(); + } else { + await handleExistingMeeting(); + } + } catch (error) { + console.error("Error during adding Wire meeting link", error); + handleAddMeetingLinkError(error); + } finally { + event.completed(); + } +} + +/** + * 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 an error while adding the Wire meeting.", + Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage + ); + } +} + +export { addMeetingLink }; diff --git a/src/taskpane/components/App.tsx b/src/taskpane/components/App.tsx index 310660b..184f921 100644 --- a/src/taskpane/components/App.tsx +++ b/src/taskpane/components/App.tsx @@ -64,7 +64,7 @@ export default class App extends React.Component {
-

Settings

+

Wire for Outlook

{isLoggedIn && user ? : }
diff --git a/src/taskpane/components/LoggedIn.tsx b/src/taskpane/components/LoggedIn.tsx index e2f77f8..f58c3da 100644 --- a/src/taskpane/components/LoggedIn.tsx +++ b/src/taskpane/components/LoggedIn.tsx @@ -1,22 +1,22 @@ -import * as React from "react"; -import { DefaultButton } from "@fluentui/react"; -import { SelfUser } from "../../types/SelfUser"; - -interface LoggedInProps { - user: SelfUser | null; - onLogout: () => void; -} - -const LoggedIn: React.FC = ({ user, onLogout }) => ( -
- Welcome!
-
- Display Name: {user.name}
- Username: {user.handle}
- E-mail: {user.email}
-
- Disconnect Add-in -
-); - -export default LoggedIn; +import * as React from "react"; +import { DefaultButton } from "@fluentui/react"; +import { SelfUser } from "../../types/SelfUser"; + +interface LoggedInProps { + user: SelfUser | null; + onLogout: () => void; +} + +const LoggedIn: React.FC = ({ user, onLogout }) => ( +
+

Welcome, {user.name}! You are logged in on + Wire for Outlook with the following credentials: +

+ Username: {user.handle}
+ E-mail: {user.email}
+
+ Disconnect Add-in +
+); + +export default LoggedIn; diff --git a/src/utils/mailbox.ts b/src/utils/mailbox.ts index 792a02e..6ee49cf 100644 --- a/src/utils/mailbox.ts +++ b/src/utils/mailbox.ts @@ -1,123 +1,123 @@ -export async function getMailboxItemSubject(item) : Promise { - return new Promise((resolve,reject) => { - const { subject } = item; - - subject.getAsync(function (asyncResult) { - if (asyncResult.status == Office.AsyncResultStatus.Failed) { - console.error("Failed to get item subject"); - reject(new Error("Failed to get item subject")); - } else { - resolve(asyncResult.value); - } - }); -}); -} - -export async function getOrganizer(item, callback) { - const { organizer } = item; - - await organizer.getAsync(function (asyncResult) { - if (asyncResult.status === Office.AsyncResultStatus.Failed) { - console.error("Failed to get organizer."); - } else { - callback(asyncResult.value.displayName); - } - }); -} - -export async function setSubject(item, newSubject:string){ - - const { subject } = item; - - subject.setAsync(newSubject, function (asyncResult) { - if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { - console.error(`Action failed with message ${asyncResult.error.message}`); - return; - } - }); -} - -export async function getMeetingTime(item): Promise { - return new Promise((resolve, reject) => { - const { start } = item; - - start.getAsync(function (asyncResult) { - if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { - console.error(`Action failed with message ${asyncResult.error.message}`); - reject(new Error(`Failed to get start time: ${asyncResult.error.message}`)); - return; - } - const locale = Office.context.displayLanguage; - const formattedDate = new Date(asyncResult.value).toLocaleString(locale, { - year: 'numeric', - month: 'numeric', - day: 'numeric', - }); - resolve(formattedDate); - }); - }); -} - - -export function getBody(item): Promise { - return new Promise((resolve, reject) => { - const { body } = item; - - body.getAsync(Office.CoercionType.Html, function (asyncResult) { - if (asyncResult.status === Office.AsyncResultStatus.Failed) { - console.error("Failed to get HTML body."); - reject(new Error("Failed to get HTML body.")); - } else { - resolve(asyncResult.value); - } - }); - }); -} - - -export function setBody(item, newBody) { - const { body } = item; - const type = { coercionType: Office.CoercionType.Html }; - - body.setAsync(newBody, type, function (asyncResult) { - if (asyncResult.status === Office.AsyncResultStatus.Failed) { - console.error("Failed to set HTML body.", asyncResult.error.message); - } - }); -} - -export async function appendToBody(item, contentToAppend) { - try { - const currentBody = await getBody(item); - setBody(item, currentBody + contentToAppend); - } catch (error) { - console.error("Failed to append to body:", error); - } -} - -export async function getLocation(item) { - return new Promise((resolve, reject) => { - const { location } = item; - - location.getAsync(function (asyncResult) { - if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { - console.error(`Action failed with message ${asyncResult.error.message}`); - reject(new Error(`Failed to get location: ${asyncResult.error.message}`)); - return; - } - resolve(asyncResult.value); - }); - }); -} - - -export async function setLocation(item, meetlingLink) { - const { location } = item; - - location.setAsync(meetlingLink, function (asyncResult) { - if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { - console.error(`Action failed with message ${asyncResult.error.message}`); - return; - } - }); -} +export async function getMailboxItemSubject(item) : Promise { + return new Promise((resolve,reject) => { + const { subject } = item; + + subject.getAsync(function (asyncResult) { + if (asyncResult.status == Office.AsyncResultStatus.Failed) { + console.error("Failed to get item subject"); + reject(new Error("Failed to get item subject")); + } else { + resolve(asyncResult.value); + } + }); +}); +} + +export async function getOrganizer(item, callback) { + const { organizer } = item; + + await organizer.getAsync(function (asyncResult) { + if (asyncResult.status === Office.AsyncResultStatus.Failed) { + console.error("Failed to get organizer."); + } else { + callback(asyncResult.value.displayName); + } + }); +} + +export async function setSubject(item, newSubject:string){ + + const { subject } = item; + + subject.setAsync(newSubject, function (asyncResult) { + if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { + console.error(`Action failed with message ${asyncResult.error.message}`); + return; + } + }); +} + +export async function getMeetingTime(item): Promise { + return new Promise((resolve, reject) => { + const { start } = item; + + start.getAsync(function (asyncResult) { + if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { + console.error(`Action failed with message ${asyncResult.error.message}`); + reject(new Error(`Failed to get start time: ${asyncResult.error.message}`)); + return; + } + const locale = Office.context.displayLanguage; + const formattedDate = new Date(asyncResult.value).toLocaleString(locale, { + year: 'numeric', + month: 'numeric', + day: 'numeric', + }); + resolve(formattedDate); + }); + }); +} + + +export function getBody(item): Promise { + return new Promise((resolve, reject) => { + const { body } = item; + + body.getAsync(Office.CoercionType.Html, function (asyncResult) { + if (asyncResult.status === Office.AsyncResultStatus.Failed) { + console.error("Failed to get HTML body."); + reject(new Error("Failed to get HTML body.")); + } else { + resolve(asyncResult.value); + } + }); + }); +} + + +export function setBody(item, newBody) { + const { body } = item; + const type = { coercionType: Office.CoercionType.Html }; + + body.setAsync(newBody, type, function (asyncResult) { + if (asyncResult.status === Office.AsyncResultStatus.Failed) { + console.error("Failed to set HTML body.", asyncResult.error.message); + } + }); +} + +export async function appendToBody(item, contentToAppend) { + try { + const currentBody = await getBody(item); + setBody(item, currentBody + contentToAppend); + } catch (error) { + console.error("Failed to append to body:", error); + } +} + +export async function getLocation(item) { + return new Promise((resolve, reject) => { + const { location } = item; + + location.getAsync(function (asyncResult) { + if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { + console.error(`Action failed with message ${asyncResult.error.message}`); + reject(new Error(`Failed to get location: ${asyncResult.error.message}`)); + return; + } + resolve(asyncResult.value); + }); + }); +} + + +export async function setLocation(item, meetlingLink) { + const { location } = item; + + location.setAsync(meetlingLink, function (asyncResult) { + if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) { + console.error(`Action failed with message ${asyncResult.error.message}`); + return; + } + }); +} From c0da926c2ed0d719e152ec5d8297a644b9fb4f3f Mon Sep 17 00:00:00 2001 From: "kevin.jaroschewski" Date: Thu, 4 Jul 2024 08:11:09 +0200 Subject: [PATCH 4/4] fix: setting conversation name instead of subject --- src/calendarIntegration/addMeetingLink.ts | 35 +++++++---------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/src/calendarIntegration/addMeetingLink.ts b/src/calendarIntegration/addMeetingLink.ts index ffdfe34..e7e06de 100644 --- a/src/calendarIntegration/addMeetingLink.ts +++ b/src/calendarIntegration/addMeetingLink.ts @@ -48,30 +48,20 @@ async function fetchCustomProperties(): Promise { } /** - * Creates a new subject for the mailbox item by getting the meeting date and appending it to the existing subject. + * Modifies the conversation subject by appending the meeting date. * - * @return {Promise} A promise that resolves when the new subject is set on the mailbox item. + * @param {string} conversationSubject - The current subject of the conversation. + * @return {Promise} The modified conversation subject. */ -async function createSubject() { - const getMeetingDate = await getMeetingTime(mailboxItem); - const subject = `CLDR::${getMeetingDate}`; +async function modifyConversationName(conversationSubject: string): Promise { + const meetingDate = await getMeetingTime(mailboxItem); - await setSubject(mailboxItem, subject); -} - -/** - * Appends the meeting date to the current subject of the mailbox item. - * - * @param {string} currentSubject - The current subject of the mailbox item. - * @return {Promise} A promise that resolves when the subject is appended. - */ -async function appendSubject(currentSubject:string){ - const getMeetingDate = await getMeetingTime(mailboxItem); - const newSubject = `${currentSubject} - CLDR::${getMeetingDate}`; + const modifiedSubject = conversationSubject ? `CLDR:: ${conversationSubject}` : `CLDR:: ${meetingDate}`; - await setSubject(mailboxItem, newSubject); + return modifiedSubject; } + /** * 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, @@ -87,13 +77,13 @@ async function createNewMeeting(): Promise { Office.MailboxEnums.ItemNotificationMessageType.ProgressIndicator ); - const subject = await getMailboxItemSubject(mailboxItem); + let subject = await getMailboxItemSubject(mailboxItem); + subject = await modifyConversationName(subject); const eventResult = await createEvent(subject || defaultSubjectValue); if (eventResult) { createdMeeting = eventResult; - subject ? appendSubject(subject) : createSubject(); await updateMeetingDetails(eventResult); await setCustomPropertyAsync(mailboxItem, "wireId", eventResult.id); await setCustomPropertyAsync(mailboxItem, "wireLink", eventResult.link); @@ -135,11 +125,6 @@ async function handleExistingMeeting(): Promise { getOrganizer(mailboxItem, async (organizer) => { - //Check if subject is empty - if(!currentSubject.includes("CLDR::")){ - currentSubject ? appendSubject(currentSubject) : createSubject(); - } - //Check if location is empty if (!currentLocation) { await setLocation(mailboxItem, createdMeeting.link);