1
1
import axios from 'axios' ;
2
- import { customAPIurl } from '../../../../Utils.js' ;
2
+ import { customAPIurl , replaceMailVaribles } from '../../../../Utils.js' ;
3
3
4
+ // `sendDoctoWebhook` is used to send res data of document on webhook
5
+ async function sendDoctoWebhook ( doc , WebhookUrl , userId ) {
6
+ if ( WebhookUrl ) {
7
+ const params = {
8
+ event : 'created' ,
9
+ ...doc ,
10
+ } ;
11
+ await axios
12
+ . post ( WebhookUrl , params , {
13
+ headers : { 'Content-Type' : 'application/json' } ,
14
+ } )
15
+ . then ( res => {
16
+ try {
17
+ // console.log('res ', res);
18
+ const webhook = new Parse . Object ( 'contracts_Webhook' ) ;
19
+ webhook . set ( 'Log' , res ?. status ) ;
20
+ webhook . set ( 'UserId' , {
21
+ __type : 'Pointer' ,
22
+ className : '_User' ,
23
+ objectId : userId ,
24
+ } ) ;
25
+ webhook . save ( null , { useMasterKey : true } ) ;
26
+ } catch ( err ) {
27
+ console . log ( 'err save in contracts_Webhook' , err ) ;
28
+ }
29
+ } )
30
+ . catch ( err => {
31
+ console . log ( 'Err send data to webhook' , err ) ;
32
+ try {
33
+ const webhook = new Parse . Object ( 'contracts_Webhook' ) ;
34
+ webhook . set ( 'Log' , err ?. status ) ;
35
+ webhook . set ( 'UserId' , {
36
+ __type : 'Pointer' ,
37
+ className : '_User' ,
38
+ objectId : userId ,
39
+ } ) ;
40
+ webhook . save ( null , { useMasterKey : true } ) ;
41
+ } catch ( err ) {
42
+ console . log ( 'err save in contracts_Webhook' , err ) ;
43
+ }
44
+ } ) ;
45
+ // console.log('res ', res.data);
46
+ }
47
+ }
4
48
export default async function createDocumentWithTemplate ( request , response ) {
5
49
const signers = request . body . signers ;
6
50
const folderId = request . body . folderId ;
7
51
const templateId = request . params . template_id ;
8
52
const protocol = customAPIurl ( ) ;
9
53
const baseUrl = new URL ( process . env . SERVER_URL ) ;
10
- const send_email = request . body . send_email || true ;
11
-
54
+ const send_email = request . body . send_email ;
55
+ const email_subject = request . body . email_subject ;
56
+ const email_body = request . body . email_body ;
12
57
try {
13
58
const reqToken = request . headers [ 'x-api-token' ] ;
14
59
if ( ! reqToken ) {
@@ -142,34 +187,32 @@ export default async function createDocumentWithTemplate(request, response) {
142
187
const serverUrl = process . env . SERVER_URL ;
143
188
const newServer = serverUrl . replaceAll ( '/' , '%2F' ) ;
144
189
const serverParams = `${ newServer } %2F&${ process . env . APP_ID } &contracts` ;
190
+ if ( send_email === false ) {
191
+ console . log ( "don't send mail" ) ;
192
+ } else {
193
+ for ( let i = 0 ; i < contact . length ; i ++ ) {
194
+ try {
195
+ const imgPng = 'https://qikinnovation.ams3.digitaloceanspaces.com/logo.png' ;
196
+ let url = `${ process . env . SERVER_URL } /functions/sendmailv3/` ;
197
+ const headers = {
198
+ 'Content-Type' : 'application/json' ,
199
+ 'X-Parse-Application-Id' : process . env . APP_ID ,
200
+ 'X-Parse-Master-Key' : process . env . MASTER_KEY ,
201
+ } ;
145
202
146
- for ( let i = 0 ; i < contact . length ; i ++ ) {
147
- try {
148
- const imgPng = 'https://qikinnovation.ams3.digitaloceanspaces.com/logo.png' ;
149
- let url = `${ process . env . SERVER_URL } /functions/sendmailv3/` ;
150
- const headers = {
151
- 'Content-Type' : 'application/json' ,
152
- 'X-Parse-Application-Id' : process . env . APP_ID ,
153
- 'X-Parse-Master-Key' : process . env . MASTER_KEY ,
154
- } ;
155
-
156
- const objectId = contact [ i ] . contactPtr . objectId ;
203
+ const objectId = contact [ i ] . contactPtr . objectId ;
157
204
158
- const hostUrl = baseUrl . origin + '/loadmf/signmicroapp' ;
159
- let signPdf = `${ hostUrl } /login/${ res . id } /${ contact [ i ] . email } /${ objectId } /${ serverParams } ` ;
160
- const openSignUrl = 'https://www.opensignlabs.com/contact-us' ;
161
- const orgName = template . ExtUserPtr . Company ? template . ExtUserPtr . Company : '' ;
162
- const themeBGcolor = '#47a3ad' ;
163
- let params = {
164
- recipient : contact [ i ] . email ,
165
- subject : `${ template . ExtUserPtr . Name } has requested you to sign ${ template . ExtUserPtr . Name } ` ,
166
- from : sender ,
167
- html :
168
- "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /> </head> <body> <div style='background-color: #f5f5f5; padding: 20px'=> <div style=' box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;background: white;padding-bottom: 20px;'> <div style='padding:10px 10px 0 10px'><img src=" +
205
+ const hostUrl = baseUrl . origin + '/loadmf/signmicroapp' ;
206
+ let signPdf = `${ hostUrl } /login/${ res . id } /${ contact [ i ] . email } /${ objectId } /${ serverParams } ` ;
207
+ const openSignUrl = 'https://www.opensignlabs.com/contact-us' ;
208
+ const orgName = template . ExtUserPtr . Company ? template . ExtUserPtr . Company : '' ;
209
+ const themeBGcolor = '#47a3ad' ;
210
+ const email_html =
211
+ "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /> </head> <body> <div style='background-color: #f5f5f5; padding: 20px'> <div style='box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;background: white;padding-bottom: 20px;'> <div style='padding:10px 10px 0 10px'><img src='" +
169
212
imgPng +
170
- " height='50' style='padding: 20px, width:170px, height:40px' /></div> <div style=' padding: 2px;font-family: system-ui;background-color:" +
213
+ "' height='50' style='padding:20px; width:170px; height:40px; ' /></div><div style='padding: 2px;font-family: system-ui;background-color:" +
171
214
themeBGcolor +
172
- ";'><p style='font-size: 20px;font-weight: 400;color: white;padding-left: 20px;' > Digital Signature Request</p></div><div><p style='padding: 20px;font-family: system-ui;font-size: 14px; margin-bottom: 10px;'> " +
215
+ ";'><p style='font-size: 20px;font-weight: 400;color: white;padding-left: 20px;' > Digital Signature Request</p></div><div><p style='padding: 20px;font-family: system-ui;font-size: 14px; margin-bottom: 10px;'> " +
173
216
template . ExtUserPtr . Name +
174
217
' has requested you to review and sign <strong> ' +
175
218
template . Name +
@@ -181,33 +224,83 @@ export default async function createDocumentWithTemplate(request, response) {
181
224
localExpireDate +
182
225
"</td></tr><tr> <td></td> <td> </td></tr></table> </div> <div style='margin-left:70px'><a href=" +
183
226
signPdf +
184
- "> <button style='padding: 12px 12px 12px 12px;background-color: #d46b0f;color: white; border: 0px;box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px,rgba(0, 0, 0, 0.08) 0px 0px 0px 1px;font-weight:bold;margin-top:30px'>Sign here</button></a> </div> <div style='display: flex; justify-content: center;margin-top: 10px;'> </div></div></div><div><p> This is an automated email from OpenSign™. For any queries regarding this email, please contact the sender " +
227
+ "> <button style='padding: 12px 12px 12px 12px;background-color: #d46b0f;color: white; border: 0px;box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px,rgba(0, 0, 0, 0.08) 0px 0px 0px 1px;font-weight:bold;margin-top:30px; '>Sign here</button></a> </div> <div style='display: flex; justify-content: center;margin-top: 10px;'> </div></div></div><div><p> This is an automated email from OpenSign™. For any queries regarding this email, please contact the sender " +
185
228
sender +
186
- ' directly.If you think this email is inappropriate or spam, you may file a complaint with OpenSign™ <a href= ' +
229
+ ' directly.If you think this email is inappropriate or spam, you may file a complaint with OpenSign™ <a href=' +
187
230
openSignUrl +
188
- ' target=_blank>here</a>.</p> </div></div></body> </html>' ,
189
- } ;
190
- sendMail = await axios . post ( url , params , { headers : headers } ) ;
191
- } catch ( error ) {
192
- console . log ( 'error' , error ) ;
231
+ ' target=_blank>here</a>.</p> </div></div></body> </html>' ;
232
+
233
+ let replaceVar ;
234
+ const variables = {
235
+ document_title : template . Name ,
236
+ sender_name : template . ExtUserPtr . Name ,
237
+ sender_mail : template . ExtUserPtr . Email ,
238
+ sender_phone : template . ExtUserPtr . Phone ,
239
+ receiver_name : contact [ i ] . name ,
240
+ receiver_email : contact [ i ] . email ,
241
+ receiver_phone : contact [ i ] . phone ,
242
+ expiry_date : localExpireDate ,
243
+ company_name : orgName ,
244
+ signing_url : signPdf ,
245
+ } ;
246
+ if ( email_subject && email_body ) {
247
+ replaceVar = replaceMailVaribles ( email_subject , email_body , variables ) ;
248
+ } else if ( email_subject ) {
249
+ replaceVar = replaceMailVaribles ( email_subject , '' , variables ) ;
250
+ replaceVar = { subject : replaceVar . subject , body : email_html } ;
251
+ } else if ( email_body ) {
252
+ replaceVar = replaceMailVaribles (
253
+ `${ template . ExtUserPtr . Name } has requested you to sign ${ template . Name } ` ,
254
+ email_body ,
255
+ variables
256
+ ) ;
257
+ } else {
258
+ replaceVar = {
259
+ subject : `${ template . ExtUserPtr . Name } has requested you to sign ${ template . Name } ` ,
260
+ body : email_html ,
261
+ } ;
262
+ }
263
+
264
+ const subject = replaceVar . subject ;
265
+ const html = replaceVar . body ;
266
+ let params = {
267
+ recipient : contact [ i ] . email ,
268
+ subject : subject ,
269
+ from : sender ,
270
+ html : html ,
271
+ } ;
272
+ sendMail = await axios . post ( url , params , { headers : headers } ) ;
273
+ } catch ( error ) {
274
+ console . log ( 'error' , error ) ;
275
+ }
193
276
}
194
277
}
195
-
196
- if ( sendMail . data . result . status === 'success' ) {
197
- const user = contact . find ( x => x . email === template . ExtUserPtr . Email ) ;
198
- if ( user && user . email ) {
199
- return response . json ( {
200
- objectId : res . id ,
201
- url : `${ baseUrl . origin } /loadmf/signmicroapp/login/${ res . id } /${ user . email } /${ user . contactPtr . objectId } /${ serverParams } ` ,
202
- message : 'Document sent successfully!' ,
203
- } ) ;
204
- } else {
205
- return response . json ( {
206
- objectId : res . id ,
207
- message : 'Document sent successfully!' ,
208
- } ) ;
278
+ // if (sendMail.data.result.status === 'success') {
279
+ try {
280
+ const doc = {
281
+ objectId : res . id ,
282
+ file : template ?. URL ,
283
+ name : template ?. Name ,
284
+ note : template ?. Note || '' ,
285
+ description : template ?. Description || '' ,
286
+ signers : contact ?. map ( x => ( { name : x . name , email : x . email , phone : x . phone } ) ) ,
287
+ createdAt : res . createdAt ,
288
+ } ;
289
+ if ( template . ExtUserPtr && template . ExtUserPtr ?. Webhook ) {
290
+ sendDoctoWebhook ( doc , template . ExtUserPtr ?. Webhook , userPtr ?. id ) ;
209
291
}
292
+ } catch ( err ) {
293
+ console . log ( 'Err' , err ) ;
210
294
}
295
+ return response . json ( {
296
+ objectId : res . id ,
297
+ signurl : contact . map ( x => ( {
298
+ email : x . email ,
299
+ url : `${ baseUrl . origin } /loadmf/signmicroapp/login/${ res . id } /${ x . email } /${ x . contactPtr . objectId } /${ serverParams } ` ,
300
+ } ) ) ,
301
+ message : 'Document sent successfully!' ,
302
+ } ) ;
303
+ // }
211
304
} else {
212
305
return response . status ( 400 ) . json ( { error : 'Please provide signers properly!' } ) ;
213
306
}
0 commit comments