From 1ccc25f18512df24cf2535f50743ec8499e70081 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 14 Feb 2022 15:02:37 +0000 Subject: [PATCH] Added new EmailBuilder class --- lib/Emailer.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 lib/Emailer.js diff --git a/lib/Emailer.js b/lib/Emailer.js new file mode 100644 index 0000000..97cd9b2 --- /dev/null +++ b/lib/Emailer.js @@ -0,0 +1,78 @@ +'use strict'; + +function removeDupes(src, dest) { + return src.filter(item => !dest.includes(item)); +} + +class EmailBuilder { + subject; + body; + HTMLBody; + from = 'Stratos '; + to; + cc; + bcc; + + constructor() { + this.to = []; + this.cc = []; + this.bcc = []; + } + + setSubject(subject) { + this.subject = subject; + return this; + } + + setBody(body) { + this.body = body; + return this; + } + + setHTMLBody(HTMLBody) { + this.HTMLBody = HTMLBody; + return this; + } + + setFrom(from) { + this.from = from; + return this; + } + + addTo(addresses) { + const newAddresses = removeDupes(addresses, this.to); + this.to.push(...newAddresses); + return this; + } + + removeTo(addresses) { + this.to = removeDupes(this.to, addresses); + return this; + } + + addCC(addresses) { + const newAddresses = removeDupes(addresses, this.to); + this.cc.push(...newAddresses); + return this; + } + + removeCC(addresses) { + this.cc = removeDupes(this.cc, addresses); + return this; + } + + addBCC(addresses) { + const newAddresses = removeDupes(addresses, this.to); + this.bcc.push(...newAddresses); + return this; + } + + removeBCC(addresses) { + this.bcc = removeDupes(this.bcc, addresses); + return this; + } +} + +module.exports = { + EmailBuilder: EmailBuilder +};