Skip to content

Commit

Permalink
Merge branch 'develop' into feature/edit-single-product-route
Browse files Browse the repository at this point in the history
  • Loading branch information
alehuo authored May 7, 2018
2 parents dfe080e + f39dccf commit 1aaa902
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 49 deletions.
65 changes: 30 additions & 35 deletions src/db/userStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,61 @@ const bcrypt = require('bcrypt');
module.exports.getUsers = () => {
return knex('RVPERSON')
.select('*')
.then((persons) => {
.then(persons => {
return persons;
});
};

module.exports.findByUsername = (username) => {
module.exports.findByUsername = username => {
return knex('RVPERSON')
.where('RVPERSON.name', '=', username)
.select('*')
.then((rows) => {
return rows.length > 0 ? rows[0] : null;
});
.first();
};

module.exports.findHighestUserId = () => {
return knex('RVPERSON')
.max('userid')
.then((rows) => {
return rows.length > 0 ? rows[0] : null;
});
.first();
};

module.exports.findByEmail = (email) => {
module.exports.findByEmail = email => {
return knex('RVPERSON')
.where('RVPERSON.univident', '=', email)
.select('*')
.then((rows) => {
return rows.length > 0 ? rows[0] : null;
});
.first();
};

module.exports.insertUser = (user, highestId) => {
return knex('RVPERSON')
.insert(
{
userid: highestId + 1,
createdate: new Date(),
roleid: 2,
name: user.username,
univident: user.email.trim(),
pass: bcrypt.hashSync(user.password, 11),
saldo: 0,
realname: user.realname
}
);
return knex('RVPERSON').insert({
userid: highestId + 1,
createdate: new Date(),
roleid: 2,
name: user.username,
univident: user.email.trim(),
pass: bcrypt.hashSync(user.password, 11),
saldo: 0,
realname: user.realname
});
};

module.exports.verifyPassword = (password, passwordHash) => {
return bcrypt.compare(password, passwordHash);
};

module.exports.findUserRoles = (username) => {
return knex.select('role')
module.exports.findUserRoles = username => {
return knex
.select('role')
.from('ROLE')
.join('RVPERSON', function() {
this.on('RVPERSON.roleid', '=', 'ROLE.roleid')
.andOn('RVPERSON.name', '=', knex.raw('?', [username]));
this.on('RVPERSON.roleid', '=', 'ROLE.roleid').andOn(
'RVPERSON.name',
'=',
knex.raw('?', [username])
);
})
.then((rows) => {
return rows.map((row) => row.role);
.then(rows => {
return rows.map(row => row.role);
});
};

Expand All @@ -74,8 +69,8 @@ module.exports.updateAccountBalance = (username, difference) => {
.select('userid', 'saldo')
.from('RVPERSON')
.where({ name: username })
.then((rows) => rows[0])
.then((user) => {
.then(rows => rows[0])
.then(user => {
user.saldo += difference;
return knex('RVPERSON')
.transacting(trx)
Expand All @@ -85,7 +80,7 @@ module.exports.updateAccountBalance = (username, difference) => {
})
.then(() => user);
})
.then((user) => {
.then(user => {
return knex
.transacting(trx)
.insert({
Expand All @@ -98,4 +93,4 @@ module.exports.updateAccountBalance = (username, difference) => {
.then(() => user.saldo);
});
});
};
};
18 changes: 8 additions & 10 deletions src/routes/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ router.post('/', async (req, res) => {
res
.status(400)
.json({
error: 'Username has at least 4 characters.'
error: 'Username has less than 4 characters.'
})
.end();
return;
} else if (body.password.length < 4) {
res
.status(400)
.json({
error: 'Password has at least 4 characters.'
error: 'Password has less than 4 characters.'
})
.end();
return;
Expand All @@ -46,23 +46,21 @@ router.post('/', async (req, res) => {
// Check if user, email exists
const user = await userStore.findByUsername(body.username);
if (user) {
res
return res
.status(403)
.json({
error: 'Username already in use.'
error: 'Username is already in use.'
})
.end();
return;
}
const userEmail = await userStore.findByEmail(body.email.trim());
if (userEmail) {
res
return res
.status(403)
.json({
error: 'Email already in use.'
error: 'Email address already in use.'
})
.end();
return;
}

// All ok
Expand All @@ -72,10 +70,10 @@ router.post('/', async (req, res) => {
const highestId = await userStore.findHighestUserId();
const inserted = await userStore.insertUser(body, highestId.max);
logger.info('Registered new user: ' + body.username);
res.status(201).json(inserted);
return res.status(201).json(inserted);
} catch (exception) {
logger.info('Error registering new user: ' + exception);
res.status(500).json({
return res.status(500).json({
error_code: 'internal_error',
message: exception
});
Expand Down
8 changes: 4 additions & 4 deletions test/register/routes.register.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('routes: register', () => {
.end((err, res) => {
should.exist(err);
res.status.should.equal(400);
expect(res.body.error).to.equal('Username has at least 4 characters.');
expect(res.body.error).to.equal('Username has less than 4 characters.');
done();
});
});
Expand All @@ -83,7 +83,7 @@ describe('routes: register', () => {
.end((err, res) => {
should.exist(err);
res.status.should.equal(400);
expect(res.body.error).to.equal('Password has at least 4 characters.');
expect(res.body.error).to.equal('Password has less than 4 characters.');
done();
});
});
Expand All @@ -104,7 +104,7 @@ describe('routes: register', () => {
.end((err, res) => {
should.exist(err);
res.status.should.equal(403);
expect(res.body.error).to.equal('Username already in use.');
expect(res.body.error).to.equal('Username is already in use.');
done();
});
});
Expand All @@ -121,7 +121,7 @@ describe('routes: register', () => {
.end((err, res) => {
should.exist(err);
res.status.should.equal(403);
expect(res.body.error).to.equal('Email already in use.');
expect(res.body.error).to.equal('Email address already in use.');
done();
});
});
Expand Down

0 comments on commit 1aaa902

Please sign in to comment.