Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const axios = require('axios');
- const mysql = require('mysql2');
- var dateFormat = require('dateformat');
- const emailid = [];
- function foreach(arr, func)
- {
- for (var i in arr)
- {
- func(i, arr[i]);
- }
- }
- const connection = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- database: 'v'
- });
- //tb_email_reminder_2102
- // checking tahun dan bulan yymm
- let cektahunbulan =dateFormat(new Date(), "yymm");
- // function cektable
- function cektable(){
- return new Promise(resolve => {
- const query = "SHOW TABLES;";
- const pool = connection.query(query, async function(err, result ) {
- if (err) {
- console.log(err)
- }
- else{
- resolve(result);
- }
- })
- })
- }
- function buat_table(namatable){
- return new Promise(resolve => {
- const query = "CREATE TABLE `tb_email_reminder_"+ cektahunbulan +"` ( `"+ cektahunbulan +"` int(11) NOT NULL, `email_id` varchar(100) NOT NULL, `body_message` varchar(4000) NOT NULL, `tgl_proses` varchar(25) DEFAULT NULL, `cycle` varchar(25) DEFAULT NULL, `jenis_surat_email` varchar(100) DEFAULT NULL, `fin_account` varchar(100) DEFAULT NULL, `cr_limit` varchar(100) DEFAULT NULL, `aging` varchar(100) DEFAULT NULL, `min_pembayaran` varchar(100) DEFAULT NULL, `total_tagihan` varchar(100) DEFAULT NULL, `jatuh_tempo` varchar(100) DEFAULT NULL, `kanwil` varchar(100) DEFAULT NULL, `out_standing` varchar(100) DEFAULT NULL, `card_no` varchar(100) DEFAULT NULL, `email` varchar(500) DEFAULT NULL, `flag_ever` varchar(100) DEFAULT NULL, `date_submit` varchar(50) DEFAULT NULL, `f_bucket` varchar(50) DEFAULT NULL, `f_ratio` varchar(50) DEFAULT NULL, `flag_dd` varchar(11) DEFAULT NULL, `priority` varchar(20) DEFAULT NULL, `status` varchar(11) DEFAULT NULL, `flag_err` int(11) DEFAULT NULL, `created_date` datetime DEFAULT '0000-00-00 00:00:00', `modified_date` datetime DEFAULT '0000-00-00 00:00:00', `status_read` varchar(11) DEFAULT NULL, `read_date` datetime DEFAULT '0000-00-00 00:00:00', `stmt_clsba` varchar(100) DEFAULT NULL, `date_payment` varchar(50) DEFAULT NULL, `age_report` varchar(11) DEFAULT NULL, `hp` varchar(25) DEFAULT NULL, `pic` varchar(500) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;"
- const pool = connection.query(query, async function(err, result ) {
- if (err) {
- console.log(err)
- }
- else{
- resolve(result);
- }
- })
- })
- }
- buat_table()
- function execute(){
- cektable().then(data => {
- if (data.indexOf(cektahunbulan)){
- }else{
- console.log("ada")
- process.exit(1);
- }
- });
- var url = "https://msapi.bankmega.com/email_gtw/email_gateway.php?token=3a7fd845c56b168fea4e67175b024bb9&request_type=get_email";
- let day =dateFormat(new Date(), "dd-mm-yyyy");
- let initanggal = day.replace(/\D/g,'');
- axios.get(url)
- .then(function(res){
- const emailid = res.data.data[0].email_id;
- let jenis_surat;
- const array = [];
- foreach(res.data.data ,function(i , v){
- const emailid = res.data.data[i].email_id;
- const email = res.data.data[i].email_address;
- const body = res.data.data[i].body_message;
- // console.log(res.data.data[i].email_id)
- const nomorkartu = res.data.data[i].body_message.split("dengan nomor ")[1].split("dengan jumlah")[0];
- const jatuhtempo = res.data.data[i].body_message.split(" pada tanggal ")[1].split(". Anda bisa melakukan")[0];
- const jumlah_tagihan = res.data.data[i].body_message.split("jumlah tagihan ")[1].split(" akan jatuh ")[0];
- const minimum_pembayaran = res.data.data[i].body_message.split("minimum sebesar ")[1].split(". Abaikan pesan")[0];
- const hitungtempo = jatuhtempo.replace(/\D/g,'');
- if (hitungtempo > initanggal){
- jenis_surat += "AFTER DUE DATE";
- }else{
- jenis_surat += "BEFORE DUE DATE";
- }
- array.push([emailid, email, body , nomorkartu, jatuhtempo , jumlah_tagihan , minimum_pembayaran])
- // console.log(array)
- })
- const query = "INSERT INTO tb_trans_email_reminder_2102(tb_email_reminder_2102_id, email,status,message) VALUES ?";
- const pool = connection.query(query, [array], function(err, result) {
- if (err) {
- console.log(err)
- }else{
- console.log('result', result)
- }
- })
- console.log(array)
- })
- }
- execute()
- // console.log(results)
- // console.log(err)
- // })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement