-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPatient.js
78 lines (65 loc) · 2.44 KB
/
getPatient.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 TIMEOUT = 5000;
exports.getPatient = getPatient;
async function getPatient(page, browser, { username, password, captcha, nir }) {
const navigationPromise = page.waitForNavigation();
await page.type("#form-login input[name=user]", username);
await page.type("#form-login input[name=password]", password);
await page.type("#form-login input[name=captcha_user_code]", captcha);
await page.click(" #form-login > .btn");
await navigationPromise;
await page
.waitForSelector('input[name="c8_contextepatient_portletnirBeneficiaire"]', { timeout: TIMEOUT })
.catch(() => {
throw Error("nir input not found");
});
await page.type('input[name="c8_contextepatient_portletnirBeneficiaire"]', nir);
await page.click("form[name=contextePatientForm] input[type=submit]");
await navigationPromise;
const beneficiaries = [];
try {
const singleBeneficiary = await getBeneficiaryInfo(page);
console.log({ singleBeneficiary });
beneficiaries.push(singleBeneficiary);
} catch (error) {
const selectCssSelector = "select[name=c8_contextepatient_portletidBeneficiaireSelectionne]";
await page.waitForSelector(selectCssSelector, { timeout: TIMEOUT });
const beneficiariesKeys = await page.$$eval(
`${selectCssSelector} > option:not([value=''])`,
// @ts-ignore
(options) => options.map((el) => el.value)
);
// get all beneficiaries infos
for (let i = 0; i < beneficiariesKeys.length; i++) {
await page.click(selectCssSelector);
await page.select(selectCssSelector, beneficiariesKeys[i]);
await navigationPromise;
const patient = await getBeneficiaryInfo(page);
beneficiaries.push(patient);
}
} finally {
browser.close();
}
return beneficiaries;
}
async function getBeneficiaryInfo(page) {
await page.waitForSelector("form[name=contextePatientForm]");
const [lastName, firstName, socialSecurityNumber, birthDate, rank, socialSecurityName, managementCentre] =
await Promise.all([
page.$eval("#info1", (el) => el.value),
page.$eval("#info2", (el) => el.value),
page.$eval("#info3", (el) => el.value),
page.$eval("#info4", (el) => el.value),
page.$eval("#info6", (el) => el.value),
page.$eval("#info8", (el) => el.value),
page.$eval("#info9", (el) => el.value),
]);
return {
lastName,
firstName,
socialSecurityNumber,
birthDate,
rank,
socialSecurityName,
managementCentre,
};
}