Skip to content

Commit

Permalink
improve code formatting rules
Browse files Browse the repository at this point in the history
- add .editorconfig
- force unix line endings
- add eslint rules
- reformat code with new rules
  • Loading branch information
kbkk committed Mar 27, 2019
1 parent d5ddb75 commit d2b87b3
Show file tree
Hide file tree
Showing 49 changed files with 295 additions and 244 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.js]
indent_style = tab
indent_size = 2
33 changes: 30 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@

"indent": [
"error",
"tab"
"tab",
{
"SwitchCase": 1,
"ObjectExpression": 1
}
],
"linebreak-style": [
"error",
"windows"
"unix"
],
"quotes": [
"error",
Expand All @@ -34,6 +38,29 @@
"semi": [
"error",
"always"
],
"eol-last": [
"error",
"always"
],
"space-before-blocks": [
"error",
"always"
],
"space-before-function-paren": [
"error",
"never"
],
"object-curly-spacing": [
"error",
"never"
],
"keyword-spacing": [
"error",
{
"before": true,
"after": true
}
]
}
}
}
5 changes: 3 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dotenv from 'dotenv';

dotenv.config({path: '.env'});
import express from 'express';
import bodyParser from 'body-parser';
Expand All @@ -22,7 +23,7 @@ const app = express();
passport();

//Setup MongoDB
mongoose.connect(process.env.DB_HOST, {useNewUrlParser: true });
mongoose.connect(process.env.DB_HOST, {useNewUrlParser: true});
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
checkDatabaseStatus(process.env.DB_HOST);
Expand All @@ -44,4 +45,4 @@ app.use('/privileged-users', privilegedUsersRouter);
app.use(errorsHandler.notFound);
app.use(errorsHandler.catchErrors);

module.exports = app;
module.exports = app;
12 changes: 6 additions & 6 deletions src/checkDatabaseStatus.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import mongoose from 'mongoose';
import chalk from 'chalk';

const { cyan } = chalk.bold;
const { yellow } = chalk.bold.yellow;
const { red } = chalk.bold.red;
const {cyan} = chalk.bold;
const {yellow} = chalk.bold.yellow;
const {red} = chalk.bold.red;

