Skip to content

Commit

Permalink
refactor getAllModules to use fancy mongo aggregations, linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbclifford committed Nov 13, 2017
1 parent 184e335 commit ff605a4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 45 deletions.
104 changes: 66 additions & 38 deletions src/libs/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,7 @@ function getModules(db, user, callback) {
}

if (modules) {
for (const mod of modules) {
const defaultOptions = getDefaultOptions(mod.type) || {};
const defaultKeys = Object.keys(defaultOptions);

if (_.isEmpty(defaultOptions)) {
delete mod.options;
continue;
}

mod.options = Object.assign({}, defaultOptions, mod.options);

for (const optionKey of Object.keys(mod.options)) {
if (!defaultKeys.includes(optionKey)) {
delete mod.options[optionKey];
}
}

if (_.isEmpty(mod.options)) {
delete mod.options;
}
}
modules = modules.map(processModuleOptions);
}

// Return default modules if none found, else return found documents
Expand Down Expand Up @@ -201,30 +181,54 @@ function getAllModules(db, callback) {
}

const userdata = db.collection('users');
userdata.find({ confirmed: true }).toArray((err, users) => {
// Begin aggregation pipeline
userdata.aggregate([
// Stage 1
{
$match: {
confirmed: true
}
},
// Stage 2
{
$lookup: {
from: 'modules',
localField: '_id',
foreignField: 'user',
as: 'modules'
}
},
// Stage 3
{
$project: {
// only include these in the final results
user: 1,
modules: {
type: 1,
row: 1,
column: 1,
width: 1,
height: 1,
options: 1
}
}
}
]).toArray((err, docs) => {
if(err) {
callback(new Error('There was a problem querying the database!'), null);
callback(new Error('There was a problem connecting to the database!'), null);
return;
}

function handleModule(i, result) {
if(i < users.length) {
const user = users[i].user;
getModules(db, user, (err, modules) => {
if(err) {
callback(err, null);
return;
}

result[user] = modules;
handleModule(++i, result);
});
const userModules = {};
for(const doc of docs) {
if(doc.modules.length < 1) {
userModules[doc.user] = defaultModules;
} else {
callback(null, result);
userModules[doc.user] = doc.modules.map(processModuleOptions);
}
}

handleModule(0, {});
callback(null, userModules);
});
}

Expand Down Expand Up @@ -394,6 +398,30 @@ function upsertModules(db, user, modules, callback) {
});
}

module.exports.get = getModules;
function processModuleOptions(mod) {
const defaultOptions = getDefaultOptions(mod.type) || {};
const defaultKeys = Object.keys(defaultOptions);

if (_.isEmpty(defaultOptions)) {
delete mod.options;
return mod;
}

mod.options = Object.assign({}, defaultOptions, mod.options);

for (const optionKey of Object.keys(mod.options)) {
if (!defaultKeys.includes(optionKey)) {
delete mod.options[optionKey];
}
}

if (_.isEmpty(mod.options)) {
delete mod.options;
}

return mod;
}

module.exports.get = getModules;
module.exports.getAll = getAllModules;
module.exports.upsert = upsertModules;
10 changes: 5 additions & 5 deletions src/libs/stickynotes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function getNotes(db, moduleId, callback) {
if(typeof callback !== 'function') return;

if(typeof db !== 'object') {
callback(new Error('Invalid database connection!'), null);
return;
Expand All @@ -21,14 +21,14 @@ function getNotes(db, moduleId, callback) {

// create a new stickynote if nothing exits under the module Id
if (notes.length === 0) {
let noteDoc = { text: 'New Stickynote', moduleId };
stickynotes.insertOne(noteDoc, (err, note) => {
const noteDoc = { text: 'New Stickynote', moduleId };
stickynotes.insertOne(noteDoc, err => {
if (err) {
callback(new Error(err), null);
return;
}
callback(null, noteDoc);
})
});
} else {
callback(null, notes[0]);
}
Expand All @@ -37,7 +37,7 @@ function getNotes(db, moduleId, callback) {

function postNote(db, text, moduleId, callback) {
if(typeof callback !== 'function') return;

if(typeof db !== 'object') {
callback(new Error('Invalid database connection!'));
return;
Expand Down
5 changes: 3 additions & 2 deletions src/test/scheduleTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ const tests = [
}
];

for(const test of tests) {
for(let i = 0; i < tests.length; i++) {
const test = tests[i];
const results = schedule.ordine(test.base, test.add);

// Convert moment.js objects to readable strings
Expand All @@ -84,5 +85,5 @@ for(const test of tests) {
}
}

console.log('Test ' + i + ' = ', results);
console.log(`Test ${i + 1} = ${results}`);
}

0 comments on commit ff605a4

Please sign in to comment.