Created
June 12, 2017 10:11
-
-
Save freeart/bac908df58ccc6f99683071e61a08352 to your computer and use it in GitHub Desktop.
prevent callback hell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async.auto({ | |
loginSuperAdmin: (cb) => { | |
send({ | |
uri: config.api + "/supervisor/auth/", | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json; charset=utf-8' | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"email": "[email protected]", | |
"password": "xxxxxx" | |
} | |
}, cb); | |
}, | |
createAdmin: ["loginSuperAdmin", (scope, cb) => { | |
send({ | |
uri: config.api + "/admin/supervisor/", | |
method: 'PUT', | |
headers: { | |
'content-type': 'application/json; charset=utf-8', | |
'authorization': scope.loginSuperAdmin.token | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"login": config.admin, | |
"tenant": config.tenant, | |
"detail": JSON.stringify({"fullname": "God Mio"}) | |
} | |
}, cb); | |
}], | |
activateAdmin: ["createAdmin", (scope, cb) => { | |
send({ | |
uri: config.api + "/supervisor/password/", | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json; charset=utf-8' | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"email": config.admin, | |
"password": "pwd_" + config.admin, | |
"code": SHA1(scope.createAdmin.id + config.admin.toLowerCase() + "xxx").toString() | |
} | |
}, cb); | |
}], | |
loginAdmin: ["activateAdmin", (scope, cb) => { | |
send({ | |
uri: config.api + "/supervisor/auth/", | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json; charset=utf-8' | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"email": config.admin, | |
"password": "pwd_" + config.admin | |
} | |
}, cb); | |
}] | |
}, (err, topScope) => { | |
if (err) { | |
throw Error(err); | |
return; | |
} | |
async.eachLimit(data, 4, (record, cb) => { | |
async.auto({ | |
createDevice: (cb) => { | |
send({ | |
uri: config.api + "/admin/device/", | |
method: 'PUT', | |
headers: { | |
'content-type': 'application/json; charset=utf-8', | |
'authorization': topScope.loginSuperAdmin.token | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"number": record.number, | |
"detail": JSON.stringify({"owner": record.login}), | |
"tenant": record.tenant | |
} | |
}, cb); | |
}, | |
createUser: (cb) => { | |
send({ | |
uri: config.api + "/supervisor/", | |
method: 'PUT', | |
headers: { | |
'content-type': 'application/json; charset=utf-8' | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"login": record.login, | |
"detail": JSON.stringify({"fullname": record.fullname}) | |
} | |
}, cb); | |
}, | |
activateUser: ["createUser", (scope, cb) => { | |
send({ | |
uri: config.api + "/supervisor/password/", | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json; charset=utf-8' | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"email": record.login, | |
"password": "pwd_" + record.login, | |
"code": SHA1(scope.createUser.id + record.login.toLowerCase() + "xxx").toString() | |
} | |
}, cb); | |
}], | |
loginUser: ["activateUser", (scope, cb) => { | |
send({ | |
uri: config.api + "/supervisor/auth/", | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json; charset=utf-8' | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"email": record.login, | |
"password": "pwd_" + record.login | |
} | |
}, cb); | |
}], | |
createContainer: ["loginUser", (scope, cb) => { | |
send({ | |
uri: config.api + "/container/", | |
method: 'PUT', | |
headers: { | |
'content-type': 'application/json; charset=utf-8', | |
'authorization': scope.loginUser.token | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"detail": JSON.stringify({"owner": record.login}) | |
} | |
}, cb); | |
}], | |
attachDevice2Container: ["createDevice", "createContainer", (scope, cb) => { | |
send({ | |
uri: config.api + "/container/attach", | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json; charset=utf-8', | |
'authorization': scope.loginUser.token | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"number": record.number | |
}, | |
qs: { | |
id: scope.createContainer.id | |
}, | |
}, cb); | |
}], | |
createContractBattery: ["attachDevice2Container", (scope, cb) => { | |
send({ | |
uri: config.api + "/admin/smartcontract/", | |
method: 'PUT', | |
headers: { | |
'content-type': 'application/json; charset=utf-8', | |
'authorization': topScope.loginAdmin.token | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"containerId": scope.createContainer.id, | |
"script": "battery", | |
"argv": {} | |
} | |
}, cb); | |
}], | |
createContractCounter: ["attachDevice2Container", (scope, cb) => { | |
send({ | |
uri: config.api + "/admin/smartcontract/", | |
method: 'PUT', | |
headers: { | |
'content-type': 'application/json; charset=utf-8', | |
'authorization': topScope.loginAdmin.token | |
}, | |
json: true, | |
followAllRedirects: true, | |
body: { | |
"containerId": scope.createContainer.id, | |
"script": "km", | |
"argv": {target: 100000} | |
} | |
}, cb); | |
}], | |
}, (err, scope) => { | |
console.log("iteration".green, record.login) | |
err && console.error(scope, err); | |
cb(err); | |
}) | |
}, (err) => { | |
console.error("final".red, err) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment