From 15c3a96291ded237262d0b8f82f7ff5122b2ccac Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 7 May 2018 17:16:52 -0600 Subject: [PATCH] fixed settings --- graph/loaders/settings.js | 16 +++++----------- services/settings.js | 15 +++++---------- test/server/services/settings.js | 19 ++++++++++++++++--- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/graph/loaders/settings.js b/graph/loaders/settings.js index a1329b619..8b07d712b 100644 --- a/graph/loaders/settings.js +++ b/graph/loaders/settings.js @@ -1,4 +1,3 @@ -const Setting = require('../../models/setting'); const Settings = require('../../services/settings'); const DataLoader = require('dataloader'); const { zipObject } = require('lodash'); @@ -15,12 +14,9 @@ class SettingsLoader { async _batchLoadFn(fields) { // Load a settings object with all the requested fields, unless we have the // entire object cached, in which case we'll return the whole cache. - const model = this._cache + const obj = this._cache ? await this._cache - : await Settings.retrieve(...fields); - - // Convert the model into an object for easier manipulation. - const obj = model.toObject(); + : await Settings.select(...fields); // Return the specific fields for each of the fields that were loaded. return fields.map(field => obj[field]); @@ -55,11 +51,9 @@ class SettingsLoader { // Load all the values for the specific fields. const values = await this._loader.loadMany(fields); - // Zip up the fields and values to create an object to return. - const obj = zipObject(fields, values); - - // Return the assembled Settings object. - return new Setting(obj); + // Zip up the fields and values to create an object to return and return the + // assembled Settings object. + return zipObject(fields, values); } } diff --git a/services/settings.js b/services/settings.js index 34b6fead4..def42187a 100644 --- a/services/settings.js +++ b/services/settings.js @@ -12,16 +12,13 @@ async function loadFn(fields = []) { throw new ErrSettingsNotInit(); } - return model; + return model.toObject(); } // batchLoadFn will load a settings object with all the requested fields. async function batchLoadFn(fields) { // Load a settings object with all the requested fields. - const model = await loadFn(fields); - - // Convert the model into an object for easier manipulation. - const obj = model.toObject(); + const obj = await loadFn(fields); // Return the specific fields for each of the fields that were loaded. return fields.map(field => obj[field]); @@ -47,11 +44,9 @@ class Settings { // Load all the values for the specific fields. const values = await batchedSettingsLoader.loadMany(fields); - // Zip up the fields and values to create an object to return. - const obj = zipObject(fields, values); - - // Return the assembled Settings object. - return new Setting(obj); + // Zip up the fields and values to create an object to return and return the + // assembled Settings object. + return zipObject(fields, values); } static async update(settings) { diff --git a/test/server/services/settings.js b/test/server/services/settings.js index 44068e6cd..ca6fdef8d 100644 --- a/test/server/services/settings.js +++ b/test/server/services/settings.js @@ -2,8 +2,13 @@ const Settings = require('../../../services/settings'); const chai = require('chai'); const expect = chai.expect; -describe('services.SettingsService', () => { - beforeEach(() => Settings.init({ moderation: 'PRE', wordlist: ['donut'] })); +describe('services.Settings', () => { + beforeEach(() => + Settings.init({ + moderation: 'PRE', + wordlist: { banned: ['bannedWord'], suspect: [] }, + }) + ); describe('#retrieve()', () => { it('should have a moderation field defined', () => { @@ -35,6 +40,14 @@ describe('services.SettingsService', () => { expect(settings).to.not.have.property('wordlist'); }); }); + it('should have a wordlist field defined and not moderation', () => { + return Settings.select('wordlist').then(settings => { + expect(settings).to.not.have.property('moderation'); + expect(settings).to.have.property('wordlist'); + expect(settings.wordlist).to.have.property('banned'); + expect(settings.wordlist.banned).to.contain('bannedWord'); + }); + }); }); describe('#update()', () => { @@ -65,7 +78,7 @@ describe('services.SettingsService', () => { const settings = await Settings.retrieve(); settings.charCount = 500; - await Settings.update(settings.toObject()); + await Settings.update(settings); }); });