-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
205 lines (171 loc) · 5.26 KB
/
index.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import type { puppeteer } from '@cityssm/puppeteer-launch'
import * as browserGlobal from './browserGlobal.js'
import * as config from './config.js'
import * as parsers from './parsers.js'
import type {
WSIBClearance_Certificate,
WSIBClearance_Failure,
WSIBClearance_Success
} from './types.js'
function cleanRawCertificateOutput(
rawOutput: Record<string, unknown>
): WSIBClearance_Certificate {
const contractorLegalTradeName = parsers.stripHTML(
rawOutput[config.certificateField_contractorLegalTradeName] as string
)
const contractorAddress = parsers.stripHTML(
rawOutput[config.certificateField_contractorAddress] as string
)
const contractorNAICSCodes = parsers.parseNAICS(
rawOutput[config.certificateField_naicsCodes] as string
)
const clearanceCertificateNumber = parsers
.stripHTML(
rawOutput[config.certificateField_clearanceCertificateNumber] as string
)
.split(' ')[0]
const validityPeriod = parsers.parseValidityPeriod(
rawOutput[config.certificateField_validityPeriod] as string
)
const principalLegalTradeName = parsers.stripHTML(
rawOutput[config.certificateField_principalLegalTradeName] as string
)
const principalAddress = parsers.stripHTML(
rawOutput[config.certificateField_principalAddress] as string
)
return {
contractorLegalTradeName,
contractorAddress,
contractorNAICSCodes,
clearanceCertificateNumber,
validityPeriodStart: validityPeriod.start as Date,
validityPeriodEnd: validityPeriod.end as Date,
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: string
): Promise<WSIBClearance_Failure | WSIBClearance_Success> {
let page: puppeteer.Page | undefined
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: HTMLTextAreaElement, accountNumberValue: string) => {
inputElement.value = accountNumberValue
},
accountNumber
)
await page.$eval(
config.clearanceStart_searchFormSelector,
(formElement: HTMLFormElement) => {
formElement.submit()
}
)
browserGlobal.keepBrowserGlobalAlive()
await page.waitForSelector('body')
// Find result link
let hasError = false
await page
.$eval(
config.clearanceResult_certificateLinkSelector,
(linkElement: HTMLAnchorElement) => {
linkElement.click()
}
)
.catch(() => {
hasError = true
})
if (hasError) {
const errorMessage = await page
.$eval(
config.clearanceResult_certificateBadStandingSelector,
(badStandingElement: HTMLElement) => 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: HTMLTableElement) => {
const parsedTableValue = {}
const thElements: NodeListOf<HTMLTableCellElement> =
tableElement.querySelectorAll('thead tr th')
const tdElements: NodeListOf<HTMLTableCellElement> =
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(): Promise<void> {
await browserGlobal.cleanUpBrowserGlobal(true)
}
export default getClearanceByAccountNumber