-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateFlowUsingCallback.js
133 lines (128 loc) · 4 KB
/
createFlowUsingCallback.js
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
const request = require('request');
const async = require('async');
const accessToken ='<Provide your IO Token>';
const url = 'https://api.staging.integrator.io/v1';
const authentication = (relativeURI='', body= '', method= '') => {
let options={
method: method,
uri :url+relativeURI,
headers:{
Authorization:'Bearer '+accessToken,
'Content-Type':'application/json',
'charset':'utf8'
},
json: body
}
return options;
}
let connectcionId, exportId, importId;
const createConnection = (callback) => {
var connectionJson = {
'type': 'http',
'name': 'Backend import Shopify Connection',
'http': {
'mediaType': 'json',
'baseURI': 'https://io-staging-demo-store.myshopify.com/admin/api/2021-01/',
'ping': {
'relativeURI': 'products.json',
'method': 'GET'
},
'auth': {
'type': 'basic',
'basic': {
'username': 'a59f3815557785b86989b44161840c30',
'password': '<Provide your Shopify private App secret key>'
},
'oauth': {
'clientCredentialsLocation': 'body'
}
}
}
};
request(authentication('/connections',connectionJson,'POST'), (err,response,body) => {
if(err) console.log(JSON.stringify(response));
callback(body);
})
}
const exportGenerator = (callback)=> {
createConnection((connection)=>{
const Exportjson = {
'name': 'Backend Export HTTP',
'_connectionId': connection._id,
'asynchronous': true,
'http': {
'relativeURI': '/products.json',
'method': 'GET'
},
'adaptorType': 'HTTPExport'
}
request(authentication('/exports',Exportjson,'POST'), (err,response,body) => {
if(err) console.log(JSON.stringify(response));
callback(body)
})
})
}
const importGenerator = (callback)=> {
createConnection((connection)=>{
const Importjson = {
'name': 'Backend Import HTTP',
'_connectionId': connection._id,
'http': {
'relativeURI': [
'/products.json'
],
'method': [
'POST'
]
},
'adaptorType': 'HTTPImport'
}
request(authentication('/imports',Importjson,'POST'), (err,response,body) => {
if(err) console.log(JSON.stringify(response));
callback(body)
})
})
}
const createFlow = (callback) => {
async.series([
(callbackOne) => {
async.parallel([
(exportIdFetcher) => {
exportGenerator((id)=>{
exportId = id._id
console.log("ExportID :",exportId);
return exportIdFetcher(null, exportId);
})
},
(ixportIdFetcher) => {
importGenerator((id)=>{
importId = id._id
console.log("ImportID :",importId);
return ixportIdFetcher(null, importId);
})
}
], (err, res)=>{
if(err) console.log('Error: ', err);
return callbackOne(null, res);
})
},
(callbackOne) => {
var json={
'name': 'Backend Flow for creating New Customer',
'disabled':true,
'_exportId':exportId,
'_importId':importId,
'skipRetries':false
}
request(authentication('/flows', json, 'POST'), (err,response,body) => {
if (err) console.log(JSON.stringify(response));
console.log('FlowID :',body._id)
return callbackOne(null, body)
})
}
], (err, res) => {
if(err) console.log(err);
//console.log(res)
})
}
createFlow();