1- const { ToBulkRecipient, ToEmailAddress, ToCustomHeader, ToMergeData } = require ( '../helpers/helpersClasses' ) ;
1+ const { ToBulkRecipient, ToEmailAddress, ToCustomHeader, ToMergeData, ToMetadata } = require ( '../helpers/helpersClasses' ) ;
22const { BasicMessage, BulkMessage } = require ( '../message/messageClasses' ) ;
33const sendValidator = require ( './sendValidator' ) ;
44const sendResultEnum = require ( '../sendResultEnum' ) ;
@@ -8,111 +8,130 @@ class InjectionRequestFactory {
88 //constructor(){}
99 generateRequest ( messageData ) {
1010 return new Promise ( ( resolve , reject ) => {
11- var {
12- to,
13- from,
14- replyTo,
15- subject,
16- textBody,
17- htmlBody,
18- apiTemplate,
19- cc,
20- bcc,
21- attachments,
22- messageId,
23- mailingId,
24- charSet,
25- customHeaders,
26- globalMergeData,
27- messageType
28- } = messageData ;
11+ var {
12+ to,
13+ from,
14+ replyTo,
15+ subject,
16+ textBody,
17+ htmlBody,
18+ apiTemplate,
19+ cc,
20+ bcc,
21+ attachments,
22+ messageId,
23+ mailingId,
24+ charSet,
25+ customHeaders,
26+ metadata,
27+ tags,
28+ globalMergeData,
29+ messageType
30+ } = messageData ;
2931
30- var result = { } ;
31- var validator = new sendValidator ( ) ;
32+ var result = { } ;
33+ var validator = new sendValidator ( ) ;
3234
33- if ( messageType === "basic" ) {
35+ if ( messageType === "basic" ) {
3436
35- var basicMsg = new BasicMessage ( ) ;
37+ var basicMsg = new BasicMessage ( ) ;
3638
37- if ( to ) {
38- if ( Array . isArray ( to ) ) basicMsg . to = to ;
39- else basicMsg . to . push ( ToEmailAddress . convert ( to ) ) ;
40- }
41- basicMsg . from = from ;
42- basicMsg . replyTo = replyTo ;
43- basicMsg . subject = subject ;
44- basicMsg . textBody = textBody ;
45- basicMsg . htmlBody = htmlBody ;
46- if ( attachments ) {
47- if ( Array . isArray ( attachments ) ) basicMsg . attachments = attachments ;
48- else basicMsg . attachments . push ( attachments ) ;
49- }
50- if ( cc ) {
51- if ( Array . isArray ( cc ) ) basicMsg . cc = cc ;
52- else basicMsg . cc . push ( ToEmailAddress . convert ( cc ) ) ;
53- }
54- if ( bcc ) {
55- if ( Array . isArray ( bcc ) ) basicMsg . bcc = bcc ;
56- else basicMsg . bcc . push ( ToEmailAddress . convert ( bcc ) ) ;
57- }
58- basicMsg . apiTemplate = apiTemplate ;
59- basicMsg . messageId = messageId ;
60- basicMsg . mailingId = mailingId ;
61- basicMsg . charSet = charSet ;
62- if ( customHeaders ) {
63- if ( Array . isArray ( customHeaders ) ) basicMsg . customHeaders = customHeaders ;
64- else basicMsg . customHeaders . push ( ToEmailAddress . convert ( customHeaders ) ) ;
65- }
66-
67- result = validator . validateBasicMessage ( basicMsg ) ;
68- if ( result . result !== sendResultEnum . Success ) {
69- reject ( result ) ;
70- }
39+ if ( to ) {
40+ if ( Array . isArray ( to ) ) basicMsg . to = to ;
41+ else basicMsg . to . push ( ToEmailAddress . convert ( to ) ) ;
42+ }
43+ basicMsg . from = from ;
44+ basicMsg . replyTo = replyTo ;
45+ basicMsg . subject = subject ;
46+ basicMsg . textBody = textBody ;
47+ basicMsg . htmlBody = htmlBody ;
48+ if ( attachments ) {
49+ if ( Array . isArray ( attachments ) ) basicMsg . attachments = attachments ;
50+ else basicMsg . attachments . push ( attachments ) ;
51+ }
52+ if ( cc ) {
53+ if ( Array . isArray ( cc ) ) basicMsg . cc = cc ;
54+ else basicMsg . cc . push ( ToEmailAddress . convert ( cc ) ) ;
55+ }
56+ if ( bcc ) {
57+ if ( Array . isArray ( bcc ) ) basicMsg . bcc = bcc ;
58+ else basicMsg . bcc . push ( ToEmailAddress . convert ( bcc ) ) ;
59+ }
60+ basicMsg . apiTemplate = apiTemplate ;
61+ basicMsg . messageId = messageId ;
62+ basicMsg . mailingId = mailingId ;
63+ basicMsg . charSet = charSet ;
64+ if ( customHeaders ) {
65+ if ( Array . isArray ( customHeaders ) ) basicMsg . customHeaders = customHeaders ;
66+ else basicMsg . customHeaders . push ( ToEmailAddress . convert ( customHeaders ) ) ;
67+ }
68+ if ( metadata ) {
69+ if ( Array . isArray ( metadata ) ) basicMsg . metadata = metadata ;
70+ else basicMsg . metadata . push ( ToMetadata . convert ( metadata ) ) ;
71+ }
72+ if ( tags ) {
73+ if ( Array . isArray ( tags ) ) basicMsg . tags = tags ;
74+ else basicMsg . tags . push ( tags ) ;
75+ }
7176
72- resolve ( basicMsg . toJSON ( ) ) ;
73- } else if ( messageType === "bulk" ) {
74- var bulkMsg = new BulkMessage ( ) ;
77+ result = validator . validateBasicMessage ( basicMsg ) ;
78+ if ( result . result !== sendResultEnum . Success ) {
79+ reject ( result ) ;
80+ }
7581
76- if ( to ) {
77- if ( Array . isArray ( to ) ) bulkMsg . to = to ;
78- else bulkMsg . to . push ( ToBulkRecipient . convert ( to ) ) ;
79- }
80- bulkMsg . from = from ;
81- bulkMsg . replyTo = replyTo ;
82- bulkMsg . subject = subject ;
83- bulkMsg . textBody = textBody ;
84- bulkMsg . htmlBody = htmlBody ;
85- if ( attachments ) {
86- if ( Array . isArray ( attachments ) ) bulkMsg . attachments = attachments ;
87- else bulkMsg . attachments . push ( attachments ) ;
88- }
89- bulkMsg . apiTemplate = apiTemplate ;
90- bulkMsg . messageId = messageId ;
91- bulkMsg . mailingId = mailingId ;
92- bulkMsg . charSet = charSet ;
93- if ( customHeaders ) {
94- if ( Array . isArray ( customHeaders ) ) bulkMsg . customHeaders = customHeaders ;
95- else bulkMsg . customHeaders . push ( ToCustomHeader . convert ( customHeaders ) ) ;
96- }
97- if ( globalMergeData ) {
98- if ( Array . isArray ( globalMergeData ) ) bulkMsg . globalMergeData = globalMergeData ;
99- else bulkMsg . globalMergeData . push ( ToMergeData . convert ( globalMergeData ) ) ;
100- }
82+ resolve ( basicMsg . toJSON ( ) ) ;
83+ } else if ( messageType === "bulk" ) {
84+ var bulkMsg = new BulkMessage ( ) ;
10185
102- result = validator . validateBulkMessage ( bulkMsg ) ;
103- if ( result . result !== sendResultEnum . Success ) {
104- reject ( result ) ;
105- }
86+ if ( to ) {
87+ if ( Array . isArray ( to ) ) bulkMsg . to = to ;
88+ else bulkMsg . to . push ( ToBulkRecipient . convert ( to ) ) ;
89+ }
90+ bulkMsg . from = from ;
91+ bulkMsg . replyTo = replyTo ;
92+ bulkMsg . subject = subject ;
93+ bulkMsg . textBody = textBody ;
94+ bulkMsg . htmlBody = htmlBody ;
95+ if ( attachments ) {
96+ if ( Array . isArray ( attachments ) ) bulkMsg . attachments = attachments ;
97+ else bulkMsg . attachments . push ( attachments ) ;
98+ }
99+ bulkMsg . apiTemplate = apiTemplate ;
100+ bulkMsg . messageId = messageId ;
101+ bulkMsg . mailingId = mailingId ;
102+ bulkMsg . charSet = charSet ;
103+ if ( customHeaders ) {
104+ if ( Array . isArray ( customHeaders ) ) bulkMsg . customHeaders = customHeaders ;
105+ else bulkMsg . customHeaders . push ( ToCustomHeader . convert ( customHeaders ) ) ;
106+ }
107+ if ( metadata ) {
108+ if ( Array . isArray ( metadata ) ) bulkMsg . metadata = metadata ;
109+ else bulkMsg . metadata . push ( ToMetadata . convert ( metadata ) ) ;
110+ }
111+ if ( tags ) {
112+ if ( Array . isArray ( tags ) ) bulkMsg . tags = tags ;
113+ else bulkMsg . tags . push ( tags ) ;
114+ }
115+ if ( globalMergeData ) {
116+ if ( Array . isArray ( globalMergeData ) ) bulkMsg . globalMergeData = globalMergeData ;
117+ else bulkMsg . globalMergeData . push ( ToMergeData . convert ( globalMergeData ) ) ;
118+ }
106119
107- resolve ( bulkMsg . toJSON ( ) ) ;
108- } else {
109- result = new sendResponse ( {
110- result : sendResultEnum . MessageValidationInvalidMessageType
111- } ) ;
120+ result = validator . validateBulkMessage ( bulkMsg ) ;
121+ if ( result . result !== sendResultEnum . Success ) {
112122 reject ( result ) ;
113123 }
124+
125+ resolve ( bulkMsg . toJSON ( ) ) ;
126+ } else {
127+ result = new sendResponse ( {
128+ result : sendResultEnum . MessageValidationInvalidMessageType
129+ } ) ;
130+ reject ( result ) ;
114131 }
115- ) }
132+ }
133+ )
116134 }
135+ }
117136
118- module . exports = new InjectionRequestFactory ( ) ;
137+ module . exports = new InjectionRequestFactory ( ) ;
0 commit comments