-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (108 loc) · 4.79 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { FasterReportExporter } from '@cityssm/faster-report-exporter';
import { csvReports, xlsxReports } from '@cityssm/faster-report-parser';
import { minutesToMillis } from '@cityssm/to-millis';
import Debug from 'debug';
import { DEBUG_NAMESPACE } from './debug.config.js';
import { deleteFile } from './utilities.js';
const debug = Debug(`${DEBUG_NAMESPACE}:index`);
const integrationsTimeoutMillis = minutesToMillis(1);
export class FasterUnofficialAPI {
#fasterReportExporter;
/**
* Initialize the Faster Unofficial API
* @param fasterTenantOrBaseUrl - The subdomain of the FASTER Web URL before ".fasterwebcloud.com"
* or the full domain and path including "/FASTER"
* @param fasterUserName - The username to log in with
* @param fasterPassword - The password to log in with
* @param options - Additional options
*/
constructor(fasterTenantOrBaseUrl, fasterUserName, fasterPassword, options = {}) {
this.#fasterReportExporter = new FasterReportExporter(fasterTenantOrBaseUrl, fasterUserName, fasterPassword, options);
}
/**
* Retrieves a list of assets using the W114 report.
* @returns A list of assets
*/
async getAssets() {
debug('Exporting asset list...');
const assetReportPath = await this.#fasterReportExporter.exportAssetList('Excel');
debug(`Asset list exported: ${assetReportPath}`);
debug('Parsing asset report...');
const report = xlsxReports.parseW114ExcelReport(assetReportPath);
debug(`Asset report parsed with ${report.data.length} asset(s).`);
await deleteFile(assetReportPath);
return report.data;
}
/**
* Retrieves a list of inventory items using the W200 report.
* @returns A list of inventory items, grouped by storeroom
*/
async getInventory() {
debug('Exporting inventory report...');
const inventoryReportPath = await this.#fasterReportExporter.exportInventory('Excel');
debug(`Inventory report exported: ${inventoryReportPath}`);
debug('Parsing inventory report...');
const report = xlsxReports.parseW200ExcelReport(inventoryReportPath);
debug(`Inventory report parsed with ${report.data.length} storeroom(s).`);
await deleteFile(inventoryReportPath);
return report.data;
}
/**
* Executes an integration by name.
* @param integrationName - The name of the integration to execute
* @returns `true` if the integration was executed, false if not
*/
async executeIntegration(integrationName) {
const { browser, page } = await this.#fasterReportExporter._getLoggedInFasterPage();
try {
await page.goto(this.#fasterReportExporter.fasterUrlBuilder.integrationsUrl, {
timeout: integrationsTimeoutMillis
});
await page.waitForNetworkIdle({
timeout: integrationsTimeoutMillis
});
// Find the integration row
const integrationTableRowElements = await page.$$(
// eslint-disable-next-line no-secrets/no-secrets
'#ctl00_ContentPlaceHolder_Content_IntegrationRadDock_C_IntegrationRadGrid_ctl00 tbody tr');
for (const integrationTableRowElement of integrationTableRowElements) {
const integrationNameElement = await integrationTableRowElement.$('td:nth-child(1) a');
if (integrationNameElement === null) {
continue;
}
const integrationNameText = await integrationNameElement.evaluate((cell) => cell.textContent);
if (integrationNameText === integrationName) {
debug(`Integration found: ${integrationName}`);
const integrationActionLinkElements = await integrationTableRowElement.$$('td:nth-child(3) a');
for (const integrationActionLinkElement of integrationActionLinkElements) {
const integrationActionLinkText = await integrationActionLinkElement.evaluate((cell) => cell.textContent);
if (integrationActionLinkText === 'Execute') {
debug(`Executing integration: ${integrationName}`);
await integrationActionLinkElement.click();
return true;
}
}
}
}
}
finally {
try {
await browser.close();
}
catch {
// Ignore errors
}
}
return false;
}
}
export const integrationNames = {
inventoryImportUtility: 'Inventory Import Utility'
};
export const parser = {
csvReports,
xlsxReports
};
export const exporter = {
FasterReportExporter
};