buli
2022-04-08 f1c525740c6a45b875d8ed96fb0ddb68c97ef3df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class AWSService {
    static sfSessionId = '';
    static insertModule = 'Insert AWS ';
    static updateModule = 'Update AWS ';
    static queryModule = 'Query AWS ';
    static searchModule = 'Search AWS ';
    static confirmTrans = 'Confirm Transaction To AWS';
    static successStatus = 'success';
    static failStatus = 'fail';
    static insertCalloutLog(module,url,request,response,status){
        if(this.sfSessionId){
            sforce.connection.sessionId = this.sfSessionId;
            let transLog = new sforce.SObject('Transaction_Log__c');
            transLog.AWS_Data_Id__c = '';
            transLog.Module__c = module;
            transLog.Interface_URL__c = url;
            transLog.Request__c = request;
            transLog.Response__c = response;
            transLog.Status__c = status;
            let insertLogResult = sforce.connection.create([transLog]);     
            if(insertLogResult[0].getBoolean(this.successStatus)) {
                console.log('Insert Log Id: ' + insertLogResult[0].id);
                return insertLogResult[0].id;
            }else {
                console.log('Faield to insert log');
                return '';
            }
        }       
    }
    //Search Contact
    static search(searchUrl,requestSearchPayload,searchBack,token) {
        fetch(searchUrl, {
            method: 'POST',
            body: requestSearchPayload,
            headers: {
                'Content-Type': 'application/json',
                'pi-token': token
            }
        }).then((data) => {
            return data.json();
        }).then((result) => {
            this.insertCalloutLog(this.searchModule,searchUrl,JSON.stringify(requestSearchPayload),JSON.stringify(result),this.successStatus);
            searchBack(result);      
        }).catch(error => {
            this.insertCalloutLog(this.searchModule,searchUrl,JSON.stringify(requestSearchPayload),JSON.stringify(error),this.failStatus);
            console.log(error);
        });
    }
 
    //query
    static query(queryURL, awsDataId, queryback, token) {
        fetch(queryURL + '?dataId=' + awsDataId, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'pi-token': token
            }
        }).then((data) => {         
            return data.json();
        }).then((data) => {
            this.insertCalloutLog(this.queryModule,queryURL,queryURL + '?dataId=' + awsDataId,JSON.stringify(data),this.successStatus);
            queryback(data);
        }).catch(error => {
            this.insertCalloutLog(this.queryModule,queryURL,queryURL + '?dataId=' + awsDataId,JSON.stringify(error),this.failStatus);
            console.log(error);
        });
    }
 
    //queryTSRepair
    static queryTSRepair(queryURL, awsDataId, queryback, token,number) {
        fetch(queryURL + '?dataId=' + awsDataId, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'pi-token': token
            }
        }).then((data) => {         
            return data.json();
        }).then((data) => {
            this.insertCalloutLog(this.queryModule,queryURL,queryURL + '?dataId=' + awsDataId,JSON.stringify(data),this.successStatus);
            queryback(data,number);
        }).catch(error => {
            this.insertCalloutLog(this.queryModule,queryURL,queryURL + '?dataId=' + awsDataId,JSON.stringify(error),this.failStatus);
            console.log(error);
        });
    }
 
    //insert
    static insert(newURL, payloadJson, payloadForNewPI, controllerSaveMethod, token, transactionURL,isNewMode,insertOrUpdateBack,redirectCallBack) {
        console.log('Process New PI Data');
        console.log(JSON.stringify(payloadForNewPI));
        fetch(newURL, {
            method: 'POST',
            body: payloadForNewPI,
            headers: {
                'Content-Type': 'application/json',
                'pi-token': token
            }
        }).then((data) => {
            console.log('data=' + JSON.stringify(data));
            return data.json();
        }).then((result) => {
            this.insertCalloutLog(this.insertModule,newURL,JSON.stringify(payloadForNewPI),JSON.stringify(result),this.successStatus);
            AWSService.back(result, payloadJson, transactionURL, token, controllerSaveMethod,isNewMode,insertOrUpdateBack,redirectCallBack);
        }).catch(error => {
            this.insertCalloutLog(this.insertModule,newURL,JSON.stringify(payloadForNewPI),JSON.stringify(error),this.failStatus);
            console.log(error);
        });
    }
 
    //update
    static update(updateURL, payloadJson, payloadForNewPI, controllerSaveMethod, token, transactionURL,isNewMode,insertOrUpdateBack,redirectCallBack) {
        console.log('Process New PI Data');
        console.log(JSON.stringify(payloadForNewPI));
        fetch(updateURL, {
            method: 'POST',
            body: payloadForNewPI,
            headers: {
                'Content-Type': 'application/json',
                'pi-token': token
            }
        }).then((data) => {
            console.log('data=' + JSON.stringify(data));
            return data.json();
        }).then((result) => {
            this.insertCalloutLog(this.updateModule,updateURL,JSON.stringify(payloadForNewPI),JSON.stringify(result),this.successStatus);
            AWSService.back(result, payloadJson, transactionURL, token, controllerSaveMethod,isNewMode,insertOrUpdateBack,redirectCallBack);
        }).catch(error => {
            this.insertCalloutLog(this.updateModule,updateURL,JSON.stringify(payloadForNewPI),JSON.stringify(error),this.failStatus);
            console.log(error);
        });
    }
 
    //update
    static post(postURL, payloadForNewPI, callback, token) {
        console.log('Process New PI Data');
        console.log(JSON.stringify(payloadForNewPI));
        fetch(postURL, {
            method: 'POST',
            body: payloadForNewPI,
            headers: {
                'Content-Type': 'application/json',
                'pi-token': token
            }
        }).then((data) => {
            console.log('data=' + JSON.stringify(data));
            return data.json();
        }).then((result) => {
            this.insertCalloutLog(this.insertModule,postURL,JSON.stringify(payloadForNewPI),JSON.stringify(result),this.successStatus);
            callback(result);
        }).catch(error => {
            this.insertCalloutLog(this.insertModule,postURL,JSON.stringify(payloadForNewPI),JSON.stringify(error),this.failStatus);
            console.log(error);
        });
    }
 
    static sfdcBack = function sfdcBack(event, result, transId, token, transactionURL,redirectCallBack) {
        let sfId = '';
        if (event.status) {
            console.log('sf Id from SF Backend:' + JSON.stringify(result));
            if (result.status == 'success') {
                sfId = result.recordId;
                let transParameters = {
                    txId: transId,
                    sfRecordId:sfId,
                    isSuccess: 1
                };
                console.log(transParameters);
                fetch(transactionURL, {
                    method: 'POST',
                    body: JSON.stringify(transParameters),
                    headers: {
                        'Content-Type': 'application/json',
                        'pi-token': token
                    }
                }).then((result) => {
                    this.insertCalloutLog(this.confirmTrans,transactionURL,JSON.stringify(transParameters),JSON.stringify(result),this.successStatus);
                    redirectCallBack(sfId,'');
                }).catch(error => {
                    this.insertCalloutLog(this.confirmTrans,transactionURL,JSON.stringify(transParameters),JSON.stringify(error),this.failStatus);
                    console.log(error);
                });
            } else {
                let errorMsg = result.message;
                let transParameters = {
                    txId: transId,
                    sfRecordId:sfId,
                    isSuccess: 0
                };
                console.log(transParameters);
                fetch(transactionURL, {
                    method: 'POST',
                    body: JSON.stringify(transParameters),
                    headers: {
                        'Content-Type': 'application/json',
                        'pi-token': token
                    }
                }).then((result) => {
                    this.insertCalloutLog(this.confirmTrans,transactionURL,JSON.stringify(transParameters),JSON.stringify(result),this.successStatus);
                    redirectCallBack('',errorMsg);
                }).catch(error => {
                    this.insertCalloutLog(this.confirmTrans,transactionURL,JSON.stringify(transParameters),JSON.stringify(error),this.failStatus);
                    console.log(error);
                });
            }
        }
    }
 
    // AWSService.back(result, payloadJson, transactionURL, token, controllerSaveMethod);
    static back = function back(result, payloadJson, transactionURL, token, controllerSaveMethod,isNewMode,insertOrUpdateBack,redirectCallBack) {
        let payloadJsonStr = JSON.stringify(insertOrUpdateBack(payloadJson, result,isNewMode));
        let transId = result.txId + '';
        Visualforce.remoting.Manager.invokeAction(
            controllerSaveMethod, // example '{!$RemoteAction.NewAndEditLeadController.saveLead}'
            payloadJsonStr, transId, isNewMode,
            function (result, event) {
                AWSService.sfdcBack(event, result, transId, token, transactionURL,redirectCallBack);
            },
            { escape: true }
        );
    }
};