Change default settings=null to settings={}

This commit is contained in:
Chi Vinh Le
2017-11-20 20:19:58 +01:00
parent e675d716cb
commit 511e71f63a
3 changed files with 35 additions and 13 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ const AssetSchema = new Schema({
// always after running `rectifySettings` against it.
settings: {
type: Schema.Types.Mixed,
default: null
default: {},
},
// Tags are added by the self or by administrators.
+33 -11
View File
@@ -4,6 +4,7 @@ const SettingsService = require('./settings');
const domainlist = require('./domainlist');
const errors = require('../errors');
const merge = require('lodash/merge');
const isEmpty = require('lodash/isEmpty');
const {dotize} = require('./utils');
module.exports = class AssetsService {
@@ -40,7 +41,7 @@ module.exports = class AssetsService {
]);
// If the asset exists and has settings then return the merged object.
if (asset && asset.settings) {
if (asset && asset.settings && !isEmpty(asset.settings)) {
settings = merge({}, globalSettings, asset.settings);
} else {
settings = globalSettings;
@@ -95,17 +96,38 @@ module.exports = class AssetsService {
/**
* Updates the settings for the asset.
* @param {[type]} id [description]
* @param {[type]} settings [description]
* @return {[type]} [description]
* @param {String} id id of asset
* @param {Object} settings new settings values
* @return {Promise}
*/
static overrideSettings(id, settings) {
console.log(settings, dotize({settings}));
return AssetModel.findOneAndUpdate({id}, {
$set: dotize({settings})
}, {
new: true
});
static async overrideSettings(id, settings) {
try {
const result = await AssetModel.findOneAndUpdate({id}, {
// The effect of dotize is that only the provided setting values are overwritten
// and does not replace the whole object.
$set: dotize({settings})
}, {
new: true
});
return result;
} catch (e) {
// Legacy data models contains `settings=null` as a default which cannot be traversed.
// New data models uses `settings={}`.
if (e.code === 16837) {
// Overwrite it fully in this case.
const result = await AssetModel.findOneAndUpdate({id}, {
$set: {settings}
}, {
new: true
});
return result;
} else {
throw e;
}
}
}
/**
+1 -1
View File
@@ -91,7 +91,7 @@ describe('services.AssetsService', () => {
.findOrCreateByUrl('https://override.test.com/asset')
.then((asset) => {
expect(asset).to.have.property('settings');
expect(asset.settings).to.be.null;
expect(asset.settings).to.be.empty;
return AssetsService.overrideSettings(asset.id, {moderation: 'PRE'});
})