const Joi = require("joi");
const env = process.env;
class Email{
constructor(from,subject,message=null,html=null,attachments=null){
this.from=`iMali Moçambique <${from}>`
this.replayTo="imali.noreplay@paytek-africa.com";
this.subject=subject;
this.text=message;
this.html=html;
this.attachments=attachments;
}
static validate(body){
return Joi.object({
messageType:Joi.string().required().valid("text","html"),
from:Joi.string().required(),
to:Joi.array().required(),
subject:Joi.string().required().max(72),
attachments:Joi.array(),
html:Joi.alternatives().conditional('messageType',{is:"html",then:Joi.string().base64().required(),otherwise: Joi.forbidden()}),
message:Joi.alternatives().conditional('messageType',{is:"text",then:Joi.string().required(),otherwise: Joi.forbidden()})
}).validate(body);
}
}
class MailConf{
constructor(){
console.log(env.MAIL_SECURE_PORT)
this.host=env.MAIL_HOST;
this.port=env.MAIL_PORT;
this.service=env.MAIL_SERVICE;
this.auth=new MailUser(env.MAIL_USERNAME,env.MAIL_PASSWORD);
this.secure=env.MAIL_SECURE_PORT === "true"; // True só quando for a porta 465
this.tls=new BypassCert(env.MAIL_USE_TLS === "true"); //Passa mesmo com certificados invalidos
// this.secure=true; // True só quando for a porta 465
// this.tls=new BypassCert(true); //Passa mesmo com certificados invalidos
}
}
class MailUser{
constructor(user,pass){
this.user=user;
this.pass=pass;
}
}
class BypassCert{
constructor(rejectUnauthorized){
this.rejectUnauthorized=rejectUnauthorized;
}
}
module.exports={Email,MailConf};