Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / services / typeaheads / elastic-bulk-tools.js
blobed4132a9671f7622a55bddf0393be8515625a6fd
1 'use strict';
3 var IS_DEVELOPMENT = process.env.NODE_ENV === 'dev';
5 module.exports = {
6 // expects an array of updates where each element is a valid req for elasticClient.update
7 createBulkUpdate: function(updates) {
8 return {
9 body: updates.reduce(function(body, update) {
10 body.push({
11 update: {
12 _index: update.index,
13 _type: update.type,
14 _id: update.id,
15 _retry_on_conflict: update._retry_on_conflict
17 });
18 body.push(update.body);
19 return body;
20 }, [])
23 findErrors: function(req, res) {
24 if (IS_DEVELOPMENT) {
25 // ERRORS for bulk updater disabled in dev until
26 // https://github.com/troupe/gitter-webapp/issues/2080
27 return;
30 if (!res.errors) return;
32 var errors = [];
34 res.items.forEach(function(item, index) {
35 if (item.update.error) {
36 errors.push({
37 path: req.body[index * 2],
38 body: req.body[index * 2 + 1],
39 resp: item
40 });
42 });
44 if (!errors.length) return;
46 return new Error(
47 'elastic bulk upload failed for some. failures: ' + JSON.stringify(errors, null, 2)