-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher-function.js
53 lines (51 loc) · 1.37 KB
/
publisher-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
52
53
const fli = require('fli-webtask');
const request = fli.npm.request;
const _ = fli.npm.lodash;
const as = fli.npm.async;
const loader = fli.lib.loader;
const publisher = (context) => (params, next) => as.map(
params.sources,
(source, next) => {
if(!!_.get(context, `body.payload.${source}Published`)) {
return next(null, `${source} already published`);
}
console.log(`-- published ${source}`);
loader({
method: 'post',
url: context.secrets[source],
qs: {token: context.secrets.token},
json: context.body
}, () => loader({
method: 'patch',
url: `${context.secrets.storeFunction}/${context.body.db}/${context.body._id}`,
qs: {token: context.secrets.token},
json: _.once(() => {
var json = {};
json[`${source}Published`] = true;
return json;
})()
}, () => {}));
return next();
},
() => next()
);
/**
* @param context {WebtaskContext}
*/
module.exports = function(context, cb) {
if(context.secrets.token !== context.query.token) {
return cb('No token.');
}
if(!_.get(context, 'body')) {
return cb('No body provided.');
}
if(!_.get(context, 'body._id')) {
return cb('No _id provided.');
}
return as.waterfall([
(next) => context.storage.get(next),
(storage, next) => publisher(context)({
sources: storage.sources
}, () => next()),
], cb);
};