diff --git a/server/controllers/index.js b/server/controllers/index.js index 20ec9c9..1debce4 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,10 +1,11 @@ -const { userEstateshandler, login } = require('./users'); -const getAllUsers = require('./users/getAllUsers'); -const { editEstate, deleteEstate } = require('./estates'); +const { + userEstateshandler, putAgent, getAllUsers, login, +} = require('./users'); +const { deleteEstate, editEstate } = require('./estates'); const logout = require('./logout'); const { adminLogin } = require('./admins'); module.exports = { - getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate, adminLogin, + getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate, putAgent,adminLogin }; diff --git a/server/controllers/users/index.js b/server/controllers/users/index.js index adee2b1..d0f5aca 100644 --- a/server/controllers/users/index.js +++ b/server/controllers/users/index.js @@ -1,11 +1,13 @@ const signup = require('./signup'); const userEstateshandler = require('./userEstates'); const getAllUsers = require('./getAllUsers'); +const putAgent = require('./putAgent'); const login = require('./login'); module.exports = { signup, getAllUsers, userEstateshandler, + putAgent, login, }; diff --git a/server/controllers/users/login.js b/server/controllers/users/login.js index 87b99eb..696baf6 100644 --- a/server/controllers/users/login.js +++ b/server/controllers/users/login.js @@ -11,16 +11,16 @@ const login = async (req, res, next) => { await loginSchema.validateAsync(req.body); const { rows } = await checkEmailQuery(email); - if (!rows.length) { return res.status(400).json({ message: 'Invalid email or password' }); } const compared = await bcrypt.compare(password, rows[0].password); + if (!compared) { return res.status(400).json({ message: 'Invalid email or password' }); } - const token = await signToken(email, rows[0].id); + const token = await signToken({ email, userId: rows[0].id }); return res.cookie('token', token).json({ message: 'You are Logged Successfully' }); } catch (err) { if (err.details) { diff --git a/server/controllers/users/putAgent.js b/server/controllers/users/putAgent.js new file mode 100644 index 0000000..c4350f4 --- /dev/null +++ b/server/controllers/users/putAgent.js @@ -0,0 +1,24 @@ +const { putAgent } = require('../../database/quieres'); +const editAgentSchema = require('../../utils/validation/editAgentSchema'); + +module.exports = async (req, res, next) => { + try { + const { userId } = req.user; + const { error, value } = editAgentSchema.validate({ ...req.body, userId }); + if (error) return res.status(400).json({ message: error.details[0].message }); + const { rowCount } = await putAgent(value); + if (rowCount === 1) { + return res.status(200).json({ + message: "Agent's data updated successfully", + }); + } + return res.status(404).json({ + message: 'There\'s no Agent, put correct id', + }); + } catch (err) { + if (err.detail) { + return res.status(400).json({ message: err.detail }); + } + return next(err); + } +}; diff --git a/server/controllers/users/signup.js b/server/controllers/users/signup.js index ab56711..1e61264 100644 --- a/server/controllers/users/signup.js +++ b/server/controllers/users/signup.js @@ -12,8 +12,10 @@ module.exports = async (req, res, next) => { } = agentSchema.validate(req.body); if (error) return res.status(400).json({ message: error.details[0].message }); const hasedPasword = await hash(password, 10); - await signUpQuery(username, email, phone, hasedPasword); - const token = await signToken(email, username, phone); + const { rows } = await signUpQuery(username, email, phone, hasedPasword); + const token = await signToken({ + email, username, phone, userId: rows[0].id, + }); return res.status(201).cookie('token', token).json({ message: 'user created' }); } catch (err) { if (err.code === '23505') { diff --git a/server/database/config/fakeData.sql b/server/database/config/fakeData.sql index 2ad51e6..86061de 100644 --- a/server/database/config/fakeData.sql +++ b/server/database/config/fakeData.sql @@ -1,6 +1,6 @@ -- INSERT INTO agents (name, email, password , phone) - VALUES ('Kai', 'kallport0@patch.com', '$2b$10$gT8Qb2Qe01W1QMRFmH9IC.3bmbA4PS2yG4XQvdkYWxKday.SbjGI2', '677-871-7450'), + VALUES ('Kai', 'kallport0@patch.com', '$2b$10$hZZ2f3zk.pV/9ndMRn78ze47MAh8SN8uy01qeoK8P54tTe526Pqz6', '677-871-7450'), ('Trixie', 'tbeadon1@plala.or.jp', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '0599832685'), ('Allina', 'aburford2@tumblr.com', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '630-385-8312'); diff --git a/server/database/quieres/account/signUp.js b/server/database/quieres/account/signUp.js index 80d429c..39bcd98 100644 --- a/server/database/quieres/account/signUp.js +++ b/server/database/quieres/account/signUp.js @@ -1,3 +1,3 @@ const connection = require('../../config/connection'); -module.exports = (userName, email, phone, password) => connection.query('INSERT INTO agents (name,email,phone,password) VALUES ($1,$2,$3,$4)', [userName, email, phone, password]); +module.exports = (userName, email, phone, password) => connection.query('INSERT INTO agents (name,email,phone,password) VALUES ($1,$2,$3,$4) RETURNING id', [userName, email, phone, password]); diff --git a/server/database/quieres/index.js b/server/database/quieres/index.js index ed76f57..56839d0 100644 --- a/server/database/quieres/index.js +++ b/server/database/quieres/index.js @@ -1,5 +1,7 @@ const userEstatesQuery = require('./userEstatesQuiery'); const getAllUsersQuery = require('./getAllUsersQuery'); +const putAgent = require('./putAgent'); +const deleteEstate = require('./deleteEstateQuery'); const checkAdminQuery = require('./checkAdmin'); const checkEmailQuery = require('./checkEmailQuery'); const editEstateQuery = require('./editEstatesQuery'); @@ -9,6 +11,8 @@ const signUpAdminQuery = require('./signUpAdminQuery'); module.exports = { getAllUsersQuery, userEstatesQuery, + putAgent, + deleteEstate, checkAdminQuery, checkEmailQuery, editEstateQuery, diff --git a/server/database/quieres/putAgent.js b/server/database/quieres/putAgent.js new file mode 100644 index 0000000..69bb01f --- /dev/null +++ b/server/database/quieres/putAgent.js @@ -0,0 +1,5 @@ +const connection = require('../config/connection'); + +module.exports = ({ + userId, username, email, phone, avater = '', +}) => connection.query('UPDATE agents SET name=$1,email=$2,phone=$3,avater=$4 WHERE id=$5', [username, email, phone, avater, userId]); diff --git a/server/routes/auth.js b/server/routes/auth.js deleted file mode 100644 index ad9201b..0000000 --- a/server/routes/auth.js +++ /dev/null @@ -1,7 +0,0 @@ -const router = require('express').Router(); -const { logout, login } = require('../controllers'); - -router.get('/logout', logout); -router.post('/login', login); - -module.exports = router; diff --git a/server/routes/estate.js b/server/routes/estate.js index 687f025..47d4f75 100644 --- a/server/routes/estate.js +++ b/server/routes/estate.js @@ -1,9 +1,7 @@ const router = require('express').Router(); - -// const { isAuth, isAdmin } = require('../middleware'); +const { isAuth } = require('../middleware'); const { editEstate, deleteEstate } = require('../controllers'); router.put('/:estateId', editEstate); - -router.delete('/:estateId', deleteEstate); +router.delete('/:estateId', isAuth, deleteEstate); module.exports = router; diff --git a/server/routes/index.js b/server/routes/index.js index 068e055..ebefe57 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -1,12 +1,12 @@ const router = require('express').Router(); -const auth = require('./auth'); const estate = require('./estate'); const users = require('./users'); +const { logout } = require('../controllers'); const admins = require('./admin'); -router.use('/users', users); +router.get('/logout', logout); router.use('/admin', admins); router.use('/estate', estate); -router.use('/', auth); +router.use('/user', users); module.exports = router; diff --git a/server/routes/users.js b/server/routes/users.js index 70aaffa..7210acc 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -1,11 +1,17 @@ const router = require('express').Router(); -const { userEstateshandler, getAllUsers } = require('../controllers'); +const { + userEstateshandler, getAllUsers, putAgent, login, +} = require('../controllers'); const estate = require('./estate'); const signup = require('../controllers/users/signup'); +const { isAuth } = require('../middleware'); router.use('/estate', estate); router.post('/signup', signup); +router.post('/login', login); router.get('/:userId/estates', userEstateshandler); +router.put('/', isAuth, putAgent); router.get('/', getAllUsers); +router.post('/login', login); module.exports = router; diff --git a/server/test/index.test.js b/server/test/index.test.js index 71b7123..eedad0e 100644 --- a/server/test/index.test.js +++ b/server/test/index.test.js @@ -7,10 +7,12 @@ const connection = require('../database/config/connection'); beforeEach(() => dbBuild()); afterAll(() => connection.end()); +const userToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImthbGxwb3J0MEBwYXRjaC5jb20iLCJ1c2VySWQiOjEsImlhdCI6MTYzNTk0OTE4OX0.LjriIEoRDmj3_52PO8VlsaqekFiItE7gzamngrlaPDk'; + describe('Get all users', () => { test('get all users', async () => { const res = await supertest(app) - .get('/api/v1/users') + .get('/api/v1/user') .expect(200) .expect('Content-Type', /json/); return expect(3).toEqual(res.body.data.length); @@ -20,10 +22,10 @@ describe('Get all users', () => { describe('Tests login route', () => { test(' login route /login ', async () => { const res = await supertest(app) - .post('/api/v1/login') + .post('/api/v1/user/login') .send({ email: 'kallport0@patch.com', - password: '12345', + password: '123456789', }) .expect(200); return expect(res.body).toEqual({ message: 'You are Logged Successfully' }); @@ -31,7 +33,7 @@ describe('Tests login route', () => { test(' login route /login with error in email or password ', async () => { const res = await supertest(app) - .post('/api/v1/login') + .post('/api/v1/user/login') .send({ email: 'kallport0@patch.com', password: '123456987', @@ -68,7 +70,7 @@ describe('Tests login route to admin', () => { describe('user estates', () => { test('get users estates', async () => { const res = await supertest(app) - .get('/api/v1/users/3/estates') + .get('/api/v1/user/3/estates') .expect(200) .expect('Content-Type', /json/); return expect(res.body).toEqual({ @@ -120,7 +122,7 @@ describe('user estates', () => { describe('user estates', () => { test('get users estates', async () => { const res = await supertest(app) - .get('/api/v1/users/three/estates') + .get('/api/v1/user/three/estates') .expect(404) .expect('Content-Type', /json/); return expect(res.body).toEqual({ @@ -180,6 +182,7 @@ describe('Delete Specific Estate By Using Id', () => { test('/estate/:estateId status 200 ', async () => { const res = await supertest(app) .delete('/api/v1/estate/1') + .set('Cookie', [`token=${userToken}`]) .expect(200) .expect('Content-Type', /json/); return expect(res.body).toEqual({ @@ -189,6 +192,7 @@ describe('Delete Specific Estate By Using Id', () => { test('/estate/:estateId status 400, when delete the same estate was deleted or not found ', async () => { const res = await supertest(app) .delete('/api/v1/estate/100') + .set('Cookie', [`token=${userToken}`]) .expect(400) .expect('Content-Type', /json/); return expect(res.body).toEqual({ @@ -198,6 +202,7 @@ describe('Delete Specific Estate By Using Id', () => { test('/estate/:estateId status 400, Invalid estate id ', async () => { const res = await supertest(app) .delete('/api/v1/estate/-121') + .set('Cookie', [`token=${userToken}`]) .expect(400) .expect('Content-Type', /json/); return expect(res.body).toEqual({ @@ -205,10 +210,11 @@ describe('Delete Specific Estate By Using Id', () => { }); }); }); + describe('test signup endpoint with all cases ', () => { test('test sign up endpoint when success', async () => { const res = await supertest(app) - .post('/api/v1/users/signup') + .post('/api/v1/user/signup') .send({ username: 'test', password: 'test123456', @@ -226,7 +232,7 @@ describe('test signup endpoint with all cases ', () => { test('test signup error validation phone" length must be 10 characters long ', async () => { const res = await supertest(app) - .post('/api/v1/users/signup') + .post('/api/v1/user/signup') .send({ username: 'Kai', password: '1234567894455', @@ -242,7 +248,7 @@ describe('test signup endpoint with all cases ', () => { }); test('test signup username or phone already exists ', async () => { const res = await supertest(app) - .post('/api/v1/users/signup') + .post('/api/v1/user/signup') .send({ username: 'Kai', password: '1234567894455', @@ -259,7 +265,7 @@ describe('test signup endpoint with all cases ', () => { test('test signup confirmpassword ', async () => { const res = await supertest(app) - .post('/api/v1/users/signup') + .post('/api/v1/user/signup') .send({ username: 'test', password: 'test123456', @@ -275,6 +281,55 @@ describe('test signup endpoint with all cases ', () => { }); }); +describe('test Edit Agent data /user/:iduser ', () => { + test('test 200', async () => { + const res = await supertest(app) + .put('/api/v1/user') + .set('Cookie', [`token=${userToken}`]) + .send({ + username: 'test', + email: 'kallport0@patch.com', + phone: '059985555555', + }) + .expect(200) + .expect('Content-Type', /json/); + return expect(res.body).toEqual({ + message: 'Agent\'s data updated successfully', + }); + }); + + test('test 400', async () => { + const res = await supertest(app) + .put('/api/v1/user') + .set('Cookie', [`token=${userToken}`]) + .send({ + username: 'test', + email: 'kallport0@patch.com', + phone: '0599', + }) + .expect(400) + .expect('Content-Type', /json/); + return expect(res.body).toEqual({ + message: '"phone" length must be at least 9 characters long', + }); + }); + test('test 404', async () => { + const res = await supertest(app) + .put('/api/v1/user') + .set('Cookie', ['token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Im1hdTdhbW1hZGFiZWRAZ21haWwuY29tIiwidXNlcklkIjo0LCJpYXQiOjE2MzU5NDkyNTl9.St177PIpsDIHAVke6PxoGC8_cJmUrggpyhEcJ4QWKfI']) + .send({ + username: 'test', + email: 'kallport0@patch.com', + phone: '059915587555', + }) + .expect(404) + .expect('Content-Type', /json/); + return expect(res.body).toEqual({ + message: 'There\'s no Agent, put correct id', + }); + }); +}); + describe('test signup as admin ', () => { test('test sign up endpoint when success', async () => { const res = await supertest(app) diff --git a/server/utils/tokenFunction.js b/server/utils/tokenFunction.js index c7ac90e..d6d1b4a 100644 --- a/server/utils/tokenFunction.js +++ b/server/utils/tokenFunction.js @@ -1,4 +1,5 @@ const { verify, sign } = require('jsonwebtoken'); +require('env2')('.env'); const { env: { ACCESS_TOKEN_SECRET }, diff --git a/server/utils/validation/editAgentSchema.js b/server/utils/validation/editAgentSchema.js new file mode 100644 index 0000000..c404d63 --- /dev/null +++ b/server/utils/validation/editAgentSchema.js @@ -0,0 +1,9 @@ +const joi = require('joi'); + +module.exports = joi.object({ + username: joi.string().required(), + email: joi.string().email().required(), + phone: joi.string().min(9).required(), + avater: joi.string(), + userId: joi.number().min(1).required(), +});