Skip to content

Commit

Permalink
renamed logout, refactored and added subject handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin.jaroschewski committed Jun 26, 2024
1 parent 54827cc commit ea46165
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 18 deletions.
46 changes: 43 additions & 3 deletions src/calendarIntegration/addMeetingLink.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -47,6 +47,31 @@ async function fetchCustomProperties(): Promise<void> {
}
}

/**
* Creates a new subject for the mailbox item by getting the meeting date and appending it to the existing subject.
*
* @return {Promise<void>} 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<void>} 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,
Expand All @@ -63,10 +88,12 @@ async function createNewMeeting(): Promise<void> {
);

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);
Expand All @@ -75,6 +102,7 @@ async function createNewMeeting(): Promise<void> {
removeNotification("adding-wire-meeting");
}


/**
* Updates the meeting details by setting the location and appending the meeting summary to the body of the mailbox item.
*
Expand All @@ -83,7 +111,7 @@ async function createNewMeeting(): Promise<void> {
*/
async function updateMeetingDetails(eventResult: EventResult): Promise<void> {
getOrganizer(mailboxItem, async (organizer) => {
await setLocation(mailboxItem, eventResult.link, () => {});
await setLocation(mailboxItem, eventResult.link);
const meetingSummary = createMeetingSummary(eventResult.link, organizer);
await appendToBody(mailboxItem, meetingSummary);
});
Expand All @@ -100,18 +128,30 @@ async function handleExistingMeeting(): Promise<void> {
}

const currentBody = await getBody(mailboxItem);
const currentSubject = await getMailboxItemSubject(mailboxItem);
const currentLocation = await getLocation(mailboxItem);
const normalizedCurrentBody = currentBody.replace(/&amp;/g, "&");
const normalizedMeetingLink = createdMeeting.link?.replace(/&amp;/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);
Expand Down
56 changes: 41 additions & 15 deletions src/utils/mailbox.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/* global Office, console */
export async function getMailboxItemSubject(item) : Promise<string> {
return new Promise((resolve,reject) => {
const { subject } = item;

export async function getMailboxItemSubject(item): Promise<string> {
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) {
Expand All @@ -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<string> {
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<string> {
return new Promise((resolve, reject) => {
const { body } = item;
Expand Down Expand Up @@ -68,7 +95,7 @@ export async function appendToBody(item, contentToAppend) {
}
}

export async function getLocation(item): Promise<string> {
export async function getLocation(item) {
return new Promise((resolve, reject) => {
const { location } = item;

Expand All @@ -84,14 +111,13 @@ export async function getLocation(item): Promise<string> {
}


export async function setLocation(item, meetlingLink, callback) {
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;
}
callback();
});
}

0 comments on commit ea46165

Please sign in to comment.