import { IUsersRepository } from "../../repositories/IUsersRepository" ; import { ICreateUserRequestDTO } from "./CreateUserDTO" ; import { User } from "../../entities/User" ; import { IMailProvider } from "../../providers/IMailProvider" ; export class CreateUserUseCase { constructor ( private usersRepository , private mailProvider ) { } async execute ( data ) { const userAlreadyExists = await this . usersRepository . findByEmail ( data . email ) ; if ( userAlreadyExists ) { throw new Error ( 'User already exists.' ) ; } const user = new User ( data ) ; await this . usersRepository . save ( user ) ; await this . mailProvider . sendMail ( { to : { name : data . name , email : data . email , } , from : { name : 'Equipe do Meu App' , email : 'equipe@meuapp.com' , } , subject : 'Seja bem-vindo à plataforma' , body : '<p>Você já pode fazer login em nossa plataforma.</p>' } ) ; } }