Skip to content

Commit

Permalink
ch[#161841595] Remove role from login
Browse files Browse the repository at this point in the history
change type to role in users table
  • Loading branch information
ebenezerdon committed Nov 9, 2018
1 parent 7c2bb32 commit 2b08619
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 55 deletions.
2 changes: 1 addition & 1 deletion server/models/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import pool from './db';
const createTables = () => {
pool.query('CREATE TABLE IF NOT EXISTS products(id serial PRIMARY KEY, productname VARCHAR(100), description TEXT, productimage TEXT, price VARCHAR(30), quantity INTEGER, minallowed INTEGER)');
pool.query('CREATE TABLE IF NOT EXISTS sales(id serial PRIMARY KEY, productname VARCHAR(100), productId INTEGER, price VARCHAR(30), quantity INTEGER, attendant_id INTEGER, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)');
pool.query('CREATE TABLE IF NOT EXISTS users(id serial PRIMARY KEY, fullname VARCHAR(100), emailaddress TEXT, password TEXT, type TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)');
pool.query('CREATE TABLE IF NOT EXISTS users(id serial PRIMARY KEY, fullname VARCHAR(100), emailaddress TEXT, password TEXT, role TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)');
};

const dropTables = () => {
Expand Down
4 changes: 2 additions & 2 deletions server/models/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const seedSales = () => {

const seedUsers = () => {
const text1 = `INSERT INTO
users(fullname, emailaddress, password, type)
users(fullname, emailaddress, password, role)
VALUES($1, $2, $3, $4)`;
const values1 = [
'Admin',
Expand All @@ -66,7 +66,7 @@ const seedUsers = () => {
'admin',
];
const text2 = `INSERT INTO
users(fullname, emailaddress, password, type)
users(fullname, emailaddress, password, role)
VALUES($1, $2, $3, $4)`;
const values2 = [
'attendant',
Expand Down
13 changes: 6 additions & 7 deletions server/routes/controllers/usersController.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ const addUser = (req, res) => {
});
}
const addQuery = `INSERT INTO
users(fullname, emailaddress, password, type)
users(fullname, emailaddress, password, role)
VALUES($1, $2, $3, $4)
returning *`;
const values = [
body.fullname,
body.emailaddress,
body.password,
body.type,
body.role,
];
pool.query(addQuery, values, (err, data) => {
if (err) throw err;
Expand All @@ -58,13 +58,13 @@ const updateUser = (req, res) => {
body
} = req;
const text = `UPDATE users
SET fullname=$1, emailaddress=$2, password=$3, type=$4
SET fullname=$1, emailaddress=$2, password=$3, role=$4
WHERE id=$5 returning *`;
const values = [
body.fullname,
body.emailaddress,
body.password,
body.type,
body.role,
req.params.id,
];
pool.query(text, values, (err, data) => {
Expand All @@ -81,7 +81,7 @@ const updateUser = (req, res) => {

const makeAdmin = (req, res) => {
const text = `UPDATE users
SET type=$1
SET role=$1
WHERE id=$2 returning *`;
const values = [
'admin',
Expand Down Expand Up @@ -120,11 +120,10 @@ const loginUser = (req, res) => {
const { body } = req;
const query = {
text: `SELECT * FROM users WHERE
emailaddress = $1 AND password = $2 AND type=$3`,
emailaddress = $1 AND password = $2`,
values: [
body.emailaddress,
body.password,
body.type,
],
};
pool.query(query).then((data) => {
Expand Down
7 changes: 1 addition & 6 deletions server/routes/middleware/validateinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ const validateUserInput = (req, res, next) => {
res.status(400).json('The user\'s password has to be more than 6 characters!')
);
}
if (!body.type) {
return (
res.status(400).json('There really has to be a type!')
);
}
return next();
};

Expand All @@ -36,7 +31,7 @@ const validateUserSignup = (req, res, next) => {
);
}
if(!body.fullname || !body.emailaddress
|| !body.type || !body.password) {
|| !body.role || !body.password) {
return res.status(400).json({
message: 'Some details are missing. Maybe check and try again?',
success: false
Expand Down
4 changes: 2 additions & 2 deletions server/routes/middleware/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const authenticate = (req, res, next) => {

const verifyAdmin = (req, res, next) => {
const { decoded } = req;
if (decoded.type !== 'admin') {
if (decoded.role !== 'admin') {
return res.status(401).json({
message: 'Hi! This resource can only be accessed by an admin',
error: true,
Expand All @@ -36,7 +36,7 @@ const verifyAdmin = (req, res, next) => {

const verifyAttendant = (req, res, next) => {
const { decoded } = req;
if (decoded.type !== 'attendant') {
if (decoded.role !== 'attendant') {
return res.status(401).json({
message: 'Hi! This resource can only be accessed by an attendant',
error: true,
Expand Down
11 changes: 0 additions & 11 deletions server/tests/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('Get Products', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -30,7 +29,6 @@ describe('Get Products', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -49,7 +47,6 @@ describe('Get Products', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand Down Expand Up @@ -79,7 +76,6 @@ describe('Create New Product', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -106,7 +102,6 @@ describe('Create New Product', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand Down Expand Up @@ -134,7 +129,6 @@ describe('Create New Product', () => {
.send({
emailaddress: 'attendant@gmail.com',
password: 'attendantpassword',
type: 'attendant',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -161,7 +155,6 @@ describe('Create New Product', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand Down Expand Up @@ -197,7 +190,6 @@ describe('Update Product', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand Down Expand Up @@ -225,7 +217,6 @@ describe('Update Product', () => {
.send({
emailaddress: 'attendant@gmail.com',
password: 'attendantpassword',
type: 'attendant',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -252,7 +243,6 @@ describe('Update Product', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand Down Expand Up @@ -288,7 +278,6 @@ describe('Delete Product', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand Down
14 changes: 0 additions & 14 deletions server/tests/sales.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('Get sales', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -30,7 +29,6 @@ describe('Get sales', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -49,7 +47,6 @@ describe('Get sales', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -68,7 +65,6 @@ describe('Get sales', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -86,7 +82,6 @@ describe('Get sales', () => {
.send({
emailaddress: 'attendant@gmail.com',
password: 'attendantpassword',
type: 'attendant',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -104,7 +99,6 @@ describe('Get sales', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -128,7 +122,6 @@ describe('Get sales', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand Down Expand Up @@ -158,7 +151,6 @@ describe('Create New sale', () => {
.send({
emailaddress: 'attendant@gmail.com',
password: 'attendantpassword',
type: 'attendant',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -184,7 +176,6 @@ describe('Create New sale', () => {
.send({
emailaddress: 'attendant@gmail.com',
password: 'attendantpassword',
type: 'attendant',
})
.end((err, res) => {
const token = res.body;
Expand Down Expand Up @@ -221,7 +212,6 @@ describe('Update sales record', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -248,7 +238,6 @@ describe('Update sales record', () => {
.send({
emailaddress: 'attendant@gmail.com',
password: 'attendantpassword',
type: 'attendant',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -274,7 +263,6 @@ describe('Update sales record', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand Down Expand Up @@ -311,7 +299,6 @@ describe('Delete sale record', () => {
.send({
emailaddress: 'admin@gmail.com',
password: 'adminpassword',
type: 'admin',
})
.end((err, res) => {
const token = res.body;
Expand All @@ -332,7 +319,6 @@ describe('Delete sale record', () => {
.send({
emailaddress: 'attendant@gmail.com',
password: 'attendantpassword',
type: 'attendant',
})
.end((err, res) => {
const token = res.body;
Expand Down
Loading

0 comments on commit 2b08619

Please sign in to comment.