-
Notifications
You must be signed in to change notification settings - Fork 0
/
informed-function.js
51 lines (50 loc) · 1.58 KB
/
informed-function.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
const fli = require('fli-webtask');
const request = fli.npm.request;
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
const informed = (context, next) => loader({
method: 'put',
url: `${context.secrets.storeFunction}/${context.body.db}/${context.body._id}`,
qs: {token: context.secrets.token},
json: {
state: 'informed'
}
}, () => next());
/**
* @param context {WebtaskContext}
*/
module.exports = function(context, cb) {
if(context.secrets.token !== context.query.token) {
return cb('No token.');
}
console.log(`- informed`);
if(!_.get(context, 'body._id')) {
return cb('No _id provided.');
}
if(!_.chain(context).get('body.payload.info.title').isEmpty().value()) {
return informed(context, () => cb('Info already provided.'));
}
var asin = _.get(context, 'body.payload.asin');
var db = _.get(context, 'body.db');
var market = context.secrets[`${db}-market`];
console.log(`-- asin: ${asin}`);
if(!!asin) {
return as.waterfall([
(next) => loader({
url: `${context.secrets.amazonFunction}/${market}/lookup/${asin}`,
qs: {token: context.secrets.token}
}, (err, info) => next(null, info)),
(info, next) => loader({
method: 'patch',
url: `${context.secrets.storeFunction}/${context.body.db}/${context.body._id}`,
qs: {token: context.secrets.token},
json: {info: info}
}, () => next(null, info)),
(info, next) => informed(context, () => next(null, info))
], cb);
}
return as.waterfall([
(next) => informed(context, () => next())
], cb);
};