Skip to content

Commit c117f37

Browse files
committed
fix(tickets): search error since TS conversion
1 parent af6de57 commit c117f37

File tree

1 file changed

+63
-102
lines changed

1 file changed

+63
-102
lines changed

src/controllers/api/v2/elasticsearch.js

+63-102
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@
1212
* Copyright (c) 2014-2019. All rights reserved.
1313
*/
1414

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'
2121

2222
const apiElasticSearch = {}
23-
const apiUtil = require('../apiUtils')
2423

2524
apiElasticSearch.rebuild = (req, res) => {
2625
es.rebuildIndex()
2726

28-
return apiUtil.sendApiSuccess(res)
27+
return apiUtils.sendApiSuccess(res)
2928
}
3029

3130
apiElasticSearch.status = async (req, res) => {
@@ -65,118 +64,80 @@ apiElasticSearch.status = async (req, res) => {
6564
response.isRebuilding = global.esRebuilding === true
6665
response.inSync = response.dbCount === response.indexCount
6766

68-
return apiUtil.sendApiSuccess(res, { status: response })
67+
return apiUtils.sendApiSuccess(res, { status: response })
6968
} catch (e) {
70-
if (process.env.NODE_ENV === 'development') winston.warn(e.message)
69+
if (process.env.NODE_ENV === 'development') logger.warn(e.message)
7170

72-
return apiUtil.sendApiError(res, 500, e.message)
71+
return apiUtils.sendApiError(res, 500, e.message)
7372
}
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-
// )
10573
}
10674

107-
apiElasticSearch.search = function (req, res) {
75+
apiElasticSearch.search = async (req, res) => {
10876
var limit = !_.isUndefined(req.query['limit']) ? req.query.limit : 100
10977
try {
11078
limit = parseInt(limit)
11179
} catch (e) {
11280
limit = 100
11381
}
11482

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
163122
}
123+
},
124+
filter: {
125+
terms: { 'group._id': g }
164126
}
165127
}
166128
}
167-
168-
return next(null, obj)
169129
}
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-
})
178130
}
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+
}
180141
}
181142

182143
module.exports = apiElasticSearch

0 commit comments

Comments
 (0)