Untitled

 avatar
user_5445601
plain_text
a year ago
3.9 kB
4
Indexable
Never
'use strict';
const express = require("express");
const app = express();
const axios = require("axios");
let tradenetkey = "ILTELB-8Z/O1A-85ZG24-P1BKZ0";
let password = "";
let loginAs = "C";
let product = "T"
let url = `http://172.16.100.240:5009/api/Main/Login_validate_Password?userId=GE0008&password=${password}&key=${tradenetkey}&loginAs=${loginAs}&product=${product}`
app.get("/", async (req, res) => {
    let formdata = ""
    const options = {
        method: "POST",
        url: url,
        data: formdata,
    };
    let { token } = await axios
        .request(options)
        .then(async function (response) {
            return response.data;
        })
        .catch(async function (error) {

            return error;
        });
    let url1 = `http://172.16.100.240:5009/api/Miscellanious/DigitalDocument_List?product=0&fromDate=20230606&toDate=20230606&docType=CNOTE`;
    let url2 = `http://172.16.100.240:5009/api/Miscellanious/DigitalDocument_File?Product=0&date=20230106&srNo=10307`
    let abs = await getcontractnote(url1, token);
    // console.log(abs)
    let seriesName = abs[0].DocSrNo;
    let contractnote = await getcontractnote2(url2, token);
    let pdf= contractnote.data.toString('base64');
    // res.setHeader('Content-Type', 'application/pdf');
    // res.setHeader('Content-Disposition', 'attachment; filename="downloaded.pdf"');
    res.end(pdf);
})



app.listen(8000, () => {
    console.log("Running on port ", 8000)
})


async function main(buffer) {
    try {
        const directory = await unzipper.Open.buffer(buffer);
        console.log('directory', directory);
    }
    catch (e) {
        console.log("EROR", e)
    }
}

function getcontractnote(url, token) {
    return axios.get(url, {
        headers: {
            accept: "*/*",
            Authorization: `Bearer ${token}`,
            "Content-Type": "application/json",
        },
        // responseType: 'stream',
    }).then((res) => {
        return res.data.data
    })
}
function getcontractnote2(url, token) {
    return axios.get(url, {
        headers: {
            // 'Accept-Encoding': 'gzip', // Request gzip-encoded response
            Authorization: `Bearer ${token}`,
            "Content-Type": "application/json",
        },
        // responseType: 'arraybuffer',
    }).then((response) => {
        // console.log(response)
        // if (response.headers['content-encoding'] === 'gzip') {
        //     zlib.gunzip(response.data, (err, buffer) => {
        //       if (!err) {
        //         const unzippedResponse = buffer.toString('utf8');
        //         // console.log(unzippedResponse);
        //         console.log("HHIhgbderrjk")
        //       } else {
        //         console.error('Error unzipping the response:', err);
        //       }
        //     });
        //   } else {
        //     // If it's not gzipped, you can handle it directly
        //     // console.log(response.data.toString('utf8'));
        //     console.log("HHI")
        //   }
        return response.data
    })
}


// exports.post_data = async (url, formdata) => {
//     // console.log(formdata);
//     const get_token = await TokenSchema.find();
//     const token = get_token[0]["token"];
//     const options = {
//         method: "POST",
//         url: process.env.tradenet + url,
//         headers: {
//             accept: "*/*",
//             Authorization: `Bearer ${token}`,
//             "Content-Type": "application/json",
//         },
//         data: formdata,
//     };
//     return axios
//     .request(options)
//     .then(async function (response) {
//         return response.data;
//     })
//     .catch(async function (error) {
//         console.log(error)
//         return error;
//     });
// };