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