i was making my nodeJs app and i realized that my controller is a little bit messy,so i tried to decompose the logic by creating various function but when i see the result, i have a feeling that something wrong with my code ????????. I mean, is it ok to use async function in another async function, or to use async functions the way i use them. Could anyone make a little code-review, i would be grateful for any help.
import {Request, Response } from "express";
import bcrypt from "bcrypt"
import Role from "../../models/Role.js";
import User from "../../models/User.js";
export default class UserControler {
public async createUser(req: Request, res: Response): Promise{
try {
async function getHashedPassword(password: any): Promise {
const salt: string = await bcrypt.genSalt(10);
const hashedPassword: string = await bcrypt.hash(password, salt);
return hashedPassword
};
async function getDefaultRoleId(): Promise {
const [roleData, createdRole] = await Role.findOrCreate({
where: {
name: 'User',
},
defaults: {
name: 'User'
}
});
const roleId: string = roleData.getDataValue('_id');
return roleId
};
async function emailExists(email: string): Promise {
const user = await User.findOne({
where: {
email: email,
}
});
if(user){
return true
} else {return false;}
};
const _emailExists: boolean = await emailExists(req.body.email)
if(_emailExists) return res.status(400).json({exists: true});
await User.create({
role: await getDefaultRoleId(),
email: req.body.email,
hidden: false,
password: await getHashedPassword(req.body.password)
}).then((data) => {
res.json({data: data});
})
} catch (err: any) {
res.status(400).json({error: err});
}
}
}