fix excel download issue

main
saif 2025-02-11 12:43:55 +05:00
parent e5a2260e4d
commit 175856217f
1 changed files with 35 additions and 20 deletions

View File

@ -221,20 +221,32 @@ const utils = require("./utils");
); );
// Function to read Excel files and convert to JSON // Function to read Excel files and convert to JSON
const convertExcelToJson = async () => { const convertExcelToJson = () => {
try { fs.readdir(unprocessedFolderPath, (err, files) => {
const files = await fs.readdir(unprocessedFolderPath); if (err) {
console.error("Error reading the directory:", err);
return;
}
// Filter Excel files (xlsx or xls)
const excelFiles = files.filter( const excelFiles = files.filter(
(file) => file.endsWith(".xlsx") || file.endsWith(".xls") || file.endsWith(".csv") (file) =>
file.endsWith(".xlsx") ||
file.endsWith(".xls") ||
file.endsWith(".csv")
); );
for (const file of excelFiles) { // Process each Excel file
excelFiles.forEach((file) => {
const filePath = path.join(unprocessedFolderPath, file); const filePath = path.join(unprocessedFolderPath, file);
const workbook = xlsx.readFile(filePath); const workbook = xlsx.readFile(filePath);
const sheetName = workbook.SheetNames[0]; const sheetName = workbook.SheetNames[0]; // Use the first sheet
const worksheet = workbook.Sheets[sheetName]; const worksheet = workbook.Sheets[sheetName];
// Convert the sheet to JSON
let jsonData = xlsx.utils.sheet_to_json(worksheet); let jsonData = xlsx.utils.sheet_to_json(worksheet);
// Modify the column names by adding underscores
jsonData = jsonData.map((row) => { jsonData = jsonData.map((row) => {
const modifiedRow = {}; const modifiedRow = {};
Object.keys(row).forEach((key) => { Object.keys(row).forEach((key) => {
@ -248,24 +260,27 @@ const utils = require("./utils");
return modifiedRow; return modifiedRow;
}); });
// write the JSON data to a file
const outputFile = path.join( const outputFile = path.join(
config.environment.temu_orders_path, config[environment].temu_orders_path,
"data/unprocessed", "data/unprocessed",
`${luxon.DateTime.now().toMillis()}.json` `${luxon.DateTime.now().toMillis()}.json`
); );
await fs.writeFile(outputFile, JSON.stringify(jsonData, null, 2)); fs.writeFileSync(outputFile, JSON.stringify(jsonData, null, 2));
console.log(`Saved JSON to ${outputFile}`); console.log(`Saved JSON to ${outputFile}`);
const processedPath = path.join(config.environment.temu_orders_path, "processed", file); console.log(
await fs.rename(filePath, processedPath); `Move Excel file ${filePath} to ${config[environment].temu_orders_path}/processed`
console.log(`Moved Excel file ${filePath} to ${processedPath}`); );
} fs.renameSync(
} catch (err) { filePath,
console.error("Error processing Excel files:", err); path.join(`${config[environment].temu_orders_path}/processed`, file)
} );
});
});
}; };
await convertExcelToJson(); convertExcelToJson();
console.log(`==========< ENDED ${luxon.DateTime.now()} >==========`); console.log(`==========< ENDED ${luxon.DateTime.now()} >==========`);
await page.close(); await page.close();
await browser.close(); await browser.close();