const Joi = require("joi");
const env = process.env;
class SMS {
constructor(message) {
this.From = 'i.Mali';
this.RegisteredDelivery = true;
this.Content = message;
this.ClientReference = this.transactionID(),
this.Time = this.getFormattedDate();
}
static validate(body){
return Joi.object({
phoneNumbers:Joi.array().required().max(15),
message:Joi.string().required().min(3),
}).validate(body);
}
getFormattedDate() {
const date=new Date();
let hour=date.getHours();
let minutes=date.getMinutes();
let seconds=date.getSeconds();
let day = date.getDate();
let month = date.getMonth() + 1;
const year = date.getFullYear();
//acrescenta 0 se o dia e o mês forem menor que 0
if(day<10) day='0'+day;
if(month<10) month='0'+month;
if(seconds<10) seconds='0'+seconds;
if(minutes<10) minutes='0'+minutes;
if(hour<10) hour='0'+hour;
const databaseTime = `${year}-${month}-${day} ${hour}:${minutes}:${seconds}`;
return databaseTime;
}
transactionID()
{
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let randomString = '';
for (let i = 0; i < 12; i++) {
randomString += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return randomString;
}
}
class SMSConf{
constructor(){
this.responseType='json';
this.headers = {
'Authorization': `Basic ${env.SMS_AUTHORIZATION_TOKEN}`,
'Content-Type': 'application/json',
'Accept':'application/json'
}
}
}
module.exports={SMS,SMSConf};