Skip to content

Commit

Permalink
WIP: Move configurations code from core-pg to ept-configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
akinsey committed Jun 7, 2019
1 parent e84b185 commit c5e4628
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 3 deletions.
Empty file.
42 changes: 42 additions & 0 deletions modules/ept-configurations/db/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var path = require('path');
var dbc = require(path.normalize(__dirname + '/db'));
var db = dbc.db;
var helper = dbc.helper;

module.exports = function(config) {
if (config.portal && config.portal.board_id) {
config.portal.board_id = helper.deslugify(config.portal.board_id);
}

// Copy fields from config
var storedConfig = (({
gaKey,
images,
portal,
emailer,
website,
postMaxLength,
inviteOnly,
logEnabled,
rateLimiting,
loginRequired,
verifyRegistration
}) => ({
gaKey,
images,
portal,
emailer,
website,
postMaxLength,
inviteOnly,
logEnabled,
rateLimiting,
loginRequired,
verifyRegistration
}))(config);

// For now we are hardcoding 'default' as the main config
// In the future we can support swappable configs
var q = 'INSERT INTO configurations (name, config) VALUES (\'default\', $1)';
return db.sqlQuery(q, [storedConfig]);
};
2 changes: 2 additions & 0 deletions modules/ept-configurations/db/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var dbc = require('epochtalk-core-pg')();
module.exports = dbc;
28 changes: 28 additions & 0 deletions modules/ept-configurations/db/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var path = require('path');
var _ = require('lodash');
var changeCase = require('change-case');
var renameKeys = require('deep-rename-keys');
var dbc = require(path.normalize(__dirname + '/db'));
var db = dbc.db;
var errors = dbc.errors;
var NotFoundError = errors.NotFoundError;

// returns object of private configurations
module.exports = function() {
var q = 'SELECT config FROM configurations WHERE name = \'default\'';
return db.sqlQuery(q)
.then(function(queryResults) {
if (queryResults.length > 0) {
var privateConfigurations = queryResults[0].config;

if (_.isObject(privateConfigurations)) {
privateConfigurations = renameKeys(privateConfigurations, function(key) {
return changeCase.camel(key);
});
}

return privateConfigurations;
}
else { throw new NotFoundError('Configurations Not Found'); }
});
};
12 changes: 12 additions & 0 deletions modules/ept-configurations/db/getPublic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var path = require('path');
var dbc = require(path.normalize(__dirname + '/db'));
var db = dbc.db;

// returns object of public configurations
module.exports = function() {
var q = 'SELECT config->>\'website\' as website FROM configurations WHERE name = \'default\'';
return db.scalar(q)
.then(function(queryResults) {
return queryResults.website;
});
};
8 changes: 8 additions & 0 deletions modules/ept-configurations/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var path = require('path');

module.exports = {
create: require(path.normalize(__dirname + '/create')),
get: require(path.normalize(__dirname + '/get')),
getPublic: require(path.normalize(__dirname + '/getPublic')),
update: require(path.normalize(__dirname + '/update'))
};
9 changes: 9 additions & 0 deletions modules/ept-configurations/db/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var path = require('path');
var dbc = require(path.normalize(__dirname + '/db'));
var db = dbc.db;

// updates configurations from an object
module.exports = function(config) {
var query = 'UPDATE configurations SET config = $1 WHERE name = \'default\'';
return db.sqlQuery(query, [config]);
};
13 changes: 13 additions & 0 deletions modules/ept-configurations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var path = require('path');
var db = require(path.normalize(__dirname + '/db'));
// var routes = require(path.normalize(__dirname + '/routes'));
// var authorization = require(path.normalize(__dirname + '/authorization'));
// var permissions = require(path.normalize(__dirname + '/permissions'));

module.exports = {
name: 'configurations',
// authorization: authorization,
// permissions: permissions,
// routes: routes,
db: db
};
Empty file.
Empty file.
1 change: 1 addition & 0 deletions modules/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = [
'ept-ignore-users',
'ept-moderators',
'ept-images',
'ept-configurations',
'ept-user-notes',
'ept-rank',
'ept-roles',
Expand Down
6 changes: 3 additions & 3 deletions setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ var _ = require('lodash');
var Promise = require('bluebird');
var path = require('path');
var config = require(path.normalize(__dirname + '/config'));
var db = require(path.normalize(__dirname + '/db'));
var dbConfigurations = require(path.normalize(__dirname + '/modules/ept-configurations/db'));
var defaultConfigurations = require(path.join(__dirname, 'configurations.json'));

// load admin options from database
module.exports = function() {
return db.configurations.get()
return dbConfigurations.get()
.then(parseConfigs)
// if admin options are not yet configured
.error(function() {
Expand All @@ -19,7 +19,7 @@ module.exports = function() {
// emailer is no longer configured by default
var configClone = _.cloneDeep(config);
configClone.emailer = {};
return db.configurations.create(configClone);
return dbConfigurations.create(configClone);
})
.then(function() { return config; });
});
Expand Down

0 comments on commit c5e4628

Please sign in to comment.