63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
// db/notification_wa.db.js
|
|
const { default: axios } = require('axios');
|
|
const CryptoJS = require('crypto-js');
|
|
const https = require('https');
|
|
|
|
const httpsAgent = new https.Agent({
|
|
rejectUnauthorized: false,
|
|
});
|
|
|
|
const generateTokenRedirect = async (userPhone, userName, id) => {
|
|
|
|
const plain = {
|
|
user_phone: userPhone,
|
|
user_name: userName,
|
|
id
|
|
}
|
|
|
|
const tokenCrypt = CryptoJS.AES.encrypt(JSON.stringify(plain), process.env.VITE_KEY_SESSION).toString();
|
|
return tokenCrypt
|
|
}
|
|
|
|
const shortUrltiny = async (encodedToken) => {
|
|
const url = `${process.env.ENDPOINT_FE}/redirect?token=${encodedToken}`
|
|
|
|
const encodedUrl = encodeURIComponent(url); // ⬅️ Encode dulu!
|
|
|
|
const response = await axios.get(`https://tinyurl.com/api-create.php?url=${encodedUrl}`,{httpsAgent}) ;
|
|
|
|
let shortUrl = response.data;
|
|
if (!shortUrl.startsWith('http')) {
|
|
shortUrl = 'https://' + shortUrl;
|
|
}
|
|
|
|
return shortUrl
|
|
}
|
|
|
|
const sendNotifikasi = async (phone, message) => {
|
|
const payload = {
|
|
phone: phone,
|
|
message: message
|
|
};
|
|
|
|
// console.log('payload', payload);
|
|
|
|
const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP;
|
|
|
|
try {
|
|
const response = await axios.post(endPointWhatsapp, payload,{httpsAgent} );
|
|
// console.log(response.data);
|
|
return response?.data
|
|
} catch (error) {
|
|
// console.error(error.response?.data || error.message);
|
|
return error.response?.data || error.message
|
|
}
|
|
|
|
};
|
|
|
|
module.exports = {
|
|
generateTokenRedirect,
|
|
shortUrltiny,
|
|
sendNotifikasi,
|
|
};
|