mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
migrated setting routes to typescript
This commit is contained in:
70
backend/src/services/UserServices/CreateUserService.ts
Normal file
70
backend/src/services/UserServices/CreateUserService.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import * as Yup from "yup";
|
||||
|
||||
import AppError from "../../errors/AppError";
|
||||
import User from "../../models/User";
|
||||
|
||||
interface Request {
|
||||
email: string;
|
||||
password: string;
|
||||
name: string;
|
||||
profile?: string;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
email: string;
|
||||
name: string;
|
||||
id: number;
|
||||
profile: string;
|
||||
}
|
||||
|
||||
const CreateUserService = async ({
|
||||
email,
|
||||
password,
|
||||
name,
|
||||
profile = "admin"
|
||||
}: Request): Promise<Response> => {
|
||||
const schema = Yup.object().shape({
|
||||
name: Yup.string().required().min(2),
|
||||
email: Yup.string()
|
||||
.email()
|
||||
.required()
|
||||
.test(
|
||||
"Check-email",
|
||||
"An user with this email already exists.",
|
||||
async value => {
|
||||
if (value) {
|
||||
const emailExists = await User.findOne({
|
||||
where: { email: value }
|
||||
});
|
||||
return !emailExists;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
),
|
||||
password: Yup.string().required().min(5)
|
||||
});
|
||||
|
||||
try {
|
||||
await schema.validate({ email, password, name });
|
||||
} catch (err) {
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
const user = await User.create({
|
||||
email,
|
||||
password,
|
||||
name,
|
||||
profile
|
||||
});
|
||||
|
||||
const serializedUser = {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
profile: user.profile
|
||||
};
|
||||
|
||||
return serializedUser;
|
||||
};
|
||||
|
||||
export default CreateUserService;
|
||||
Reference in New Issue
Block a user