-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsendBatch.js
39 lines (30 loc) · 1.02 KB
/
sendBatch.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
const Request = require("../Request");
const Errors = require("../Errors");
const buildInputData = require("../util/buildInputData");
module.exports = (batch, sender, Result, keyTranslationFormat) => {
if (batch.isEmpty()) throw new Errors.BatchEmptyError;
let request = new Request();
if (batch.length() === 1) request.parameters = generateRequestPayload(batch)[0];
else request.payload = generateRequestPayload(batch);
return new Promise((resolve, reject) => {
sender.send(request)
.then(response => {
if (response.error) reject(response.error);
resolve(assignResultsToLookups(batch, response));
})
.catch(reject);
});
function generateRequestPayload(batch) {
return batch.lookups.map((lookup) => {
return buildInputData(lookup, keyTranslationFormat);
});
}
function assignResultsToLookups(batch, response) {
response.payload.map(rawResult => {
let result = new Result(rawResult);
let lookup = batch.getByIndex(result.inputIndex);
lookup.result.push(result);
});
return batch;
}
};