/
searchIndex.js
44 lines (35 loc) · 1.02 KB
/
searchIndex.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
const acl = require('./../model/acl')
const config = require('./../../../config')
const help = require('./../help')
const SearchIndex = function (server) {
this.server = server
server.app.routeMethods('/api/index', {
post: this.post.bind(this)
})
}
SearchIndex.prototype.post = function (req, res, next) {
if (!req.dadiApiClient.clientId) {
return help.sendBackJSON(null, res, next)(
acl.createError(req.dadiApiClient)
)
}
// 404 if Search is not enabled
if (config.get('search.enabled') !== true) {
return next()
}
res.statusCode = 204
res.end(JSON.stringify({'message': 'Indexing started'}))
try {
Object.keys(this.server.components).forEach(key => {
let value = this.server.components[key]
let hasModel = Object.keys(value).includes('model') &&
value.model.constructor.name === 'Model'
if (hasModel) {
value.model.searchHandler.batchIndex()
}
})
} catch (err) {
console.log(err)
}
}
module.exports = server => new SearchIndex(server)