* Build from data
(data)
| 51 | * Build from data |
| 52 | */ |
| 53 | fromData(data) { |
| 54 | |
| 55 | //Expecting object |
| 56 | if (typeof data !== 'object') { |
| 57 | throw new Error('Expecting object for Mail data'); |
| 58 | } |
| 59 | |
| 60 | //Convert to camel case to make it workable, making a copy to prevent |
| 61 | //changes to the original objects |
| 62 | data = deepClone(data); |
| 63 | data = toCamelCase(data, ['substitutions', 'dynamicTemplateData', 'customArgs', 'headers', 'sections']); |
| 64 | |
| 65 | //Extract properties from data |
| 66 | const { |
| 67 | to, from, replyTo, cc, bcc, sendAt, subject, text, html, content, |
| 68 | templateId, personalizations, attachments, ipPoolName, batchId, |
| 69 | sections, headers, categories, category, customArgs, asm, mailSettings, |
| 70 | trackingSettings, substitutions, substitutionWrappers, dynamicTemplateData, isMultiple, |
| 71 | hideWarnings, replyToList, |
| 72 | } = data; |
| 73 | |
| 74 | //Set data |
| 75 | this.setFrom(from); |
| 76 | this.setReplyTo(replyTo); |
| 77 | this.setSubject(subject); |
| 78 | this.setSendAt(sendAt); |
| 79 | this.setTemplateId(templateId); |
| 80 | this.setBatchId(batchId); |
| 81 | this.setIpPoolName(ipPoolName); |
| 82 | this.setAttachments(attachments); |
| 83 | this.setContent(content); |
| 84 | this.setSections(sections); |
| 85 | this.setHeaders(headers); |
| 86 | this.setCategories(category); |
| 87 | this.setCategories(categories); |
| 88 | this.setCustomArgs(customArgs); |
| 89 | this.setAsm(asm); |
| 90 | this.setMailSettings(mailSettings); |
| 91 | this.setTrackingSettings(trackingSettings); |
| 92 | this.setHideWarnings(hideWarnings); |
| 93 | this.setReplyToList(replyToList); |
| 94 | |
| 95 | if (this.isDynamic) { |
| 96 | this.setDynamicTemplateData(dynamicTemplateData); |
| 97 | } else { |
| 98 | this.setSubstitutions(substitutions); |
| 99 | this.setSubstitutionWrappers(substitutionWrappers); |
| 100 | } |
| 101 | |
| 102 | //Add contents from text/html properties |
| 103 | this.addTextContent(text); |
| 104 | this.addHtmlContent(html); |
| 105 | |
| 106 | //Using "to" property for personalizations |
| 107 | if (personalizations) { |
| 108 | this.setPersonalizations(personalizations); |
| 109 | } else if (isMultiple && Array.isArray(to)) { |
| 110 | //Multiple individual emails |
no test coverage detected