payments_dashboard_pipedream/payment-display-p_WxCpgo3/parse/entry.js
2026-03-31 23:46:11 +00:00

37 lines
No EOL
984 B
JavaScript

import fs from "fs";
import { parse } from "csv-parse/sync";
export default defineComponent({
async run({ steps }) {
const filePath = `/tmp/${steps.trigger.event.name}`;
if (!fs.existsSync(filePath)) {
throw new Error(`File not found at ${filePath}`);
}
const csvText = fs.readFileSync(filePath, "utf8");
const rows = parse(csvText, {
columns: true,
skip_empty_lines: true,
trim: true,
});
const normalizedRows = rows.map((row) => {
const clean = {};
for (const [key, value] of Object.entries(row)) {
clean[String(key).trim()] = typeof value === "string" ? value.trim() : value;
}
return clean;
});
return {
file_path: filePath,
file_name: steps.trigger.event.name,
row_count: normalizedRows.length,
headers: normalizedRows.length ? Object.keys(normalizedRows[0]) : [],
sample_rows: normalizedRows.slice(0, 5),
rows: normalizedRows,
};
},
});