Skip to content

Commit

Permalink
create handlers update email, update product price and create product…
Browse files Browse the repository at this point in the history
… and tests from api b00tc4mp#101
  • Loading branch information
LucasOrtsBaguena committed Aug 23, 2024
1 parent 46ff525 commit 154e682
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 34 deletions.
15 changes: 15 additions & 0 deletions staff/lucas-orts/project/api/handlers/createProductHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { logic } from 'cor'

export default (req, res, next) => {
const { userId } = req

const { name, type, minprice, maxprice, image } = req.body

try {
logic.createProduct(userId, name, type, minprice, maxprice, image)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
}
14 changes: 13 additions & 1 deletion staff/lucas-orts/project/api/handlers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import registerUserHandler from './registerUserHandler.js'
import authenticateUserHandler from './authenticateUserHandler.js'
import getUserNameHandler from './getUserNameHandler.js'
import updatePasswordHandler from './updatePasswordHandler.js'
import updateUserAddressHandler from './updateUserAddressHandler.js'
import updateUserPhoneHandler from './updateUserPhoneHandler.js'
import updateEmailHandler from './updateEmailHandler.js'
import createProductHandler from './createProductHandler.js'
import updateProductPriceHandler from './updateProductPriceHandler.js'

export {
registerUserHandler,
authenticateUserHandler,
getUserNameHandler
getUserNameHandler,
updatePasswordHandler,
updateUserAddressHandler,
updateUserPhoneHandler,
updateEmailHandler,
createProductHandler,
updateProductPriceHandler
}
15 changes: 15 additions & 0 deletions staff/lucas-orts/project/api/handlers/updateEmailHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { logic } from 'cor'

export default (req, res, next) => {
const { userId } = req

const { email, password } = req.body

try {
logic.updateEmail(userId, email, password)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { logic } from '../../cor/index.js'
export default (req, res, next) => {
const { userId } = req

const { oldPassword, newPassword } = req.body
const { oldPassword, newPassword, newPasswordRepeat } = req.body

try {
logic.updatePassword(userId, oldPassword, newPassword)
logic.updatePassword(userId, oldPassword, newPassword, newPasswordRepeat)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
Expand Down
17 changes: 17 additions & 0 deletions staff/lucas-orts/project/api/handlers/updateProductPriceHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { logic } from 'cor'

export default (req, res, next) => {
const { userId } = req

const { productId } = req.params

const { minprice, maxprice } = req.body

try {
logic.updateProductPrice(userId, productId, minprice, maxprice)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

export default (req, res, next) => {
const { userId } = req
Expand Down
18 changes: 18 additions & 0 deletions staff/lucas-orts/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import {
registerUserHandler,
authenticateUserHandler,
getUserNameHandler,
updatePasswordHandler,
updateUserAddressHandler,
updateUserPhoneHandler,
updateEmailHandler,
createProductHandler,
updateProductPriceHandler
} from './handlers/index.js'

mongoose.connect(process.env.MONGODB_URI)
Expand All @@ -24,6 +30,18 @@ mongoose.connect(process.env.MONGODB_URI)

api.get('/users/:targetUserId/name', jwtVerifier, getUserNameHandler)

api.patch('/users/password', jwtVerifier, jsonBodyParser, updatePasswordHandler)

api.patch('/users/address', jwtVerifier, jsonBodyParser, updateUserAddressHandler)

api.patch('/users/phone', jwtVerifier, jsonBodyParser, updateUserPhoneHandler)

api.patch('/users/email', jwtVerifier, jsonBodyParser, updateEmailHandler)

api.post('/products', jwtVerifier, jsonBodyParser, createProductHandler)

api.patch('/products/price', jwtVerifier, jsonBodyParser, updateProductPriceHandler)

api.use(errorHandler)

api.listen(process.env.PORT, () => console.info(`API listening on PORT ${process.env.PORT}`))
Expand Down
1 change: 1 addition & 0 deletions staff/lucas-orts/project/api/test/update-address.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -v http://localhost:8080/users/address -X PATCH -d '{"address":"Calle Loturia 5, Marbella"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM3NGM4YTM2MGNmY2UwN2U3OWI5YTIiLCJpYXQiOjE3MjQzMzczNTN9.XbsybN3JliqNyBnOcK59BNJ88Btz_lbdaWRD8Tk4xTY"
1 change: 1 addition & 0 deletions staff/lucas-orts/project/api/test/update-email.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -v http://localhost:8080/users/email -X PATCH -d '{"email":"[email protected]", "password":"123456789"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM3NGM4YTM2MGNmY2UwN2U3OWI5YTIiLCJpYXQiOjE3MjQzMzczNTN9.XbsybN3JliqNyBnOcK59BNJ88Btz_lbdaWRD8Tk4xTY"
1 change: 1 addition & 0 deletions staff/lucas-orts/project/api/test/update-password.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -v http://localhost:8080/users/password -X PATCH -d '{"oldPassword":"123123123", "newPassword":"123456789", "newPasswordRepeat":"123456789"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM3NGM4YTM2MGNmY2UwN2U3OWI5YTIiLCJpYXQiOjE3MjQzMzczNTN9.XbsybN3JliqNyBnOcK59BNJ88Btz_lbdaWRD8Tk4xTY"
1 change: 1 addition & 0 deletions staff/lucas-orts/project/api/test/update-phone.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -v http://localhost:8080/users/phone -X PATCH -d '{"phone":"+34 965152331"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM3NGM4YTM2MGNmY2UwN2U3OWI5YTIiLCJpYXQiOjE3MjQzMzczNTN9.XbsybN3JliqNyBnOcK59BNJ88Btz_lbdaWRD8Tk4xTY"
6 changes: 5 additions & 1 deletion staff/lucas-orts/project/cor/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import updateEmail from './updateEmail.js'
import updatePassword from './updatePassword.js'
import updateUserAddress from './updateUserAddress.js'
import updateUserPhone from './updateUserPhone.js'
import createProduct from './createProduct.js'
import updateProductPrice from './updateProductPrice.js'

const logic = {
registerUser,
Expand All @@ -13,7 +15,9 @@ const logic = {
updateEmail,
updatePassword,
updateUserAddress,
updateUserPhone
updateUserPhone,
createProduct,
updateProductPrice
}

export default logic
34 changes: 11 additions & 23 deletions staff/lucas-orts/project/cor/logic/updateEmail.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { User } from '../data/models.js'

import errors from '../../com/errors.js'

const { NotFoundError, ValidationError } = errors
const { NotFoundError, ValidationError, CredentialsError } = errors

describe('updateEmail', () => {
before(() => mongoose.connect(process.env.MONGODB_URI))
Expand All @@ -22,10 +22,10 @@ describe('updateEmail', () => {
debugger
return bcrypt.hash('123123123', 8)
.then(hash => User.create({ name: 'Ester', surname: 'Colero', email: '[email protected]', phone: '966234731', address: 'calle Tertulia 3, Cuenca', password: hash }))
.then(user => updateEmail(user.id, 'Peta@zeta.com', '123123123')
.then(() => User.findOne({ email: 'Peta@zeta.com' }).lean()
.then(user => updateEmail(user.id, 'peta@zeta.com', '123123123')
.then(() => User.findOne({ email: 'peta@zeta.com' }).lean()
.then(user => {
expect(user.email).to.equal('Peta@zeta.com')
expect(user.email).to.equal('peta@zeta.com')
})
.then(match => expect(match).to.be.true)
)
Expand All @@ -35,31 +35,19 @@ describe('updateEmail', () => {
it('fails on non-existing user', () => {
let _error

return updateEmail(new ObjectId().toString(), 'Peta@zeta.com', '123123123')
return updateEmail(new ObjectId().toString(), 'peta@zeta.com', '123123123')
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(NotFoundError)
expect(_error.message).to.equal('user not found')
})
})

it('fails on non matching passwords', () => {
let _error

return User.create({ name: 'Ester', surname: 'Colero', email: '[email protected]', phone: '966234731', address: 'calle Tertulia 3, Cuenca', password: '123123123' })
.then(user => updateEmail(user.id, '[email protected]', '123123124'))
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(ValidationError)
expect(_error.message).to.equal('wrong password')
})
})

it('fails on non-string userId', () => {
let error

try {
updateEmail(123, 'Peta@zeta.com', '123123123')
updateEmail(123, 'peta@zeta.com', '123123123')
} catch (_error) {
error = _error
} finally {
Expand All @@ -72,7 +60,7 @@ describe('updateEmail', () => {
let error

try {
updateEmail(new ObjectId().toString(), 'Peta@zeta.com', 123123123)
updateEmail(new ObjectId().toString(), 'peta@zeta.com', 123123123)
} catch (_error) {
error = _error
} finally {
Expand All @@ -85,7 +73,7 @@ describe('updateEmail', () => {
let error

try {
updateEmail(new ObjectId().toString(), 'Peta@zeta.com', '123123')
updateEmail(new ObjectId().toString(), 'peta@zeta.com', '123123')
} catch (_error) {
error = _error
} finally {
Expand All @@ -98,7 +86,7 @@ describe('updateEmail', () => {
let error

try {
updateEmail(new ObjectId().toString(), 'Peta@zeta.com', '123123 123')
updateEmail(new ObjectId().toString(), 'peta@zeta.com', '123123 123')
} catch (_error) {
error = _error
} finally {
Expand All @@ -111,11 +99,11 @@ describe('updateEmail', () => {
let error

try {
updateEmail(new ObjectId().toString(), 'Peta@zeta.com', '123123123')
updateEmail(new ObjectId().toString(), 'peta@zeta.com', '123123123')
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(Error)
expect(error).to.be.instanceOf(CredentialsError)
expect(error.message).to.equal('passwords do not match')
}
})
Expand Down
2 changes: 1 addition & 1 deletion staff/lucas-orts/project/cor/logic/updateEmail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import updateEmail from './updateEmail.js'
import mongoose from 'mongoose'

mongoose.connect(process.env.MONGODB_URI)
.then(() => updateEmail('66c44943fedca00b82f34b98', '[email protected]', '123123123'))
.then(() => updateEmail('66c7435ea25f13cef7c37a31', '[email protected]', '123123123'))
.then(() => console.log('Email updated'))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())
8 changes: 4 additions & 4 deletions staff/lucas-orts/project/cor/logic/updateProductPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { User, Product } from '../data/models.js'
import { validate, errors } from '../../com/index.js'
const { NotFoundError, SystemError } = errors

export default (userId, productId, minprize, maxprize) => {
export default (userId, productId, minprice, maxprice) => {
validate.string(userId, 'userId')
validate.string(productId, 'productId')
validate.number(minprize, 'minprize')
validate.number(maxprize, 'maxprize')
validate.number(minprice, 'minprice')
validate.number(maxprice, 'maxprice')

return User.findById(userId).lean()
.catch(error => { throw new SystemError(error.message) })
Expand All @@ -18,7 +18,7 @@ export default (userId, productId, minprize, maxprize) => {
.then(product => {
if (!product) throw new NotFoundError('product not found')

return Product.updateOne({ _id: productId }, { $set: { minprize, maxprize } })
return Product.updateOne({ _id: productId }, { $set: { minprice, maxprice } })
.catch(error => { throw new SystemError(error.message) })
})
})
Expand Down

0 comments on commit 154e682

Please sign in to comment.