improvements to the code

This commit is contained in:
2026-07-27 17:16:11 +02:00
parent 88c77e61f7
commit aaacec71f4
+29 -4
View File
@@ -45,6 +45,11 @@ async function initializeSalesforceConnection() {
console.log('Salesforce connection is ready.'); console.log('Salesforce connection is ready.');
} }
// Escape single quotes for SOQL queries (e.g., D'Angelo -> D\'Angelo)
function escapeSoql(str) {
return str ? str.replace(/'/g, "\\'") : '';
}
// the pdf files contain the identifying ids of the users such as 'Codice Fiscale' (CF) and we use them to attach // the pdf files contain the identifying ids of the users such as 'Codice Fiscale' (CF) and we use them to attach
// the page/pages to the specific account that has that id. If CFs are not available we search based on the // the page/pages to the specific account that has that id. If CFs are not available we search based on the
// name an lastname of the user // name an lastname of the user
@@ -79,11 +84,12 @@ async function attachPdfToAccount(sfdc) {
const fullName = parts.join(' '); const fullName = parts.join(' ');
const reversedName = [...parts.slice(1), parts[0]].join(' '); const reversedName = [...parts.slice(1), parts[0]].join(' ');
conditions.push(`Name='${fullName}'`); //conditions.push(`Name='${fullName}'`);
if (fullName !== reversedName) { if (fullName !== reversedName) {
conditions.push(`Name='${reversedName}'`); conditions.push(`Name='${ escapeSoql(reversedName) }'`);
} }
} }
conditions.push(`Name='${ escapeSoql(rawName) }'`);
} }
// use the soql command to search in the database // use the soql command to search in the database
@@ -192,7 +198,7 @@ async function splitExcelFile(inputFile) {
}) })
// to store the names of the columns in columnNames // to store the names of the columns in columnNames
worksheet.getRow(1).eachCell((cell, cell_num) => { worksheet.getRow(1).eachCell({ includeEmpty: true }, (cell, cell_num) => {
columnNames.push(cell.value); columnNames.push(cell.value);
}) })
@@ -200,7 +206,7 @@ async function splitExcelFile(inputFile) {
for (let i = 2; i <= numRows + 1; i++) { for (let i = 2; i <= numRows + 1; i++) {
let dataUser = {}; let dataUser = {};
worksheet.getRow(i) worksheet.getRow(i)
.eachCell((cell, cell_num) => { .eachCell({ includeEmpty: true }, (cell, cell_num) => {
const rawColumnName = columnNames[cell_num - 1] || `column_${cell_num}`; const rawColumnName = columnNames[cell_num - 1] || `column_${cell_num}`;
// 1. Trim whitespace and replace spaces with underscores // 1. Trim whitespace and replace spaces with underscores
@@ -273,7 +279,25 @@ async function splitPDF(numRows, inputFile) {
} }
} }
// cleans up temporary files from OS temp directory
function cleanUp() {
tempFilePath.forEach((file) => {
try {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
}
catch (err) {
console.log(`Failed in deleting the temporary file ${ file }: `, err);
}
})
}
async function main() { async function main() {
if (!excelFile || !pdfFile) {
console.error('Error: Please provide both Excel and PDF paths.\nUsage: node script.js <path-to-excel> <path-to-pdf>');
process.exit(1);
}
const result = await splitExcelFile(excelFile); const result = await splitExcelFile(excelFile);
await splitPDF(result, pdfFile); await splitPDF(result, pdfFile);
const rl = readline.createInterface({ input, output }); const rl = readline.createInterface({ input, output });
@@ -299,6 +323,7 @@ async function main() {
} }
finally { finally {
rl.close(); rl.close();
cleanUp();
} }
} }