multiple accounts (with the same client's name) problem solved
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
const ExcelJS = require('exceljs');
|
const ExcelJS = require('exceljs');
|
||||||
const { PDFParse } = require('pdf-parse');
|
const { PDFParse } = require('pdf-parse');
|
||||||
const { PDFDocument } = require('pdf-lib');
|
const { PDFDocument } = require('pdf-lib');
|
||||||
const readline = require('node:readline/promises');
|
|
||||||
const { stdin: input, stdout: output } = require('node:process');
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
@@ -22,7 +20,7 @@ const authConfig = {
|
|||||||
|
|
||||||
async function initializeSalesforceConnection() {
|
async function initializeSalesforceConnection() {
|
||||||
// sfdc.setProductionBaseUrl(); // or sfdc.setSandboxBaseUrl();
|
// sfdc.setProductionBaseUrl(); // or sfdc.setSandboxBaseUrl();
|
||||||
sfdc.setSandboxBaseUrl(); // Example: using sandbox
|
sfdc.setProductionBaseUrl(); // Example: using sandbox
|
||||||
sfdc.setTokenFile(authConfig.sfdcTokenFile);
|
sfdc.setTokenFile(authConfig.sfdcTokenFile);
|
||||||
sfdc.setClientId(authConfig.sfdcClientId);
|
sfdc.setClientId(authConfig.sfdcClientId);
|
||||||
sfdc.setClientSecret(authConfig.sfdcClientSecret);
|
sfdc.setClientSecret(authConfig.sfdcClientSecret);
|
||||||
@@ -54,7 +52,6 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
|||||||
let parser;
|
let parser;
|
||||||
try {
|
try {
|
||||||
let contentVersion;
|
let contentVersion;
|
||||||
const rl = readline.createInterface({ input, output });
|
|
||||||
for (let i = 0; i < tempFiles.length; i++) {
|
for (let i = 0; i < tempFiles.length; i++) {
|
||||||
// 1. Read the local PDF file and convert it to Base64
|
// 1. Read the local PDF file and convert it to Base64
|
||||||
const pdfBuffer = fs.readFileSync(tempFiles[i]);
|
const pdfBuffer = fs.readFileSync(tempFiles[i]);
|
||||||
@@ -63,11 +60,17 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
|||||||
try {
|
try {
|
||||||
const result = await parser.getText();
|
const result = await parser.getText();
|
||||||
let conditions = [];
|
let conditions = [];
|
||||||
|
let matchAccount = null;
|
||||||
|
|
||||||
const userID = result.text.match(/Codice fiscale:\s*([A-Za-z0-9]+)/i);
|
const userID = result.text.match(/Codice fiscale:\s*([A-Za-z0-9]+)/i);
|
||||||
const codiceFiscale = userID ? userID[1].trim() : null;
|
const codiceFiscale = userID ? userID[1].trim() : null;
|
||||||
if (codiceFiscale) {
|
if (codiceFiscale) {
|
||||||
conditions.push(`CF__c='${ codiceFiscale }'`);
|
const cfSoql = `SELECT Id, Name, CF__c FROM Account WHERE CF__c='${codiceFiscale }' ORDER BY CreatedDate ASC LIMIT 1`;
|
||||||
|
const cfResult = await sfdc.query(cfSoql);
|
||||||
|
|
||||||
|
if (cfResult && cfResult.records && cfResult.records.length > 0) {
|
||||||
|
matchAccount = cfResult.records[0];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// since the name and lastname of the user are reversed in some files, we need to put it in order
|
// since the name and lastname of the user are reversed in some files, we need to put it in order
|
||||||
@@ -86,29 +89,30 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
|||||||
conditions.push(`Name='${ escapeSoql(reversedName) }'`);
|
conditions.push(`Name='${ escapeSoql(reversedName) }'`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
conditions.push(`Name='${ escapeSoql(rawName) }'`);
|
conditions.push(`Name='${ escapeSoql(rawName) }'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// use the soql command to search in the database
|
const nameSoql = `SELECT Id, Name, CF__c FROM Account WHERE (${conditions.join(' OR ')}) ORDER BY CF__c DESC NULLS LAST, CreatedDate ASC LIMIT 1`;
|
||||||
// if codice fiscale is not available, I search the user's account based on their name and lastname
|
const nameResult = await sfdc.query(nameSoql);
|
||||||
const soql1 = `SELECT Id, Name FROM Account WHERE (${conditions.join(' OR ')}) ORDER BY CreatedDate DESC LIMIT 1`;
|
|
||||||
const result_soql = await sfdc.query(soql1);
|
|
||||||
|
|
||||||
if (result_soql && result_soql.records && result_soql.records.length > 0) { // if we find something
|
if (nameResult && nameResult.records && nameResult.records.length > 0) {
|
||||||
let matchFound = result_soql.records[0];
|
matchAccount = nameResult.records[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matchAccount) { // if we find something
|
||||||
console.log('--- Match found! ---');
|
console.log('--- Match found! ---');
|
||||||
|
|
||||||
if (isDryRun) {
|
if (isDryRun) {
|
||||||
console.log('DRY RUN: non creato documento');
|
console.log('DRY RUN: non creato documento');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const title = await rl.question('Choose a title for the document: ');
|
|
||||||
const nowIsoString = new Date().toISOString(); // current timestamp in UTC
|
const nowIsoString = new Date().toISOString(); // current timestamp in UTC
|
||||||
const newDocument = await sfdc.create('Documento__c', {
|
const newDocument = await sfdc.create('Documento__c', {
|
||||||
Account__c: `${ result_soql.records[0].Id }`,
|
Account__c: `${ matchAccount.Id }`,
|
||||||
Name: `${ title }`,
|
Name: 'Attestato di Formazione',
|
||||||
Data_Caricamento__c: nowIsoString,
|
Data_Caricamento__c: `${ nowIsoString }`,
|
||||||
Tipo_Documento__c: "Stage doc",
|
Tipo_Documento__c: "Dossier di Tirocinio",
|
||||||
Stato__c: 'Valido'
|
Stato__c: 'Valido'
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -116,10 +120,9 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
|||||||
// to FirstPublishLocationId field
|
// to FirstPublishLocationId field
|
||||||
const soql2 = `SELECT Account__c FROM Documento__c WHERE Id='${ newDocument.id }'`;
|
const soql2 = `SELECT Account__c FROM Documento__c WHERE Id='${ newDocument.id }'`;
|
||||||
const docQuery = await sfdc.query(soql2);
|
const docQuery = await sfdc.query(soql2);
|
||||||
console.log(`Doc Id: ${ newDocument.id }`);
|
|
||||||
contentVersion = await sfdc.create('ContentVersion', {
|
contentVersion = await sfdc.create('ContentVersion', {
|
||||||
Title: `${ result_soql.records[0].Name }_Document.pdf`,
|
Title: `${ matchAccount.Name }_Document.pdf`,
|
||||||
PathOnClient: `${ result_soql.records[0].Name }_Document.pdf`,
|
PathOnClient: `${ matchAccount.Name }_Document.pdf`,
|
||||||
VersionData: base64Pdf, // Must be Base64 string
|
VersionData: base64Pdf, // Must be Base64 string
|
||||||
FirstPublishLocationId: `${ docQuery.records[0].Account__c }`
|
FirstPublishLocationId: `${ docQuery.records[0].Account__c }`
|
||||||
});
|
});
|
||||||
@@ -128,6 +131,8 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
|||||||
else {
|
else {
|
||||||
console.log('Account non trovato su Salesforce!');
|
console.log('Account non trovato su Salesforce!');
|
||||||
}
|
}
|
||||||
|
console.log(`Name: ${ matchAccount.Name }`);
|
||||||
|
console.log(`Name: ${ matchAccount.Id }`);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if (parser) {
|
if (parser) {
|
||||||
@@ -137,7 +142,6 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
|||||||
}
|
}
|
||||||
console.log(`PDFs successfully attached!`);
|
console.log(`PDFs successfully attached!`);
|
||||||
console.log('Dry run completed!');
|
console.log('Dry run completed!');
|
||||||
rl.close();
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error uploading PDF: `, error);
|
console.error(`Error uploading PDF: `, error);
|
||||||
|
|||||||
Reference in New Issue
Block a user