-
Notifications
You must be signed in to change notification settings - Fork 0
/
queued-function.js
45 lines (44 loc) · 1.13 KB
/
queued-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
const request = require('request');
const as = require('async');
const _ = require('lodash');
const loader = require('fli-webtask').lib.loader;
/**
* @param context {WebtaskContext}
*/
module.exports = function(context, cb) {
if(context.secrets.token !== context.query.token) {
return cb('No token.');
}
let _id = _.get(context, 'body._id');
if(!_id) {
return cb('No _id provided.');
}
let db = _.get(context, 'body.db');
if(!db) {
return cb('No db provided.');
}
let queueNameKey = db.split('-')[0];
let queueName = context.secrets[queueNameKey];
if(!queueName) {
return cb('No queueName provided.');
}
return as.waterfall([
(next) => loader({
method: 'post',
url: `${context.secrets.queueFunction}/${queueName}/add`,
json: { msg: {
id: _id,
db: db
}},
qs: {token: context.secrets.token}
}, next),
(msg, next) => loader({
method: 'put',
url: `${context.secrets.storeFunction}/${context.body.db}/${context.body._id}`,
qs: {token: context.secrets.token},
json: {
state: 'queued'
}
}, () => next(null, msg))
], cb);
};