-
-
Save suissa/5aeb342a9eea439175583f27a9e09aaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nodemailer from 'nodemailer'; | |
const transporter = nodemailer.createTransport({ | |
host: 'smtp.gmail.com', | |
port: 465, | |
secure: true, // use SSL | |
auth: { | |
user: "[email protected]", | |
pass: "senha" | |
} | |
}); | |
function createEmailBody(idUser) { | |
const path = `https://promorverse-api.herokuapp.com/active-user/${idUser}`; | |
let body = "<p> deseja ativar sua conta?" | |
body = body+`<a href='${path}'>`; | |
body = body+"Ativar Conta</a>"; | |
return body; | |
} | |
function createNewAccount(object) { | |
const account = {}; | |
account.password = object.password; | |
account.role = object.role; | |
account.username = object.username; | |
account.email = object.email; | |
return account; | |
}; | |
// faça igual essa e a de baixo nas outras parecidas. | |
function updateAccount(object) { | |
return { | |
_id: object._id, | |
role: object.role, | |
email: object.email | |
} | |
} | |
function setDetalhes(object) { | |
return { | |
birthDate: object.birthDate, | |
sex: object.sex, | |
eyeColor: object.eyeColor, | |
height: object.height, | |
weight: object.weight, | |
skinColor: object.skinColor, | |
} | |
} | |
function setAddress(object) { | |
return { | |
cep: object.cep, | |
publicPlace: object.publicPlace, | |
complement: object.complement, | |
neighborhood: object.neighborhood, | |
city: object.city, | |
state: object.state, | |
number: object.number, | |
} | |
} | |
function createNewUser(object) { | |
let user = {}; | |
// passe pra um switch e encapsule cada uma em uam fun~çao | |
if (object.role === 'type1') { | |
user.detalhes = {}; | |
user.address = {}; | |
user.jobs = []; | |
if (object.jobs) { | |
user.jobs = object.jobs.filter(el => !el.removed); | |
}; | |
user.name = object.name; | |
user.cpf = object.cpf; | |
user.detalhes = setDetalhes(object); | |
user.address = setAddress(object); | |
} | |
if (object.role === 'type2') { | |
user.address = {}; | |
user.socialName = object.socialName; | |
user.fantasyName = object.fantasyName; | |
user.cnpj = object.cnpj; | |
user.address = setAddress(object); | |
} | |
if (object.role === 'admin') { | |
user.name = object.name; | |
user.age = object.age; | |
} | |
return user; | |
}; | |
function updateUser(object) { | |
let user = {}; | |
if (object.role === 'type1') { | |
user.detalhes = {}; | |
user.address = {}; | |
user.socialName = object._company.socialName; | |
user.fantasyName = object._company.fantasyName; | |
user.cnpj = object._company.cnpj; | |
user._id = object._company._id; | |
user.address = setAddress(object._company.address); | |
}; | |
if (object.role === 'type2') { | |
user.detalhes = {}; | |
user.address = {}; | |
user.jobs = []; | |
if (object._customer.jobs) { | |
user.jobs = object._customer.jobs | |
} | |
user.name = object._customer.name; | |
user.cpf = object._customer.cpf; | |
user._id = object._customer._id; | |
user.detalhes = setDetalhes(object._customer.detalhes); | |
user.address = setAddress(object._customer.address); | |
}; | |
return user; | |
}; | |
function executeRollBack(entity, id, error) { | |
return entity.remove({ _id: id }) | |
.then(error) | |
.catch(error) | |
}; | |
function executeFindAndUpdate(firstModel, secondModel, account, user, type, sucess, error) { | |
return firstModel.findOneAndUpdate({ _id: user._id }, user) | |
.then(inseredUser => { | |
account[type] = inseredUser._id; | |
secondModel.findOneAndUpdate({ _id: account._id }, account) | |
.then(sucess) | |
.catch(error) | |
}) | |
.catch(error) | |
.done() | |
}; | |
function executeCreate(firstModel, secondModel, account, user, type, success, error) { | |
return firstModel.create(user) | |
.then(inseredUser => { | |
// encapsule os thens e catchs | |
account[type] = inseredUser._id; | |
secondModel.create(account) | |
.then(inseredAccount => { | |
const mailOptions = { | |
from: '[email protected]', | |
to: account.email, | |
subject: 'Texto assunto', | |
text: 'Texto', | |
html:createEmailBody(inseredAccount._id) | |
}; | |
transporter.sendMail(mailOptions, function (error, info) { | |
if (error) { | |
console.log('deu erro:',error); | |
} else { | |
console.log('Email enviado: ' + info.response); | |
} | |
}); | |
}) | |
.then(() => { | |
if(inseredUser._id) { | |
success(inseredUser); | |
} | |
}) | |
.catch(err => { | |
if (err) { | |
executeRollBack(firstModel, inseredUser._id, error(err)); | |
} | |
}) | |
}) | |
} | |
const factory = { | |
createNewAccount, | |
createNewUser, | |
updateAccount, | |
updateUser, | |
executeFindAndUpdate, | |
executeCreate | |
} | |
export default factory; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment