1
- const randomId = ( ) => Math . floor ( 1000 + Math . random ( ) * 9000 ) ;
1
+ // import batchQuery from './batchquery.js';
2
+
3
+ // const randomId = () => Math.floor(1000 + Math.random() * 9000);
2
4
export default async function createDocument ( request , response ) {
3
5
const name = request . body . Title ;
4
6
const note = request . body . Note ;
5
7
const description = request . body . Description ;
6
8
const signers = request . body . Signers ;
7
9
const folderId = request . body . FolderId ;
8
- // const file = request.body.file;
9
- const url = process . env . SERVER_URL ;
10
- const fileData = request . files [ 0 ] ? request . files [ 0 ] . buffer : null ;
10
+ const base64File = request . body . file ;
11
+ const url = request ?. get ( 'host' ) ;
12
+ const fileData = request . files ?. [ 0 ] ? request . files [ 0 ] . buffer : null ;
13
+ console . log ( 'fileData ' , fileData ) ;
11
14
try {
12
15
const reqToken = request . headers [ 'x-api-token' ] ;
13
16
if ( ! reqToken ) {
@@ -18,55 +21,116 @@ export default async function createDocument(request, response) {
18
21
const token = await tokenQuery . first ( { useMasterKey : true } ) ;
19
22
if ( token !== undefined ) {
20
23
// Valid Token then proceed request
21
- const userPtr = token . get ( 'userId' ) ;
24
+ if ( signers ) {
25
+ const userPtr = token . get ( 'userId' ) ;
26
+ let fileUrl ;
27
+ if ( request . files ?. [ 0 ] ) {
28
+ const file = new Parse . File ( request . files ?. [ 0 ] ?. originalname , {
29
+ base64 : fileData ?. toString ( 'base64' ) ,
30
+ } ) ;
31
+ await file . save ( { useMasterKey : true } ) ;
32
+ fileUrl = file . url ( ) ;
33
+ } else {
34
+ const file = new Parse . File ( `${ name } .pdf` , {
35
+ base64 : base64File ,
36
+ } ) ;
37
+ await file . save ( { useMasterKey : true } ) ;
38
+ fileUrl = file . url ( ) ;
39
+ }
40
+ const contractsUser = new Parse . Query ( 'contracts_Users' ) ;
41
+ contractsUser . equalTo ( 'UserId' , userPtr ) ;
42
+ const extUser = await contractsUser . first ( { useMasterKey : true } ) ;
43
+ const extUserPtr = {
44
+ __type : 'Pointer' ,
45
+ className : 'contracts_Users' ,
46
+ objectId : extUser . id ,
47
+ } ;
22
48
23
- const file = new Parse . File ( request . files ?. [ 0 ] ?. originalname , {
24
- base64 : fileData . toString ( 'base64' ) ,
25
- } ) ;
26
- await file . save ( { useMasterKey : true } ) ;
27
- const fileUrl = file . url ( ) ;
28
- const contractsUser = new Parse . Query ( 'contracts_Users' ) ;
29
- contractsUser . equalTo ( 'UserId' , userPtr ) ;
30
- const extUser = await contractsUser . first ( { useMasterKey : true } ) ;
31
- const extUserPtr = { __type : 'Pointer' , className : 'contracts_Users' , objectId : extUser . id } ;
49
+ const folderPtr = {
50
+ __type : 'Pointer' ,
51
+ className : 'contracts_Document' ,
52
+ objectId : folderId ,
53
+ } ;
32
54
33
- const folderPtr = { __type : 'Pointer' , className : 'contracts_Document' , objectId : folderId } ;
55
+ const object = new Parse . Object ( 'contracts_Document' ) ;
56
+ object . set ( 'Name' , name ) ;
57
+ if ( note ) {
58
+ object . set ( 'Note' , note ) ;
59
+ }
60
+ if ( description ) {
61
+ object . set ( 'Description' , description ) ;
62
+ }
63
+ object . set ( 'URL' , fileUrl ) ;
64
+ object . set ( 'CreatedBy' , userPtr ) ;
65
+ object . set ( 'ExtUserPtr' , extUserPtr ) ;
66
+ if ( signers ) {
67
+ let parseSigners ;
68
+ if ( base64File ) {
69
+ parseSigners = signers ;
70
+ } else {
71
+ parseSigners = JSON . parse ( signers ) ;
72
+ }
34
73
35
- const object = new Parse . Object ( 'contracts_Document' ) ;
36
- object . set ( 'Name' , name ) ;
37
- if ( note ) {
38
- object . set ( 'Note' , note ) ;
39
- }
40
- if ( description ) {
41
- object . set ( 'Description' , description ) ;
42
- }
43
- object . set ( 'URL' , fileUrl ) ;
44
- object . set ( 'CreatedBy' , userPtr ) ;
45
- object . set ( 'ExtUserPtr' , extUserPtr ) ;
46
- if ( signers ) {
47
- const parseSigners = JSON . parse ( signers ) ;
48
- const placeholders = parseSigners . map ( x => ( {
49
- email : x ,
50
- Id : randomId ( ) ,
51
- Role : '' ,
52
- blockColor : '' ,
53
- signerObjId : '' ,
54
- signerPtr : { } ,
55
- placeHolder : [ ] ,
56
- } ) ) ;
57
- object . set ( 'Placeholders' , placeholders ) ;
58
- }
59
- if ( folderId ) {
60
- object . set ( 'Folder' , folderPtr ) ;
74
+ const contactbook = new Parse . Query ( 'contracts_Contactbook' ) ;
75
+ contactbook . equalTo ( 'UserId' , userPtr ) ;
76
+ contactbook . notEqualTo ( 'IsDeleted' , true ) ;
77
+ contactbook . containedIn ( 'Email' , parseSigners ) ;
78
+ const contactbookRes = await contactbook . find ( { useMasterKey : true } ) ;
79
+ console . log ( 'contactbookRes ' , contactbookRes ) ;
80
+ const parseContactbookRes = JSON . parse ( JSON . stringify ( contactbookRes ) ) ;
81
+ // console.log('userPtr ', userPtr);
82
+ // const newContacts = parseSigners
83
+ // .filter(x => !parseContactbookRes.some(y => y.Email === x))
84
+ // .map(email => ({
85
+ // method: 'POST',
86
+ // path: '/app/classes/contracts_Contactbook',
87
+ // body: {
88
+ // Email: email,
89
+ // CreatedBy: { __type: 'Pointer', className: '_User', objectId: userPtr.id },
90
+ // UserId: { __type: 'Pointer', className: '_User', objectId: userPtr.id },
91
+ // Role: 'contracts_Guest',
92
+ // IsDeleted: false,
93
+ // ACL: {
94
+ // [userPtr.id]: { read: true, write: true },
95
+ // },
96
+ // },
97
+ // }));
98
+ // console.log('newContacts', newContacts);
99
+ let contactSigners = parseContactbookRes ?. map ( x => ( {
100
+ __type : 'Pointer' ,
101
+ className : 'contracts_Contactbook' ,
102
+ objectId : x . objectId ,
103
+ } ) ) ;
104
+ // if (newContacts.length > 0) {
105
+ // const batchRes = await batchQuery(newContacts);
106
+ // const contacts = batchRes?.map(x => ({
107
+ // __type: 'Pointer',
108
+ // className: 'contracts_Contactbook',
109
+ // objectId: x.success.objectId,
110
+ // }));
111
+ // contactSigners = [...contactSigners, ...contacts];
112
+ // }
113
+ // console.log('contactsigners', contactSigners);
114
+
115
+ object . set ( 'Signers' , contactSigners ) ;
116
+ }
117
+ if ( folderId ) {
118
+ object . set ( 'Folder' , folderPtr ) ;
119
+ }
120
+ const newACL = new Parse . ACL ( ) ;
121
+ newACL . setPublicReadAccess ( false ) ;
122
+ newACL . setPublicWriteAccess ( false ) ;
123
+ newACL . setReadAccess ( userPtr . id , true ) ;
124
+ newACL . setWriteAccess ( userPtr . id , true ) ;
125
+ object . setACL ( newACL ) ;
126
+ const res = await object . save ( null , { useMasterKey : true } ) ;
127
+ return response . json ( {
128
+ objectId : res . id ,
129
+ url : 'https://' + url + '/load/signmicroapp/placeholdersign/' + res . id ,
130
+ } ) ;
131
+ } else {
132
+ return response . status ( 400 ) . json ( { error : 'Please provide signers!' } ) ;
61
133
}
62
- const newACL = new Parse . ACL ( ) ;
63
- newACL . setPublicReadAccess ( false ) ;
64
- newACL . setPublicWriteAccess ( false ) ;
65
- newACL . setReadAccess ( userPtr . id , true ) ;
66
- newACL . setWriteAccess ( userPtr . id , true ) ;
67
- object . setACL ( newACL ) ;
68
- const res = await object . save ( null , { useMasterKey : true } ) ;
69
- return response . json ( { objectId : res . id , url : url } ) ;
70
134
} else {
71
135
return response . status ( 405 ) . json ( { error : 'Invalid API Token!' } ) ;
72
136
}
0 commit comments