first commit
This commit is contained in:
195
controllers/controller.js
Normal file
195
controllers/controller.js
Normal file
@ -0,0 +1,195 @@
|
||||
const nodemailer = require('nodemailer');
|
||||
const request = require('request');
|
||||
var baseurlsms="https://mw.telkomcel.tl/tcel/v1/portal/sms/";
|
||||
|
||||
class Controller{
|
||||
constructor(){
|
||||
|
||||
}
|
||||
|
||||
getApiResultDefined(){
|
||||
return {
|
||||
success: false,
|
||||
meta :{
|
||||
code: 200,
|
||||
message: "",
|
||||
|
||||
},
|
||||
// meta:{
|
||||
// table: objectname,
|
||||
// type: typeobject,
|
||||
// total: 0
|
||||
// },
|
||||
data:[]
|
||||
};
|
||||
}
|
||||
|
||||
getPagination(datalength, limit){
|
||||
let pagination = datalength / limit;
|
||||
if(!Number.isInteger(pagination)){
|
||||
pagination=(Math.floor(datalength / limit))+1;
|
||||
}
|
||||
return pagination;
|
||||
}
|
||||
|
||||
getErrorDefined(errcode, msg){
|
||||
if(errcode=='404'){
|
||||
return {
|
||||
code : 404,
|
||||
message : msg!='' ? msg : "Record Not Found"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static getResultJson(results){
|
||||
let resultJson = JSON.stringify(results);
|
||||
resultJson = JSON.parse(resultJson);
|
||||
return resultJson;
|
||||
}
|
||||
|
||||
sendResponse(statusCode, data, res){
|
||||
// res.append("responseData", JSON.stringify(data));
|
||||
// res.status(statusCode).json(data);
|
||||
res.status(statusCode).json(data);
|
||||
}
|
||||
|
||||
static decodeBase64Image(dataString) {
|
||||
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
|
||||
response = {};
|
||||
|
||||
if (matches.length !== 3) {
|
||||
return new Error('Invalid input string');
|
||||
}
|
||||
|
||||
response.type = matches[1];
|
||||
response.data = new Buffer.from(matches[2], 'base64');
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
getToken(){
|
||||
try {
|
||||
var n1=Math.floor(Math.random() * 9) + 0;
|
||||
var n2=Math.floor(Math.random() * 9) + 0;
|
||||
var n3=Math.floor(Math.random() * 9) + 0;
|
||||
var n4=Math.floor(Math.random() * 9) + 0;
|
||||
var n5=Math.floor(Math.random() * 9) + 0;
|
||||
var n6=Math.floor(Math.random() * 9) + 0;
|
||||
var otp=n1.toString()+n2.toString()+n3.toString()+n4.toString()+n5.toString()+n6.toString();
|
||||
// console.log(otp);
|
||||
return otp;
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
static sentMail(emailto,subject,msg,callback){
|
||||
console.log(process.env.EMAIL_KEY);
|
||||
var transporter = nodemailer.createTransport({
|
||||
service: 'gmail',
|
||||
auth: {
|
||||
user: 'mitrasejatipangan2021@gmail.com',
|
||||
pass: process.env.EMAIL_KEY
|
||||
}
|
||||
});
|
||||
|
||||
var mailOptions = {
|
||||
from: 'MSP <mitrasejatipangan2021@gmail.com>',
|
||||
to: emailto,
|
||||
subject: subject,
|
||||
text: msg
|
||||
};
|
||||
|
||||
transporter.sendMail(mailOptions, function(error, info){
|
||||
console.log(error);
|
||||
if (error) {
|
||||
console.log(error);
|
||||
callback(error,null);
|
||||
} else {
|
||||
callback(null,info);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static sendSMS(header,msisdn,msg,callback){
|
||||
try {
|
||||
var msisdnnew = msisdn.length==8 ? '670'+msisdn : msisdn;
|
||||
let resuri={
|
||||
'method': 'GET',
|
||||
// 'url': baseurl+'signon',
|
||||
'url': baseurlsms+'?msisdn='+msisdnnew+'&content='+msg+'&header='+header,
|
||||
'headers': {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
// form: formjson,
|
||||
};
|
||||
// console.log(resuri);
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
||||
request(resuri, function (error, response) {
|
||||
if (error) {
|
||||
// console.log(error);
|
||||
callback(error,null);
|
||||
}
|
||||
else
|
||||
{
|
||||
// console.log(response);
|
||||
callback(null,response);
|
||||
}
|
||||
|
||||
});
|
||||
} catch (error) {
|
||||
callback(error,null);
|
||||
}
|
||||
}
|
||||
|
||||
static sendSMSold(header,msisdn,msg,callback)
|
||||
{
|
||||
|
||||
request.post(
|
||||
'http://150.242.111.251:81/tlstream/index/api',
|
||||
{
|
||||
json: {
|
||||
views:'vwsentsms',
|
||||
task:'sentmessage',
|
||||
header: header,
|
||||
msisdn: msisdn,
|
||||
msg : msg
|
||||
}
|
||||
},
|
||||
(error, res, body) => {
|
||||
if (error) {
|
||||
console.error(error)
|
||||
callback(null,error);
|
||||
return
|
||||
}
|
||||
// console.log(`statusCode: ${res.statusCode}`)
|
||||
// console.log(body);
|
||||
callback(null,body);
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
static getYmD(){
|
||||
var dates= new Date();
|
||||
var today=dates.getFullYear()+"-"+dates.getMonth()+"-"+dates.getDate()+" "+dates.getHours()+":"+dates.getMinutes()+":"+dates.getSeconds();
|
||||
//console.log(today);
|
||||
return today;
|
||||
}
|
||||
|
||||
static getmonth(){
|
||||
var dates= new Date();
|
||||
//console.log(dates.getMonth());
|
||||
return dates.getMonth()+1;
|
||||
}
|
||||
static getyears(){
|
||||
var dates= new Date();
|
||||
return dates.getFullYear();
|
||||
}
|
||||
static getday(){
|
||||
var dates= new Date();
|
||||
return dates.getDate();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Controller;
|
||||
Reference in New Issue
Block a user