@@ -6,7 +6,7 @@ export default async function createDocumentWithTemplate(request, response) {
6
6
const folderId = request . body . folderId ;
7
7
const templateId = request . params . template_id ;
8
8
const protocol = customAPIurl ( ) ;
9
-
9
+ const baseUrl = new URL ( process . env . SERVER_URL ) ;
10
10
try {
11
11
const reqToken = request . headers [ 'x-api-token' ] ;
12
12
if ( ! reqToken ) {
@@ -19,8 +19,9 @@ export default async function createDocumentWithTemplate(request, response) {
19
19
// Valid Token then proceed request
20
20
const userPtr = token . get ( 'userId' ) ;
21
21
22
- const templateQyuery = new Parse . Query ( 'contracts_Template' ) ;
23
- const templateRes = await templateQyuery . get ( templateId , { useMasterKey : true } ) ;
22
+ const templateQuery = new Parse . Query ( 'contracts_Template' ) ;
23
+ templateQuery . include ( 'ExtUserPtr' ) ;
24
+ const templateRes = await templateQuery . get ( templateId , { useMasterKey : true } ) ;
24
25
if ( templateRes ) {
25
26
const template = JSON . parse ( JSON . stringify ( templateRes ) ) ;
26
27
if ( template ?. Placeholders ?. length > 0 ) {
@@ -30,7 +31,7 @@ export default async function createDocumentWithTemplate(request, response) {
30
31
signers . length <= template ?. Placeholders ?. length ;
31
32
const placeholder =
32
33
signers . length > emptyplaceholder . length ? template . Placeholders : emptyplaceholder ;
33
- const updateSigners = placeholder . every ( y => signers ?. some ( x => x . Role === y . Role ) ) ;
34
+ const updateSigners = placeholder . every ( y => signers ?. some ( x => x . role === y . Role ) ) ;
34
35
// console.log('isValid ', isValid);
35
36
if ( isValid && updateSigners ) {
36
37
const folderPtr = {
@@ -48,16 +49,16 @@ export default async function createDocumentWithTemplate(request, response) {
48
49
object . set ( 'Description' , template . Description ) ;
49
50
}
50
51
let templateSigner = template ?. Signers ? template ?. Signers : [ ] ;
52
+ let contact = [ ] ;
51
53
if ( signers && signers . length > 0 ) {
52
54
let parseSigners = [ ...signers ] ;
53
55
let createContactUrl = protocol + '/v1/createcontact' ;
54
56
55
- let contact = [ ] ;
56
57
for ( const obj of parseSigners ) {
57
58
const body = {
58
- name : obj ?. Name || '' ,
59
- email : obj ?. Email || '' ,
60
- phone : obj ?. Phone || '' ,
59
+ name : obj ?. name || '' ,
60
+ email : obj ?. email || '' ,
61
+ phone : obj ?. phone || '' ,
61
62
} ;
62
63
try {
63
64
const res = await axios . post ( createContactUrl , body , {
@@ -89,7 +90,7 @@ export default async function createDocumentWithTemplate(request, response) {
89
90
object . set ( 'Signers' , [ ...templateSigner , ...contactPtrs ] ) ;
90
91
91
92
let updatedPlaceholder = template ?. Placeholders ?. map ( x => {
92
- let matchingSigner = contact . find ( y => x . Role && x . Role === y . Role ) ;
93
+ let matchingSigner = contact . find ( y => x . Role && x . Role === y . role ) ;
93
94
94
95
if ( matchingSigner ) {
95
96
return {
@@ -109,7 +110,11 @@ export default async function createDocumentWithTemplate(request, response) {
109
110
}
110
111
object . set ( 'URL' , template . URL ) ;
111
112
object . set ( 'CreatedBy' , template . CreatedBy ) ;
112
- object . set ( 'ExtUserPtr' , template . ExtUserPtr ) ;
113
+ object . set ( 'ExtUserPtr' , {
114
+ __type : 'Pointer' ,
115
+ className : 'contracts_Users' ,
116
+ objectId : template . ExtUserPtr . objectId ,
117
+ } ) ;
113
118
if ( folderId ) {
114
119
object . set ( 'Folder' , folderPtr ) ;
115
120
}
@@ -120,10 +125,85 @@ export default async function createDocumentWithTemplate(request, response) {
120
125
newACL . setWriteAccess ( userPtr . id , true ) ;
121
126
object . setACL ( newACL ) ;
122
127
const res = await object . save ( null , { useMasterKey : true } ) ;
123
- return response . json ( {
124
- objectId : res . id ,
125
- url : protocol + '/load/signmicroapp/placeholdersign/' + res . id ,
128
+
129
+ const newDate = new Date ( ) ;
130
+ newDate . setDate ( newDate . getDate ( ) + 15 ) ;
131
+ const localExpireDate = newDate . toLocaleDateString ( 'en-US' , {
132
+ day : 'numeric' ,
133
+ month : 'long' ,
134
+ year : 'numeric' ,
126
135
} ) ;
136
+ let sender = template . ExtUserPtr . Email ;
137
+ let sendMail ;
138
+ const serverUrl = process . env . SERVER_URL ;
139
+ const newServer = serverUrl . replaceAll ( '/' , '%2F' ) ;
140
+ const serverParams = `${ newServer } %2F&${ process . env . APP_ID } &contracts` ;
141
+
142
+ for ( let i = 0 ; i < contact . length ; i ++ ) {
143
+ try {
144
+ const imgPng = 'https://qikinnovation.ams3.digitaloceanspaces.com/logo.png' ;
145
+ let url = `${ process . env . SERVER_URL } /functions/sendmailv3/` ;
146
+ const headers = {
147
+ 'Content-Type' : 'application/json' ,
148
+ 'X-Parse-Application-Id' : process . env . APP_ID ,
149
+ 'X-Parse-Master-Key' : process . env . MASTER_KEY ,
150
+ } ;
151
+
152
+ const objectId = contact [ i ] . contactPtr . objectId ;
153
+
154
+ const hostUrl = baseUrl . origin + '/loadmf/signmicroapp' ;
155
+ let signPdf = `${ hostUrl } /login/${ res . id } /${ contact [ i ] . email } /${ objectId } /${ serverParams } ` ;
156
+ const openSignUrl = 'https://www.opensignlabs.com/contact-us' ;
157
+ const orgName = template . ExtUserPtr . Company ? template . ExtUserPtr . Company : '' ;
158
+ const themeBGcolor = '#47a3ad' ;
159
+ let params = {
160
+ recipient : contact [ i ] . email ,
161
+ subject : `${ template . ExtUserPtr . Name } has requested you to sign ${ template . ExtUserPtr . Name } ` ,
162
+ from : sender ,
163
+ html :
164
+ "<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=" +
165
+ imgPng +
166
+ " height='50' style='padding: 20px,width:170px,height:40px' /></div> <div style=' padding: 2px;font-family: system-ui;background-color:" +
167
+ themeBGcolor +
168
+ ";'><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;'> " +
169
+ template . ExtUserPtr . Name +
170
+ ' has requested you to review and sign <strong> ' +
171
+ template . Name +
172
+ "</strong>.</p><div style='padding: 5px 0px 5px 25px;display: flex;flex-direction: row;justify-content: space-around;'><table> <tr> <td style='font-weight:bold;font-family:sans-serif;font-size:15px'>Sender</td> <td> </td> <td style='color:#626363;font-weight:bold'>" +
173
+ sender +
174
+ "</td></tr><tr><td style='font-weight:bold;font-family:sans-serif;font-size:15px'>Organization</td> <td> </td><td style='color:#626363;font-weight:bold'> " +
175
+ orgName +
176
+ "</td></tr> <tr> <td style='font-weight:bold;font-family:sans-serif;font-size:15px'>Expire on</td><td> </td> <td style='color:#626363;font-weight:bold'>" +
177
+ localExpireDate +
178
+ "</td></tr><tr> <td></td> <td> </td></tr></table> </div> <div style='margin-left:70px'><a href=" +
179
+ signPdf +
180
+ "> <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 " +
181
+ sender +
182
+ ' directly.If you think this email is inappropriate or spam, you may file a complaint with OpenSign™ <a href= ' +
183
+ openSignUrl +
184
+ ' target=_blank>here</a>.</p> </div></div></body> </html>' ,
185
+ } ;
186
+ sendMail = await axios . post ( url , params , { headers : headers } ) ;
187
+ } catch ( error ) {
188
+ console . log ( 'error' , error ) ;
189
+ }
190
+ }
191
+
192
+ if ( sendMail . data . result . status === 'success' ) {
193
+ const user = contact . find ( x => x . email === template . ExtUserPtr . Email ) ;
194
+ if ( user && user . email ) {
195
+ return response . json ( {
196
+ objectId : res . id ,
197
+ url : `${ baseUrl . origin } /loadmf/signmicroapp/login/${ res . id } /${ user . email } /${ user . contactPtr . objectId } /${ serverParams } ` ,
198
+ message : 'Document sent successfully!' ,
199
+ } ) ;
200
+ } else {
201
+ return response . json ( {
202
+ objectId : res . id ,
203
+ message : 'Document sent successfully!' ,
204
+ } ) ;
205
+ }
206
+ }
127
207
} else {
128
208
return response . status ( 400 ) . json ( { error : 'Please provide signers properly!' } ) ;
129
209
}
0 commit comments