temu-labels-crawler/send-shipping-rates.js

58 lines
1.7 KiB
JavaScript

const axios = require("axios");
const fs = require("fs");
const path = require("path");
const dotenv = require("dotenv").config({ path: __dirname + "/.env" });
const { exit } = require("process");
(async function () {
/**
* load config data
*/
const config = JSON.parse(fs.readFileSync(__dirname + "/config.json"));
const environment = process.env["ENVIRONMENT"];
/**
* directory path
*/
let shippingRatesPath = config[environment].temu_orders_shipping_rates;
let unProcessedPath = shippingRatesPath + "/unprocessed";
let processedPath = shippingRatesPath + "/processed";
if (!fs.existsSync(processedPath)) {
fs.mkdirSync(processedPath);
}
/**
* read all files in directory, send data to cosmos then move to processed
*/
const jsonFiles = fs
.readdirSync(unProcessedPath)
.filter((file) => path.extname(file).toLocaleLowerCase() === ".json");
if( jsonFiles.length === 0 ){
console.log( `No Files Present at ${unProcessedPath}`)
}
for (const file of jsonFiles) {
try {
const filePath = path.join(unProcessedPath, file);
const orders = JSON.parse(fs.readFileSync(filePath, "utf-8"));
console.log(`Processing: ${filePath}`);
// send post request to cosmos
const axiosConfig = {
method: "get",
url: config[environment].cosmos_temu_order_shipping_rates,
headers: {
"Content-Type": "application/json",
},
data: orders, // Add the orders object to the data field
};
const res = await axios(axiosConfig);
if (res["status"] == 200) {
fs.renameSync(filePath, path.join(processedPath, file));
}
} catch (e) {
console.log(e);
}
}
})();