Const, service, and model updates

- Updated enum values to be uppercase
- Updated services to expose service models
- Updated models to only export the mongoose model
- Moved all mongoose static methods over to new services
- Updated tests to refelct new setup

BREAKING

- Status that were uppercased (caps) have caused issues with the
  admin pages
This commit is contained in:
Wyatt Johnson
2017-01-24 12:10:32 -07:00
parent 0994023864
commit a7e9c0c776
54 changed files with 1831 additions and 2499 deletions
+1 -71
View File
@@ -1,6 +1,5 @@
const mongoose = require('../services/mongoose');
const Schema = mongoose.Schema;
const _ = require('lodash');
const WordlistSchema = new Schema({
banned: [String],
@@ -64,12 +63,6 @@ const SettingSchema = new Schema({
}
});
/**
* toJSON provides settings overrides to this object's serialization methods.
*/
SettingSchema.options.toJSON = {};
SettingSchema.options.toJSON.virtuals = true;
/**
* Merges two settings objects.
*/
@@ -88,72 +81,9 @@ SettingSchema.method('merge', function(src) {
});
});
/**
* Filters the object for the given user only allowing those with the allowed
* roles/permissions to access particular parameters.
*/
SettingSchema.method('filterForUser', function(user = false) {
if (!user || !user.roles.includes('admin')) {
return _.pick(this.toJSON(), [
'moderation',
'infoBoxEnable',
'infoBoxContent',
'closeTimeout',
'closedMessage',
'charCountEnable',
'charCount',
'requireEmailConfirmation'
]);
}
return this.toJSON();
});
/**
* The Mongo Mongoose object.
*/
const Setting = mongoose.model('Setting', SettingSchema);
/**
* The Setting Service object exposing the Setting model.
* @type {Object}
*/
const SettingService = module.exports = {};
/**
* The selector used to uniquely identify the settings document.
*/
const selector = {id: '1'};
/**
* Gets the entire settings record and sends it back
* @return {Promise} settings the whole settings record
*/
SettingService.retrieve = () => Setting.findOne(selector);
/**
* This will update the settings object with whatever you pass in
* @param {object} setting a hash of whatever settings you want to update
* @return {Promise} settings Promise that resolves to the entire (updated) settings object.
*/
SettingService.update = (settings) => Setting.findOneAndUpdate(selector, {
$set: settings
}, {
upsert: true,
new: true,
setDefaultsOnInsert: true
});
/**
* This is run once when the app starts to ensure settings are populated.
* @return {Promise} null initialize the global settings object
*/
SettingService.init = (defaults) => {
// Inject the defaults on top of the passed in defaults to ensure that the new
// settings conform to the required selector.
defaults = Object.assign({}, defaults, selector);
// Actually update the settings collection.
return SettingService.update(defaults);
};
module.exports = Setting;