Skip to content

Commit

Permalink
fix to avoid multiplies gets to the db
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelvilche committed Jun 9, 2020
1 parent 134b27a commit 3de1ef5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
23 changes: 10 additions & 13 deletions lib/client-settings.js
Expand Up @@ -58,23 +58,20 @@ module.exports = class ClientSettings {
static async getForClient(entity, settingName) {

const clientSettingsModel = this.session.getSessionInstance(ClientSettingsModel);
const setting = await clientSettingsModel.getBy('entity', entity, { unique: true });
const clientSettings = await clientSettingsModel.getBy('entity', entity, { unique: true });

const value = setting && setting.values[settingName] ? setting.values[settingName] : this.getDefault(entity, settingName);
const defaultSettings = DefinitionFetcher.getByEntity(this.settingsPath, entity);

if(!cache[this.settingsPath][entity])
cache[this.settingsPath][entity] = {};
const settingsFormatted = {};

cache[this.settingsPath][entity][settingName] = value;
Object.entries(defaultSettings).forEach(([defaultSettingName, setting]) => {
settingsFormatted[defaultSettingName] = (clientSettings && clientSettings.values
&& typeof clientSettings.values[defaultSettingName] !== 'undefined')
? clientSettings.values[defaultSettingName]
: setting.default;
});

return value;
}

static getDefault(entity, settingName) {

cache[this.settingsPath][entity] = {
[settingName]: DefinitionFetcher.getDefaultValue(this.settingsPath, entity, settingName)
};
cache[this.settingsPath][entity] = settingsFormatted;

return cache[this.settingsPath][entity][settingName];
}
Expand Down
7 changes: 4 additions & 3 deletions lib/definition-fetcher.js
Expand Up @@ -43,10 +43,11 @@ module.exports = class DefinitionFetcher {
return definitions[pathToGet] && definitions[pathToGet][entity] && definitions[pathToGet][entity][settingName];
}

static getDefaultValue(definitionPath, entity, settingName) {
const setting = this.get(definitionPath, entity, settingName);
static getByEntity(definitionPath, entity) {

return setting && setting.default;
const pathToGet = this.getPath(definitionPath);

return definitions[pathToGet] && definitions[pathToGet][entity];
}

};
8 changes: 3 additions & 5 deletions tests/client-settings.js
Expand Up @@ -241,11 +241,10 @@ describe('ClientSettings', () => {
mockRequire(uniqueDefinitionPath, settingsDefinition);

sinon.stub(ClientSettingsModel.prototype, 'getBy');
ClientSettingsModel.prototype.getBy.onCall(0).resolves({
ClientSettingsModel.prototype.getBy.resolves({
entity,
values: { [settingName]: value }
});
ClientSettingsModel.prototype.getBy.onCall(1).resolves(null);

ClientSettings
.setSettingsDefinitionPath(uniqueDefinitionPath)
Expand All @@ -267,9 +266,8 @@ describe('ClientSettings', () => {

assert.deepStrictEqual(otherSettingValue, 0);

sinon.assert.calledTwice(ClientSettingsModel.prototype.getBy);
sinon.assert.calledWithExactly(ClientSettingsModel.prototype.getBy.getCall(0), 'entity', entity, { unique: true });
sinon.assert.calledWithExactly(ClientSettingsModel.prototype.getBy.getCall(1), 'entity', entity, { unique: true });
sinon.assert.calledOnce(ClientSettingsModel.prototype.getBy);
sinon.assert.calledWithExactly(ClientSettingsModel.prototype.getBy, 'entity', entity, { unique: true });
});

});

0 comments on commit 3de1ef5

Please sign in to comment.