function checkDatabaseStatus(url) {
const db = mongoose.connection;

db.on('connected', function(){
db.on('connected', function() {
console.log(cyan(`Mongoose connection is open to ${url}`));
});

mongoose.connection.on('error', function(err){
mongoose.connection.on('error', function(err) {
console.log(red(`Mongoose connection has occured ${err} error`));
});

mongoose.connection.on('disconnected', function(){
mongoose.connection.on('disconnected', function() {
console.log(yellow('Mongoose connection is disconnected'));
});
}
Expand Down
15 changes: 10 additions & 5 deletions src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import jwt from 'jsonwebtoken';

export default {
async login(req, res) {
const token = jwt.sign({ id: req.user._id }, process.env.JWT_SECRET, { expiresIn: 3600 });
const token = jwt.sign({id: req.user._id}, process.env.JWT_SECRET, {expiresIn: 3600});
return res.status(200).json({
success: true,
message: 'User signed in successfully.',
Expand All @@ -14,20 +14,25 @@ export default {
async register(req, res) {
const {name, email, phone, gender, birthDate, purpose, password, geometry} = req.body;
let profilePicture = '';
if(req.file) {
if (req.file) {
profilePicture = req.file.path;
}
const user = new User({name, email, password, phone, gender, birthDate, purpose, profilePicture, geometry});
const detailsToShow = (({ _id, name, email, profilePicture, geometry }) => ({ _id, name, email, profilePicture, geometry }))(user);
const detailsToShow = (({_id, name, email, profilePicture, geometry}) => ({
_id,
name,
email,
profilePicture,
geometry
}))(user);
try {
await User.register(user, password);
return res.status(201).json({
success: true,
message: 'User registered successfully.',
user: detailsToShow
});
}
catch(err) {
} catch (err) {
return res.status(409).json(err);
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/controllers/errorTicketController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
})
.skip(offset).limit(perPage)
.then(item => {
if(!item.length || !item) return res.status(404).json({
if (!item.length || !item) return res.status(404).json({
success: false,
message: 'Error tickets not found.'
});
Expand All @@ -34,10 +34,12 @@ export default {
select: '-__v -salt -hash -createdAt -updatedAt'
})
.then(item => {
if(!item) return res.status(404).json({
success: false,
message: `Error ticket with ID ${req.params.id} not found.`
});
if (!item) {
return res.status(404).json({
success: false,
message: `Error ticket with ID ${req.params.id} not found.`
});
}
res.status(200).json({
success: true,
data: item
Expand Down Expand Up @@ -68,7 +70,7 @@ export default {
createNew(req, res) {
const errorTicket = new ErrorTicket(req.body);
return errorTicket.save()
.then(item => res.status(201).json({
.then(item => res.status(201).json({
success: true,
message: 'Error ticket created successfully.',
data: item
Expand All @@ -78,10 +80,10 @@ export default {
message: err
}));
},
deleteOne(req, res) {
deleteOne(req, res) {
return ErrorTicket.findById(req.params.id)
.then(item => {
if(!item) return res.status(404).json({
if (!item) return res.status(404).json({
success: false,
message: `Error ticket with ID ${req.params.id} not found.`
});
Expand All @@ -96,4 +98,4 @@ export default {
message: err
}));
}
};
};
28 changes: 15 additions & 13 deletions src/controllers/matchController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Match from '../models/Match';

export default {
export default {
async getAll(req, res) {
const offset = parseInt(req.query.offset) || 0;
const perPage = parseInt(req.query.perPage) || 100;
Expand All @@ -16,7 +16,7 @@ export default {
})
.skip(offset).limit(perPage)
.then(item => {
if(!item.length || !item) return res.status(404).json({
if (!item.length || !item) return res.status(404).json({
success: false,
message: 'Matches not found.'
});
Expand All @@ -42,11 +42,11 @@ export default {
select: 'name profilePicture _id'
})
.then(item => {
if(!item) return res.status(404).json({
if (!item) return res.status(404).json({
success: false,
message: `Match with ID ${req.params.id} not found.`
});
if(req.user._id.equals(item.user1._id) || req.user._id.equals(item.user2._id)) {
if (req.user._id.equals(item.user1._id) || req.user._id.equals(item.user2._id)) {
res.status(200).json({
success: true,
data: item
Expand All @@ -64,17 +64,19 @@ export default {
}));
},
getResourcesOfUser(req, res) {
return Match.find({$or:
return Match.find({
$or:
[
{'user1': req.params.id},
{'user2': req.params.id}
]})
]
})
.then(item => {
if(!item.length || !item) return res.status(404).json({
if (!item.length || !item) return res.status(404).json({
success: false,
message: `Resources of user ID ${req.params.id} not found.`
});
if(req.user._id.equals(item.user1._id) || req.user._id.equals(item.user2._id)) {
if (req.user._id.equals(item.user1._id) || req.user._id.equals(item.user2._id)) {
res.status(200).json(item);
} else {
res.status(403).json({
Expand Down Expand Up @@ -104,11 +106,11 @@ export default {
updateOne(req, res) {
return Match.findById(req.params.id)
.then(item => {
if(!item) return res.status(404).json({
if (!item) return res.status(404).json({
success: false,
message: `Match with ID ${req.params.id} not found.`
});
if(req.user._id.equals(item.user1._id) || req.user._id.equals(item.user2._id)) {
if (req.user._id.equals(item.user1._id) || req.user._id.equals(item.user2._id)) {
const match = Object.assign(item, req.body);
match.save()
.then(item => res.status(200).json({
Expand All @@ -131,11 +133,11 @@ export default {
deleteOne(req, res) {
return Match.findById(req.params.id)
.then(item => {
if(!item) return res.status(404).json({
if (!item) return res.status(404).json({
success: false,
message: `Match with ID ${req.params.id} not found.`
});
if(req.user._id.equals(item.user1._id) || req.user._id.equals(item.user2._id)) {
if (req.user._id.equals(item.user1._id) || req.user._id.equals(item.user2._id)) {
item.delete({_id: req.params.id});
res.status(200).json({
success: true,
Expand All @@ -153,4 +155,4 @@ export default {
message: err
}));
}
};
};
Loading

0 comments on commit d2b87b3

Please sign in to comment.