-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (67 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const jsforce = require('jsforce')
const express = require('express')
require('dotenv').config()
const fs = require('fs');
const app = express()
const PORT = 3001
const { SF_LOGINURL, SF_USERNAME, SF_PASSWORD, SF_TOKEN } = process.env
const fileName = './output.txt';
const PASSWORD_TOKEN = SF_PASSWORD+SF_TOKEN
const conn = new jsforce.Connection({
loginUrl: SF_LOGINURL,
version: '60.0'
})
conn.login(SF_USERNAME, PASSWORD_TOKEN, (err, userInfo) => {
if (err) {
console.log(err);
} else {
console.log("User Id:" + userInfo.id);
console.log("Org Id:" + userInfo.organizationId)
console.log(`open localhost:${PORT} to fetch report`)
}
})
function getReportIdList() {
const data = fs.readFileSync('./input.txt', { encoding: 'utf8', flag: 'r' });
return data.trim().split(',');
}
async function getReportTypes(recordsList) {
const metaData = [];
let count = 1;
for (const reportId of recordsList) {
try {
const report = await conn.analytics.report(reportId).describe();
metaData.push(report.reportMetadata.reportType.type);
console.log(report.reportMetadata.reportType.type);
console.log(count++, ' completed');
} catch (error) {
console.error(`Error for report ${reportId}:`, error);
metaData.push(count++,' error Id ', reportId,);
}
}
return metaData;
}
async function writeToFile(data, fileName) {
try {
const content = data.join('\n') + '\n'; // Join data with newlines and add a final newline
fs.writeFileSync(fileName, content, (err) => err && console.error('file write err'));
console.log('Data appended to file successfully!');
} catch (error) {
console.error('Error appending to file:', error);
}
}
app.get('/', async (req, res) => {
try {
const reportIdList = await getReportIdList();
const metaData = await getReportTypes(reportIdList);
console.log('completed');
await writeToFile(metaData, fileName);
console.log("File written successfully!");
res.send(metaData);
} catch (error) {
console.error("Error:", error);
res.status(500).send("Internal Server Error");
}
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`)
})