Skip to content

Commit

Permalink
style: run eslint --fix with updated AirBnB rules
Browse files Browse the repository at this point in the history
  • Loading branch information
swashcap committed Oct 21, 2018
1 parent a3f4710 commit 5cf3eef
Show file tree
Hide file tree
Showing 139 changed files with 3,118 additions and 3,278 deletions.
169 changes: 78 additions & 91 deletions packages/coinstac-api-server/src/auth-helpers.js
Expand Up @@ -2,9 +2,9 @@ const crypto = require('crypto');
const Boom = require('boom');
const jwt = require('jsonwebtoken');
const rethink = require('rethinkdb');
const Promise = require('bluebird');
const config = require('../config/default');
const dbmap = require('/etc/coinstac/cstacDBMap'); // eslint-disable-line import/no-absolute-path, import/no-unresolved
const Promise = require('bluebird');

const helperFunctions = {
/**
Expand All @@ -23,35 +23,35 @@ const helperFunctions = {
*/
createUser(user, passwordHash) {
return helperFunctions.getRethinkConnection()
.then((connection) => {
const userDetails = {
id: user.username,
email: user.email,
institution: user.institution,
passwordHash,
permissions: {
computations: {},
consortia: {},
pipelines: {},
},
consortiaStatuses: {
.then((connection) => {
const userDetails = {
id: user.username,
email: user.email,
institution: user.institution,
passwordHash,
permissions: {
computations: {},
consortia: {},
pipelines: {},
},
consortiaStatuses: {

},
};
},
};

if (user.permissions) {
userDetails.permissions = user.permissions;
}
if (user.permissions) {
userDetails.permissions = user.permissions;
}

if (user.consortiaStatuses) {
userDetails.consortiaStatuses = user.consortiaStatuses;
}
if (user.consortiaStatuses) {
userDetails.consortiaStatuses = user.consortiaStatuses;
}

return rethink.table('users')
.insert(userDetails, { returnChanges: true })
.run(connection);
})
.then(result => result.changes[0].new_val);
return rethink.table('users')
.insert(userDetails, { returnChanges: true })
.run(connection);
})
.then(result => result.changes[0].new_val);
},
/**
* Returns RethinkDB connection
Expand All @@ -78,36 +78,25 @@ const helperFunctions = {
*/
getUserDetails(credentials) {
return helperFunctions.getRethinkConnection()
.then(connection => Promise.all([connection, rethink.table('users').get(credentials.username).run(connection)]))
.then(([connection, user]) => {
if (user) {
return rethink.table('users').get(credentials.username).merge(user =>
({
.then(connection => Promise.all([connection, rethink.table('users').get(credentials.username).run(connection)]))
.then(([connection, user]) => {
if (user) {
return rethink.table('users').get(credentials.username).merge(user => ({
permissions: user('permissions').coerceTo('array')
.map(table =>
table.map(tableArr =>
rethink.branch(
tableArr.typeOf().eq('OBJECT'),
tableArr.coerceTo('array').map(doc =>
doc.map(docArr =>
rethink.branch(
docArr.typeOf().eq('ARRAY'),
docArr.fold({}, (acc, row) =>
acc.merge(rethink.table('roles').get(row)('verbs'))
),
docArr
)
)
).coerceTo('object'),
tableArr
)
)
).coerceTo('object'),
.map(table => table.map(tableArr => rethink.branch(
tableArr.typeOf().eq('OBJECT'),
tableArr.coerceTo('array').map(doc => doc.map(docArr => rethink.branch(
docArr.typeOf().eq('ARRAY'),
docArr.fold({}, (acc, row) => acc.merge(rethink.table('roles').get(row)('verbs'))),
docArr
))).coerceTo('object'),
tableArr
))).coerceTo('object'),
})).run(connection);
}
}

return null;
});
return null;
});
},
/**
* Hashes password for storage in database
Expand Down Expand Up @@ -148,13 +137,13 @@ const helperFunctions = {
*/
validateToken(decoded, request, callback) {
helperFunctions.getUserDetails({ username: decoded.username })
.then((user) => {
if (user.id) {
callback(null, true, user);
} else {
callback(null, false, null);
}
});
.then((user) => {
if (user.id) {
callback(null, true, user);
} else {
callback(null, false, null);
}
});
},
/**
* Confirms that submitted email is new
Expand All @@ -177,24 +166,22 @@ const helperFunctions = {
*/
validateUniqueUser(req, res) {
return helperFunctions.getRethinkConnection()
.then(connection =>
helperFunctions.validateUniqueUsername(req, connection)
.then((isUniqueUsername) => {
if (isUniqueUsername) {
return helperFunctions.validateUniqueEmail(req, connection);
}
.then(connection => helperFunctions.validateUniqueUsername(req, connection)
.then((isUniqueUsername) => {
if (isUniqueUsername) {
return helperFunctions.validateUniqueEmail(req, connection);
}

res(Boom.badRequest('Username taken'));
return null;
})
.then((isUniqueEmail) => {
if (isUniqueEmail) {
res(req.payload);
} else if (isUniqueEmail === false) {
res(Boom.badRequest('Email taken'));
}
})
);
res(Boom.badRequest('Username taken'));
return null;
})
.then((isUniqueEmail) => {
if (isUniqueEmail) {
res(req.payload);
} else if (isUniqueEmail === false) {
res(Boom.badRequest('Email taken'));
}
}));
},
/**
* Confirms that submitted username is new
Expand All @@ -217,20 +204,20 @@ const helperFunctions = {
*/
validateUser(req, res) {
return helperFunctions.getUserDetails(req.payload)
.then((user) => {
if (user) {
helperFunctions.verifyPassword(req.payload.password, user.passwordHash)
.then((passwordMatch) => {
if (user && user.passwordHash && passwordMatch) {
res(user);
} else {
res(Boom.unauthorized('Incorrect username or password.'));
}
});
} else {
res(Boom.unauthorized('Incorrect username or password.'));
}
});
.then((user) => {
if (user) {
helperFunctions.verifyPassword(req.payload.password, user.passwordHash)
.then((passwordMatch) => {
if (user && user.passwordHash && passwordMatch) {
res(user);
} else {
res(Boom.unauthorized('Incorrect username or password.'));
}
});
} else {
res(Boom.unauthorized('Incorrect username or password.'));
}
});
},
/**
* Verify that authenticating user is using correct password
Expand Down
36 changes: 12 additions & 24 deletions packages/coinstac-api-server/src/data/resolvers.js
Expand Up @@ -3,10 +3,10 @@ const Boom = require('boom');
const GraphQLJSON = require('graphql-type-json');
const Promise = require('bluebird');
const { PubSub, withFilter } = require('graphql-subscriptions');
const axios = require('axios');
const helperFunctions = require('../auth-helpers');
const initSubscriptions = require('./subscriptions');
const config = require('../../config/default');
const axios = require('axios');

/**
* Helper function to retrieve all members of given table
Expand All @@ -15,9 +15,7 @@ const axios = require('axios');
*/
function fetchAll(table) {
return helperFunctions.getRethinkConnection()
.then(connection =>
rethink.table(table).orderBy({ index: 'id' }).run(connection)
)
.then(connection => rethink.table(table).orderBy({ index: 'id' }).run(connection))
.then(cursor => cursor.toArray());
}

Expand All @@ -29,30 +27,20 @@ function fetchAll(table) {
*/
function fetchOne(table, id) {
return helperFunctions.getRethinkConnection()
.then(connection =>
rethink.table(table).get(id).run(connection)
);
.then(connection => rethink.table(table).get(id).run(connection));
}

function fetchOnePipeline(table, id) {
return helperFunctions.getRethinkConnection()
.then(connection =>
rethink.table('pipelines')
.get(id)
// Populate computations subfield with computation meta information
.merge(pipeline =>
({
steps: pipeline('steps').map(step =>
step.merge({
computations: step('computations').map(compId =>
rethink.table('computations').get(compId)
),
})
),
})
)
.run(connection)
)
.then(connection => rethink.table('pipelines')
.get(id)
// Populate computations subfield with computation meta information
.merge(pipeline => ({
steps: pipeline('steps').map(step => step.merge({
computations: step('computations').map(compId => rethink.table('computations').get(compId)),
})),
}))
.run(connection))
.then(result => result);
}

Expand Down
44 changes: 16 additions & 28 deletions packages/coinstac-api-server/src/data/subscriptions.js
Expand Up @@ -17,11 +17,8 @@ const tables = [
const initSubscriptions = (pubsub) => {
tables.forEach((table) => {
helperFunctions.getRethinkConnection()
.then(connection =>
rethink.table(table.tableName).changes().run(connection)
)
.then(feed =>
feed.each((err, change) => {
.then(connection => rethink.table(table.tableName).changes().run(connection))
.then(feed => feed.each((err, change) => {
let val = {};

if (!change.new_val) {
Expand All @@ -33,37 +30,28 @@ const initSubscriptions = (pubsub) => {

if (table.tableName === 'pipelines' && !val.delete) {
return helperFunctions.getRethinkConnection()
.then(connection =>
rethink.table('pipelines')
.then(connection => rethink.table('pipelines')
.get(val.id)
// Populate computations subfield with computation meta information
.merge(pipeline =>
({
steps: pipeline('steps').map(step =>
step.merge({
computations: step('computations').map(compId =>
rethink.table('computations').get(compId)
),
})
),
})
)
.run(connection)
)
.then((result) => {
pubsub.publish(table.subVar, {
[table.subVar]: result,
[table.idVar]: result.id,
// Populate computations subfield with computation meta information
.merge(pipeline => ({
steps: pipeline('steps').map(step => step.merge({
computations: step('computations').map(compId => rethink.table('computations').get(compId)),
})),
}))
.run(connection))
.then((result) => {
pubsub.publish(table.subVar, {
[table.subVar]: result,
[table.idVar]: result.id,
});
});
});
}

pubsub.publish(table.subVar, {
[table.subVar]: val,
[table.idVar]: val.id,
});
})
);
}));
});
};

Expand Down
5 changes: 2 additions & 3 deletions packages/coinstac-api-server/src/index.js
Expand Up @@ -13,7 +13,7 @@ server.connection({

server.register(plugins, (err) => {
if (err) {
console.log(err); // eslint-disable-line no-console
console.log(err); // eslint-disable-line no-console
}

/**
Expand All @@ -25,8 +25,7 @@ server.register(plugins, (err) => {
key: helperFunctions.JWTSecret,
validateFunc: helperFunctions.validateToken,
verifyOptions: { algorithms: ['HS256'] },
}
);
});

server.auth.default('jwt');
server.route(routes);
Expand Down
2 changes: 1 addition & 1 deletion packages/coinstac-api-server/src/plugins.js
@@ -1,6 +1,6 @@
const { schema } = require('./data/schema');
const jwt2 = require('hapi-auth-jwt2');
const { graphiqlHapi, graphqlHapi } = require('apollo-server-hapi');
const { schema } = require('./data/schema');

module.exports = [
{
Expand Down

0 comments on commit 5cf3eef

Please sign in to comment.