-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
133 lines (133 loc) · 4.99 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as browserGlobal from './browserGlobal.js';
import * as config from './config.js';
import * as parsers from './parsers.js';
function cleanRawCertificateOutput(rawOutput) {
const contractorLegalTradeName = parsers.stripHTML(rawOutput[config.certificateField_contractorLegalTradeName]);
const contractorAddress = parsers.stripHTML(rawOutput[config.certificateField_contractorAddress]);
const contractorNAICSCodes = parsers.parseNAICS(rawOutput[config.certificateField_naicsCodes]);
const clearanceCertificateNumber = parsers
.stripHTML(rawOutput[config.certificateField_clearanceCertificateNumber])
.split(' ')[0];
const validityPeriod = parsers.parseValidityPeriod(rawOutput[config.certificateField_validityPeriod]);
const principalLegalTradeName = parsers.stripHTML(rawOutput[config.certificateField_principalLegalTradeName]);
const principalAddress = parsers.stripHTML(rawOutput[config.certificateField_principalAddress]);
return {
contractorLegalTradeName,
contractorAddress,
contractorNAICSCodes,
clearanceCertificateNumber,
validityPeriodStart: validityPeriod.start,
validityPeriodEnd: validityPeriod.end,
principalLegalTradeName,
principalAddress
};
}
/**
* Retrieves a WSIB clearance certificate from the WSIB website.
* @param accountNumber - The WSIB account number
* @returns The WSIB clearance certificate data.
*/
export async function getClearanceByAccountNumber(accountNumber) {
let page;
try {
const browser = await browserGlobal.getBrowserGlobal();
page = await browser.newPage();
// Set up page options
page.setDefaultNavigationTimeout(browserGlobal.pageTimeoutMillis);
page.setDefaultTimeout(browserGlobal.pageTimeoutMillis);
await page.setExtraHTTPHeaders({
'Accept-Language': 'en'
});
// Load eservice
const pageResponse = await page.goto(config.clearanceStart_url, {
referer: 'https://www.wsib.ca/en',
waitUntil: 'domcontentloaded'
});
if (!(pageResponse?.ok() ?? false)) {
throw new Error(`Response Code = ${pageResponse?.status().toString()}`);
}
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector('body');
// Fill out form
await page.$eval(config.clearanceStart_searchFieldSelector, (inputElement, accountNumberValue) => {
inputElement.value = accountNumberValue;
}, accountNumber);
await page.$eval(config.clearanceStart_searchFormSelector, (formElement) => {
formElement.submit();
});
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector('body');
// Find result link
let hasError = false;
await page
.$eval(config.clearanceResult_certificateLinkSelector, (linkElement) => {
linkElement.click();
})
.catch(() => {
hasError = true;
});
if (hasError) {
const errorMessage = await page
.$eval(config.clearanceResult_certificateBadStandingSelector, (badStandingElement) => badStandingElement
? badStandingElement.textContent
: config.clearanceResult_defaultErrorMessage)
.catch(() => {
throw new Error(config.clearanceResult_defaultErrorMessage);
});
throw new Error(errorMessage ?? '');
}
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector('body');
// Parse the certificate
const certificateURL = page.url();
const parsedTable = await page.$eval(config.certificate_tableSelector, (tableElement) => {
const parsedTableValue = {};
const thElements = tableElement.querySelectorAll('thead tr th');
const tdElements = tableElement.querySelectorAll('tbody tr td');
for (const [index, thElement] of thElements.entries()) {
parsedTableValue[thElement.textContent ?? ''] =
tdElements[index].innerHTML;
}
return parsedTableValue;
});
const certificate = cleanRawCertificateOutput(parsedTable);
return {
success: true,
accountNumber,
...certificate,
certificateURL
};
}
catch (error) {
let errorURL = '';
try {
errorURL = page?.url() ?? '';
}
catch {
// ignore
}
return {
success: false,
accountNumber,
error,
errorURL
};
}
finally {
try {
if (page !== undefined) {
await page.close();
}
}
catch {
// ignore
}
}
}
/**
* Closes the cached web browser.
*/
export async function cleanUpBrowser() {
await browserGlobal.cleanUpBrowserGlobal(true);
}
export default getClearanceByAccountNumber;