-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseEmails.js
34 lines (26 loc) · 1.06 KB
/
parseEmails.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
const { JSDOM } = require('jsdom');
function getContentInsideSquareBrackets(inputString) {
const regex = /\[(.*?)\]/; // This regular expression matches anything inside square brackets and captures it
const match = inputString.match(regex);
// Check if there's a match and return the content inside brackets, or an empty string if there's no match
return match ? match[1] : '';
}
function parseEmail(email, outlookmail) {
const subject = email.subject;
const ticketId = getContentInsideSquareBrackets(subject);
const emailBody = email.body.content;
// Create a virtual DOM
const dom = new JSDOM(emailBody);
const doc = dom.window.document;
let targetDiv = doc.querySelector('div[dir="ltr"]');
if(email.sender.emailAddress.address === outlookmail) {
targetDiv = doc.querySelector('.elementToProof');
}
const divTextContent = targetDiv.textContent;
return {
ticketId,
sender: email.sender.emailAddress.address,
message: divTextContent
};
}
module.exports = parseEmail;