Skip to content

Commit

Permalink
feat/ehj headless001 (#220)
Browse files Browse the repository at this point in the history
* headless

* dswid

* ver

* owl
  • Loading branch information
hajonsoft authored Mar 15, 2024
1 parent de0474c commit e94233a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hajonsoft-eagle",
"version": "2.6.31",
"version": "2.6.33",
"description": "HAJonSoft node app to submit passengers to visa systems",
"main": "index.js",
"scripts": {
Expand Down
137 changes: 100 additions & 37 deletions src/ehj.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ const config = [
name: "index",
regex: "https://ehaj.haj.gov.sa/EH/index.xhtml;jsessionid=",
},
{
name: "index2",
regex: "https://ehaj.haj.gov.sa/EH/index.xhtml.dswid=",
},
{
name: "login",
regex: "https://ehaj.haj.gov.sa/EH/login.xhtml",
Expand Down Expand Up @@ -100,8 +104,10 @@ const config = [
{
selector:
"#kt_app_content_container > div:nth-child(2) > form > div.ui-panel.ui-widget.ui-widget-content.ui-corner-all > div.ui-panel-content.ui-widget-content > div:nth-child(4) > div > input",
value: (row) =>
`${row.name.first}.${row.name.last}.${row.passportNumber}@emailinthecloud.com`.toLowerCase(),
value: (row) => {
const name = `${row.name.first}.${row.name.last}`.substring(0, 20);
return `${name}.${row.passportNumber}@emailinthecloud.com`.toLowerCase();
},
},
{
selector:
Expand All @@ -128,8 +134,10 @@ const config = [
{
selector:
"#kt_app_content_container > div:nth-child(2) > form > div.ui-panel.ui-widget.ui-widget-content.ui-corner-all > div.ui-panel-content.ui-widget-content > div:nth-child(4) > div > input",
value: (row) =>
`${row.name.first}.${row.name.last}.${row.passportNumber}@emailinthecloud.com`.toLowerCase(),
value: (row) => {
const name = `${row.name.first}.${row.name.last}`.substring(0, 20);
return `${name}.${row.passportNumber}@emailinthecloud.com`.toLowerCase();
},
},
{
selector:
Expand Down Expand Up @@ -486,11 +494,11 @@ async function pageContentHandler(currentConfig) {
switch (currentConfig.name) {
case "home":
case "index":
try {
const anchors = await page.$$eval("a", (els) => {
return els.map((el) => el.removeAttribute("target"));
});
} catch {}
case "index2":
console.log("index");
if (global.headless) {
await page.goto("https://ehaj.haj.gov.sa/EH/login.xhtml");
}
break;
case "login":
const isError = await page.$("#stepItemsMSGs > div > div");
Expand Down Expand Up @@ -582,9 +590,40 @@ async function pageContentHandler(currentConfig) {
await page.click("#verifyGAuthToken > div > div.col-lg-4 > a");
break;
case "dashboard":
// await page.goto(
// "https://ehaj.haj.gov.sa/EH/pages/hajMission/lookup/hajData/AddMrz.xhtml"
// );
// find if it is a mission account or a company account
// hajMission/lookup/hajData
const url = await page.url();
try {
const missionQuota = await page.$eval(
"#form > div > div > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > h4",
(el) => el.innerText
);

if (global.headless && missionQuota.includes("Mission")) {
const dswid = url.split("dswid=")[1];
await page.goto(
`https://ehaj.haj.gov.sa/EH/pages/hajMission/lookup/hajData/AddMrz.xhtml?dswid=${dswid}`
);
}
} catch (err) {
// console.log(err);
}
try {
const companyQuota = await page.$eval(
"#j_idt4630 > div > div > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > h4",
(el) => el.innerText
);

if (global.headless && companyQuota.includes("Default")) {
// get dswid value from the url
const companydswid = url.split("dswid=")[1];
await page.goto(
`https://ehaj.haj.gov.sa/EH/pages/hajCompany/lookup/hajData/AddMrz.xhtml?dswid=${companydswid}`
);
}
} catch (err) {
// console.log(err);
}
break;
case "add-pilgrim-select-method":
case "add-pilgrim-select-method-company":
Expand Down Expand Up @@ -655,7 +694,7 @@ async function pageContentHandler(currentConfig) {
passportNumber: t.passportNumber,
};
});
for (let i = 1; i <= 100; i++) {
for (let i = 1; i <= 200; i++) {
const isRowValid = await page.$(
`tbody > tr:nth-child(${i}) > td:nth-child(1)`
);
Expand Down Expand Up @@ -708,6 +747,7 @@ async function pageContentHandler(currentConfig) {
mofaNumber,
ehajNumber,
status,
"submissionData.ehj.status": "Submitted",
});
}

Expand Down Expand Up @@ -776,6 +816,24 @@ async function pageContentHandler(currentConfig) {
break;
case "add-mission-pilgrim":
case "add-company-pilgrim":
if (!passenger || !passenger.codeline) {
return;
}
// If the last pilgrim failed move on to the next one
// #stepItemsMSGs > div > div > div > ul > li > span
try {
const errorMessage = await page.$eval(
"#stepItemsMSGs > div > div > div > ul > li > span",
(el) => el.innerText
);
if (errorMessage.includes("active Haj Data")) {
util.incrementSelectedTraveler();
await sendPassenger(util.getSelectedTraveler());
return;
}
} catch (err) {
// console.log(err);
}
console.log(passenger.codeline);
if (startTime) {
fs.appendFileSync(
Expand All @@ -789,6 +847,9 @@ async function pageContentHandler(currentConfig) {
`\n${counter} - ${startTime} - ${passenger?.slug}\n${passenger.codeline}\n`
);

if (global.headless) {
fs.writeFileSync(getPath("loop.txt"), "ehaj", "utf-8");
}
await util.toggleBlur(page, false);
if (fs.existsSync(getPath("loop.txt"))) {
await sendPassenger(util.getSelectedTraveler());
Expand Down Expand Up @@ -896,10 +957,6 @@ async function pageContentHandler(currentConfig) {
rememberValue("#embassy", "ehaj_pilgrim_embassy");
rememberValue("#packge2", "ehaj_pilgrim_package");
rememberValue("#roomType", "ehaj_pilgrim_roomType");
rememberValue(
"#countryOfResidence",
"ehaj_pilgrim_countryOfResidence"
);
rememberValue("#hajType", "ehaj_pilgrim_hajType");
},
},
Expand Down Expand Up @@ -1016,17 +1073,29 @@ async function pageContentHandler(currentConfig) {
);
} catch {}
await page.waitForTimeout(1000);
await util.commit(
page,
[
{
selector: "#passportIssueDate",
value: (row) =>
`${row.passIssueDt.dd}/${row.passIssueDt.mm}/${row.passIssueDt.yyyy}`,
},
],
passenger
);
// try issue date 5 times
const passportIssueDateFromData = `${passenger.passIssueDt.dd}/${passenger.passIssueDt.mm}/${passenger.passIssueDt.yyyy}`;
for (let i = 1; i <= 5; i++) {
await util.commit(
page,
[
{
selector: "#passportIssueDate",
value: (row) =>
`${row.passIssueDt.dd}/${row.passIssueDt.mm}/${row.passIssueDt.yyyy}`,
},
],
passenger
);
await page.waitForTimeout(500);
const passportIssueDateFromPage1 = await page.$eval(
"#passportIssueDate",
(el) => el.value
);
if (passportIssueDateFromPage1 === passportIssueDateFromData) {
break;
}
}
if (pageUrl.includes("hajMission")) {
await page.$eval(
"#j_idt4105_content > div > div:nth-child(4) > div > label",
Expand Down Expand Up @@ -1056,10 +1125,7 @@ async function pageContentHandler(currentConfig) {
await page.click("#covidVaccines");
}

const rememberedCountryOfResidence = budgie.get(
"ehaj_pilgrim_countryOfResidence",
passenger.nationality.telCode
);
const rememberedCountryOfResidence = data.system.country.telCode;
if (rememberedCountryOfResidence) {
const countryOfResidenceText = await page.$$eval(
"#countryOfResidence > option",
Expand Down Expand Up @@ -1101,7 +1167,6 @@ async function pageContentHandler(currentConfig) {
"#passportIssueDate",
(el) => el.value
);
const passportIssueDateFromData = `${passenger.passIssueDt.dd}/${passenger.passIssueDt.mm}/${passenger.passIssueDt.yyyy}`;
if (passportIssueDateFromPage !== passportIssueDateFromData) {
console.log(
"passport issue date from page: ",
Expand All @@ -1125,10 +1190,8 @@ async function pageContentHandler(currentConfig) {
"#actionPanel > div > input.btn.btn-primary"
).value = "Submit (Ok)";
});
if (fs.existsSync(getPath("loop.txt"))) {
await page.waitForSelector(submitButtonSelector);
await page.click(submitButtonSelector);
}
await page.waitForSelector(submitButtonSelector);
await page.click(submitButtonSelector);
}

break;
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ async function initPage(config, onContentLoaded, data) {
}

const launchOptions = {
headless: isCloudRun || isHeadless,
headless: process.argv.includes("--debug") ? false : isCloudRun || isHeadless,
ignoreHTTPSErrors: true,
defaultViewport,
args,
Expand Down

0 comments on commit e94233a

Please sign in to comment.