|
12 | 12 | * Copyright (c) 2014-2019. All rights reserved.
|
13 | 13 | */
|
14 | 14 |
|
15 |
| -const _ = require('lodash') |
16 |
| -const async = require('async') |
17 |
| -const winston = require('../../../logger') |
18 |
| -const es = require('../../../elasticsearch') |
19 |
| -const ticketSchema = require('../../../models/ticket') |
20 |
| -const groupSchema = require('../../../models/group') |
| 15 | +import _ from 'lodash' |
| 16 | +import logger from '../../../logger' |
| 17 | +import es from '../../../elasticsearch' |
| 18 | +import ticketSchema from '../../../models/ticket' |
| 19 | +import { DepartmentModel, GroupModel } from '../../../models' |
| 20 | +import apiUtils from '../apiUtils' |
21 | 21 |
|
22 | 22 | const apiElasticSearch = {}
|
23 |
| -const apiUtil = require('../apiUtils') |
24 | 23 |
|
25 | 24 | apiElasticSearch.rebuild = (req, res) => {
|
26 | 25 | es.rebuildIndex()
|
27 | 26 |
|
28 |
| - return apiUtil.sendApiSuccess(res) |
| 27 | + return apiUtils.sendApiSuccess(res) |
29 | 28 | }
|
30 | 29 |
|
31 | 30 | apiElasticSearch.status = async (req, res) => {
|
@@ -65,118 +64,80 @@ apiElasticSearch.status = async (req, res) => {
|
65 | 64 | response.isRebuilding = global.esRebuilding === true
|
66 | 65 | response.inSync = response.dbCount === response.indexCount
|
67 | 66 |
|
68 |
| - return apiUtil.sendApiSuccess(res, { status: response }) |
| 67 | + return apiUtils.sendApiSuccess(res, { status: response }) |
69 | 68 | } catch (e) {
|
70 |
| - if (process.env.NODE_ENV === 'development') winston.warn(e.message) |
| 69 | + if (process.env.NODE_ENV === 'development') logger.warn(e.message) |
71 | 70 |
|
72 |
| - return apiUtil.sendApiError(res, 500, e.message) |
| 71 | + return apiUtils.sendApiError(res, 500, e.message) |
73 | 72 | }
|
74 |
| - |
75 |
| - // async.parallel( |
76 |
| - // [ |
77 |
| - // function (done) { |
78 |
| - // return es.checkConnection(done) |
79 |
| - // }, |
80 |
| - // function (done) { |
81 |
| - // es.getIndexCount(function (err, data) { |
82 |
| - // if (err) return done(err) |
83 |
| - // response.indexCount = !_.isUndefined(data.count) ? data.count : 0 |
84 |
| - // return done() |
85 |
| - // }) |
86 |
| - // }, |
87 |
| - // function (done) { |
88 |
| - // ticketSchema.getCount(function (err, count) { |
89 |
| - // if (err) return done(err) |
90 |
| - // response.dbCount = count |
91 |
| - // return done() |
92 |
| - // }) |
93 |
| - // } |
94 |
| - // ], |
95 |
| - // function (err) { |
96 |
| - // if (err) return res.status(500).json({ success: false, error: err }) |
97 |
| - // |
98 |
| - // response.esStatus = global.esStatus |
99 |
| - // response.isRebuilding = global.esRebuilding === true |
100 |
| - // response.inSync = response.dbCount === response.indexCount |
101 |
| - // |
102 |
| - // res.json({ success: true, status: response }) |
103 |
| - // } |
104 |
| - // ) |
105 | 73 | }
|
106 | 74 |
|
107 |
| -apiElasticSearch.search = function (req, res) { |
| 75 | +apiElasticSearch.search = async (req, res) => { |
108 | 76 | var limit = !_.isUndefined(req.query['limit']) ? req.query.limit : 100
|
109 | 77 | try {
|
110 | 78 | limit = parseInt(limit)
|
111 | 79 | } catch (e) {
|
112 | 80 | limit = 100
|
113 | 81 | }
|
114 | 82 |
|
115 |
| - async.waterfall( |
116 |
| - [ |
117 |
| - function (next) { |
118 |
| - if (!req.user.role.isAdmin && !req.user.role.isAgent) |
119 |
| - return groupSchema.getAllGroupsOfUserNoPopulate(req.user._id, next) |
120 |
| - |
121 |
| - var Department = require('../../../models/department') |
122 |
| - return Department.getDepartmentGroupsOfUser(req.user._id, next) |
123 |
| - }, |
124 |
| - function (groups, next) { |
125 |
| - var g = _.map(groups, function (i) { |
126 |
| - return i._id |
127 |
| - }) |
128 |
| - // For docker we need to add a unique ID for the index. |
129 |
| - var obj = { |
130 |
| - index: es.indexName, |
131 |
| - body: { |
132 |
| - size: limit, |
133 |
| - from: 0, |
134 |
| - query: { |
135 |
| - bool: { |
136 |
| - must: { |
137 |
| - multi_match: { |
138 |
| - query: req.query['q'], |
139 |
| - type: 'cross_fields', |
140 |
| - operator: 'and', |
141 |
| - fields: [ |
142 |
| - 'uid^5', |
143 |
| - 'subject^4', |
144 |
| - 'issue^4', |
145 |
| - 'owner.fullname', |
146 |
| - 'owner.username', |
147 |
| - 'owner.email', |
148 |
| - 'comments.owner.email', |
149 |
| - 'tags.normalized', |
150 |
| - 'priority.name', |
151 |
| - 'type.name', |
152 |
| - 'group.name', |
153 |
| - 'comments.comment^3', |
154 |
| - 'notes.note^3', |
155 |
| - 'dateFormatted' |
156 |
| - ], |
157 |
| - tie_breaker: 0.3 |
158 |
| - } |
159 |
| - }, |
160 |
| - filter: { |
161 |
| - terms: { 'group._id': g } |
162 |
| - } |
| 83 | + try { |
| 84 | + let groups = [] |
| 85 | + if (!req.user.role.isAdmin && !req.user.role.isAgent) { |
| 86 | + groups = await GroupModel.getAllGroupsOfUser(req.user._id) |
| 87 | + } else { |
| 88 | + groups = await DepartmentModel.getDepartmentGroupsOfUser(req.user._id) |
| 89 | + } |
| 90 | + |
| 91 | + const g = _.map(groups, i => i._id) |
| 92 | + |
| 93 | + const obj = { |
| 94 | + index: es.indexName, |
| 95 | + body: { |
| 96 | + size: limit, |
| 97 | + from: 0, |
| 98 | + query: { |
| 99 | + bool: { |
| 100 | + must: { |
| 101 | + multi_match: { |
| 102 | + query: req.query['q'], |
| 103 | + type: 'cross_fields', |
| 104 | + operator: 'and', |
| 105 | + fields: [ |
| 106 | + 'uid^5', |
| 107 | + 'subject^4', |
| 108 | + 'issue^4', |
| 109 | + 'owner.fullname', |
| 110 | + 'owner.username', |
| 111 | + 'owner.email', |
| 112 | + 'comments.owner.email', |
| 113 | + 'tags.normalized', |
| 114 | + 'priority.name', |
| 115 | + 'type.name', |
| 116 | + 'group.name', |
| 117 | + 'comments.comment^3', |
| 118 | + 'notes.note^3', |
| 119 | + 'dateFormatted' |
| 120 | + ], |
| 121 | + tie_breaker: 0.3 |
163 | 122 | }
|
| 123 | + }, |
| 124 | + filter: { |
| 125 | + terms: { 'group._id': g } |
164 | 126 | }
|
165 | 127 | }
|
166 | 128 | }
|
167 |
| - |
168 |
| - return next(null, obj) |
169 | 129 | }
|
170 |
| - ], |
171 |
| - function (err, obj) { |
172 |
| - if (err) return apiUtil.sendApiError(res, 500, err.message) |
173 |
| - if (!es || !es.esclient) return apiUtil.sendApiError(res, 400, 'Elasticsearch is not configured') |
174 |
| - |
175 |
| - es.esclient.search(obj).then(function (r) { |
176 |
| - return res.send(r) |
177 |
| - }) |
178 | 130 | }
|
179 |
| - ) |
| 131 | + |
| 132 | + if (!es || !es.esclient) return apiUtils.sendApiError(res, 400, 'Elasticsearch is not configured') |
| 133 | + |
| 134 | + es.esclient.search(obj).then(r => { |
| 135 | + return res.send(r) |
| 136 | + }) |
| 137 | + } catch (e) { |
| 138 | + logger.debug(e) |
| 139 | + return apiUtils.sendApiError(res, 500, { error: e }) |
| 140 | + } |
180 | 141 | }
|
181 | 142 |
|
182 | 143 | module.exports = apiElasticSearch
|
0 commit comments