Skip to content

Commit

Permalink
Merge branch 'develop' into feature/frontend/479-mostrar-aviso-credit…
Browse files Browse the repository at this point in the history
…os-representante
  • Loading branch information
alemerpal committed May 17, 2024
2 parents fed58d8 + 779c111 commit c411299
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
37 changes: 21 additions & 16 deletions api/v1/modules/user/middlewares/UserMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ export const checkRealUser: any = async (req: Request, res: Response, next: Next
const data = req.body
// Comprobar si el usuario existe
const userByEmail = await User.findOne({ email: data.usernameOrEmail })
const userByUsername=await User.findOne({ username: data.usernameOrEmail })
const userByUsername = await User.findOne({ username: data.usernameOrEmail })
if (!userByEmail && !userByUsername) {
const message = 'User not found'
ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404)
}else {
} else {
next()
}
} catch (error: any) {
Expand All @@ -211,19 +211,18 @@ export const checkCorrectToken: any = async (req: Request, res: Response, next:
return
}
const decodedToken = verifyJWT(token)
if(!decodedToken.sub){
if (!decodedToken.sub) {
const message = 'Incorrect token'
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 401)
return
}
const user= await User.findById(decodedToken.sub)
if(!user){
const user = await User.findById(decodedToken.sub)
if (!user) {
const message = 'User not found'
ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404)
}else {
} else {
next()
}

} catch (error: any) {
ApiResponse.sendError(res, [
{
Expand All @@ -234,21 +233,24 @@ export const checkCorrectToken: any = async (req: Request, res: Response, next:
}
}

export const checkRepeatedPassword: any = async (req: Request, res: Response, next: NextFunction) => {
export const checkRepeatedPassword: any = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const {newPassword,repeatedPassword} = req.body
const compare=newPassword === repeatedPassword
if(newPassword.lenght ===0 || repeatedPassword.lenght===0){
const { newPassword, repeatedPassword } = req.body
const compare = newPassword === repeatedPassword
if (newPassword.lenght === 0 || repeatedPassword.lenght === 0) {
const message = 'There are empty fields'
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 400)
}else if(!(compare)){
} else if (!compare) {
const message = 'The passwords dont match'
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 400)
}else {
req.body.encryptedPassword=await encrypt(newPassword)
} else {
req.body.encryptedPassword = await encrypt(newPassword)
next()
}

} catch (error: any) {
ApiResponse.sendError(res, [
{
Expand All @@ -273,7 +275,10 @@ export const checkLoginUser: any = async (req: Request, res: Response, next: Nex
return
}
// Comprobar si el usuario existe
const user = await User.findOne({ username: data.username })
const userByEmail = await User.findOne({ email: data.username })
const userByUsername = await User.findOne({ username: data.username })
const user = userByEmail ?? userByUsername

if (!user) {
const message = 'User not found'
ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404)
Expand Down
45 changes: 24 additions & 21 deletions api/v1/modules/user/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,45 +100,44 @@ export const deleteUser: any = async (id: any, role: string) => {
}
}

export const sendEmail: any = async (to: string, subject: string, text: string,html: string) =>{
try{
const from= process.env.SENGRID_EMAIL ?? ''
export const sendEmail: any = async (to: string, subject: string, text: string, html: string) => {
try {
const from = process.env.SENGRID_EMAIL ?? ''
const msg = {
to,
from,
subject,
text,
html,
};
}
await sgMail
.send(msg)
.then(() => {
console.log('Email sent')
})
.catch((error:any) => {
console.error(error)
})
}catch(error:any){
.send(msg)
.then(() => {
console.log('Email sent')
})
.catch((error: any) => {
console.error(error)
})
} catch (error: any) {
console.error('Error creating your request:', error)
throw error
}
}

export const createChangePasswordRequest: any = async (data: any,originalUrl: string) => {
export const createChangePasswordRequest: any = async (data: any, originalUrl: string) => {
try {
const userByEmail = await User.findOne({ email: data.usernameOrEmail })
const userByUsername=await User.findOne({ username: data.usernameOrEmail })
const userByUsername = await User.findOne({ username: data.usernameOrEmail })
const user = userByEmail ?? userByUsername
const id = user?._id.toString()
const token = generateJWTWithSoonerExpiration(id)
const result = originalUrl+"/"+token
const text=`To change the forgotten password, access this link: ${result}. \n
const result = originalUrl + '/' + token
const text = `The user with the username: ${user?.username} and email: ${user?.email} has requested to change the password.
To change the forgotten password, access this link: ${result}. \n
\n
In case of error, simply ignore the message.
Thank you very much for using IT TALENT :3`
await sendEmail(user?.email,'Verify password change',
text,`<strong> ${text} </strong>`
)
await sendEmail(user?.email, 'Verify password change', text, `<strong> ${text} </strong>`)
} catch (error) {
console.error('Error creating your request:', error)
throw error
Expand All @@ -147,7 +146,10 @@ export const createChangePasswordRequest: any = async (data: any,originalUrl: st

export const loginUser: any = async (data: any) => {
try {
const user = await User.findOne({ username: data.username })
const userByEmail = await User.findOne({ email: data.username })
const userByUsername = await User.findOne({ username: data.username })
const user = userByEmail ?? userByUsername

const id = user?._id.toString()
const token = generateJWT(id)
const result = { token, user }
Expand All @@ -166,5 +168,6 @@ export default {
updateUserProfilePicture,
updateUserPassword,
deleteUser,
loginUser,createChangePasswordRequest
loginUser,
createChangePasswordRequest,
}

0 comments on commit c411299

Please sign in to comment.