-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathadmin.js
More file actions
61 lines (53 loc) · 1.83 KB
/
Copy pathadmin.js
File metadata and controls
61 lines (53 loc) · 1.83 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/
/**
* api/v1/controllers/admin.js
*/
'use strict'; // eslint-disable-line strict
const featureToggles = require('feature-toggles');
const sampleStore = require('../../../cache/sampleStore');
const sampleStoreInit = require('../../../cache/sampleStoreInit');
const apiErrors = require('../apiErrors');
const httpStatus = require('../constants').httpStatus;
const u = require('../helpers/verbs/utils');
module.exports = {
/**
* POST /admin/sampleStore/rebuild
*
* Rebuild the redis sampleStore from the samples in the database. Admin
* only.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
rebuildSampleStore(req, res, next) {
console.log('\n\n rebuildSampleStore ==>>>');
const resultObj = { reqStartTime: req.timestamp };
if (!req.headers.IsAdmin) {
return u.forbidden(next);
}
const enabled = featureToggles
.isFeatureEnabled(sampleStore.constants.featureName);
if (!enabled) {
const err = new apiErrors.InvalidSampleStoreState({
explanation: 'You cannot rebuild the sample store if the ' +
'ENABLE_REDIS_SAMPLE_STORE feature is not enabled.',
});
return next(err);
}
return sampleStoreInit.eradicate()
.then(() => sampleStoreInit.populate())
.then(() => {
resultObj.dbTime = new Date() - resultObj.reqStartTime;
u.logAPI(req, resultObj, {});
res.status(httpStatus.NO_CONTENT).json();
})
.catch(() => u.forbidden(next));
},
}; // exports