Created
May 24, 2024 04:24
-
-
Save nickjanssen/6ba4e1c7acf0b90a94c36d7ae2060915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mailsWithoutReplyId = inboxEmails.filter((email) => { | |
return email.replyToMessageId === null; | |
}); | |
const groupedEmails: EmailThread[] = []; | |
for (const mailWithoutReplyId of mailsWithoutReplyId) { | |
const chain = recursiveExtractReplies([mailWithoutReplyId]); | |
const emails = chain | |
.sort((a, b) => { | |
return a.sentAt.getTime() - b.sentAt.getTime(); | |
}); | |
groupedEmails.push({ | |
emails, | |
lead: mailWithoutReplyId.lead, | |
emailAccount: mailWithoutReplyId.emailAccount, | |
campaign: mailWithoutReplyId.campaign, | |
}); | |
} | |
const recursiveExtractReplies = (inboxEmails: InboxEmailWithExtraInfo[]) => { | |
const finalArray: InboxEmailWithExtraInfo[] = [...inboxEmails]; | |
for (const actualEmail of inboxEmails) { | |
let replies = inboxEmails.filter( | |
(email) => email.replyToMessageId === actualEmail.messageId | |
); | |
const subReplies = recursiveExtractReplies(replies); | |
if (subReplies) { | |
finalArray.push(...subReplies); | |
} | |
} | |
return finalArray; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment