Skip to content

Commit

Permalink
create logic updates of email, password, user address, user phone and…
Browse files Browse the repository at this point in the history
…e of create product in app/logic b00tc4mp#101
  • Loading branch information
LucasOrtsBaguena committed Aug 26, 2024
1 parent a52d561 commit 192023e
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 6 deletions.
34 changes: 34 additions & 0 deletions staff/lucas-orts/project/app/logic/createProduct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { validate, errors } from 'com'

const { SystemError } = errors

export default (name, type, minprice, maxprice, image) => {
validate.string(name, 'name')
validate.string(type, 'type')
validate.number(minprice, 'minprice')
validate.number(maxprice, 'maxprice')
validate.url(image, 'image')
return fetch(`${import.meta.env.VITE_API_URL}/products`, {
method: 'POST',
headers: {
Authorization: `Bearer ${sessionStorage.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ name, type, minprice, maxprice, image })
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
const { status } = response

if (status === 201) return

return response.json()
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
32 changes: 32 additions & 0 deletions staff/lucas-orts/project/app/logic/updateEmail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { validate, errors } from '../../com/index.js'

const { SystemError } = errors

export default (email, password) => {
validate.email(email)
validate.password(password)

return fetch(`${import.meta.env.VITE_API_URL}/users/email`, {
method: 'PATCH',
headers: {
Authorization: `Bearer ${sessionStorage.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ email, password })
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
const { status } = response

if (status === 204) return

return response.json()
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
33 changes: 33 additions & 0 deletions staff/lucas-orts/project/app/logic/updatePassword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { validate, errors } from '../../com/index.js'

const { SystemError } = errors

export default (oldPassword, newPassword, newPasswordRepeat) => {
validate.password(oldPassword, 'oldPassword')
validate.password(newPassword, 'newPassword')
validate.password(newPasswordRepeat, 'newPasswordRepeat')

return fetch(`${import.meta.env.VITE_API_URL}/users/password`, {
method: 'PATCH',
headers: {
Authorization: `Bearer ${sessionStorage.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ oldPassword, newPassword, newPasswordRepeat })
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
const { status } = response

if (status === 204) return

return response.json()
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
31 changes: 31 additions & 0 deletions staff/lucas-orts/project/app/logic/updateUserAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { validate, errors } from '../../com/index.js'

const { SystemError } = errors

export default (address) => {
validate.address(address)

return fetch(`${import.meta.env.VITE_API_URL}/users/address`, {
method: 'PATCH',
headers: {
Authorization: `Bearer ${sessionStorage.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ address })
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
const { status } = response

if (status === 204) return

return response.json()
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
31 changes: 31 additions & 0 deletions staff/lucas-orts/project/app/logic/updateUserPhone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { validate, errors } from '../../com/index.js'

const { SystemError } = errors

export default (phone) => {
validate.phone(phone)

return fetch(`${import.meta.env.VITE_API_URL}/users/phone`, {
method: 'PATCH',
headers: {
Authorization: `Bearer ${sessionStorage.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ phone })
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
const { status } = response

if (status === 204) return

return response.json()
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
8 changes: 4 additions & 4 deletions staff/lucas-orts/project/com/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ function validateString(value, explain = 'value') {
if (typeof value !== 'string') throw new ValidationError(`${explain} is not a string`)
}

function validatePassword(password) {
validateString(password, 'password')
if (password.trim().length < 8) throw new ValidationError('password length is lower than 8 characters')
if (password.includes(' ')) throw new ValidationError('password has empty spaces')
function validatePassword(password, explain = 'password') {
validateString(password, explain)
if (password.trim().length < 8) throw new ValidationError(`${explain} length is lower than 8 characters`)
if (password.includes(' ')) throw new ValidationError(`${explain} has empty spaces`)
}

function validateName(name, explain = 'name') {
Expand Down
1 change: 1 addition & 0 deletions staff/lucas-orts/project/cor/logic/registerUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default (name, surname, email, phone, address, password, passwordRepeat)
validate.phone(phone)
validate.address(address)
validate.password(password)
validate.password(passwordRepeat, 'passwordRepeat')

if (password !== passwordRepeat) throw new ValidationError('passwords do not match')

Expand Down
5 changes: 3 additions & 2 deletions staff/lucas-orts/project/cor/logic/updatePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const { NotFoundError, CredentialsError, SystemError, ValidationError } = errors

export default (userId, oldPassword, newPassword, newPasswordRepeat) => {
validate.string(userId, 'userId')
validate.password(oldPassword)
validate.password(newPassword)
validate.password(oldPassword, 'oldPassword')
validate.password(newPassword, 'newPassword')
validate.password(newPasswordRepeat, 'newPasswordRepeat')

if (newPassword !== newPasswordRepeat) throw new ValidationError('passwords do not match')

Expand Down

0 comments on commit 192023e

Please sign in to comment.