Commit 8164f545 authored by Laurent's avatar Laurent

iguana: fix fetch holds & loans

parent e9269472
......@@ -3,6 +3,7 @@ import * as cheerio from 'cheerio'
import * as xmljs from 'xml-js'
import {Account, Loan, Hold} from '../../models'
import {WrongLoginPassword} from './wrong-login-password'
import * as UrlParse from 'url-parse';
export class IguanaV3 extends PortalAdapter {
public getIdentifier(): string {
......@@ -16,25 +17,24 @@ export class IguanaV3 extends PortalAdapter {
public canHandleWebsite($): boolean {
return $('a[href*="www.main.cls?sUrl="]').length
return $('a[href*="www.main.cls?sUrl="]').length > 0
}
protected _fetchLoans(account: Account): Promise<Array<Loan>> {
return this
._soapRequest(account,
'CurrentLoans',
{
CurrentLoans: {
SessionId: account.credentials['session_id'],
Data: {
From: 1,
To: 20
}
SessionId: account.credentials['session_id'],
Data: {
From: 1,
To: 20
}
})
.then( (datas: any) => {
let items = datas.CurrentLoansResponse.CurrentLoansResult.Items.Item;
return items.map( (item) => {
return (Array.isArray(items) ? items : [items]).map( (item) => {
let title_parts = /([^\/]+)\/([^;]+)/.exec(item.Title._text);
return new Loan()
......@@ -53,19 +53,19 @@ export class IguanaV3 extends PortalAdapter {
protected _fetchHolds(account: Account): Promise<Array<Hold>> {
return this
._soapRequest(account,
'CurrentReservations',
{
CurrentReservations: {
SessionId: account.credentials['session_id'],
Data: {
From: 1,
To: 20
}
SessionId: account.credentials['session_id'],
Data: {
From: 1,
To: 20
}
})
.then( (datas: any) => {
let items = datas.CurrentReservationsResponse.CurrentReservationsResult.Items.Item;
return items.map( (item) => {
let title_parts = /([^:]+):.*\/([^;]+)/.exec(item.Title._text);
return (Array.isArray(items) ? items : [items]).map( (item) => {
let title_parts = /([^:\/]+).*\/([^;]+)/.exec(item.Title._text);
return new Hold()
.setHoldId(item.ReservationNumber._text)
......@@ -91,49 +91,92 @@ export class IguanaV3 extends PortalAdapter {
if (!(account.credentials['login'] && account.credentials['password']))
return Promise.reject(new WrongLoginPassword());
let csp_session_id:string;
return this
.request(account, { url: account.getUrl() + '/iguana/www.main.cls?surl=accueil' })
.request(account, { url: account.getUrl() })
.then( (response) => {
let session_regex = /Vfocus.Settings.sessionID = "([a-zA-Z0-9]+)"/
return session_regex.exec(response.content.toString())[1]
})
.then( (csp_session_id) => {
csp_session_id = session_regex.exec(response.content.toString())[1]
return this
._soapRequest(account,
'CheckCredentials',
{
CheckCredentials: {
Language: 'fre',
Profile: 'Iguana',
CspSessionId: csp_session_id,
LogonId: account.credentials['login'],
Password: account.credentials['password']
}
Language: 'fre',
Profile: 'Iguana',
CspSessionId: csp_session_id,
LogonId: account.credentials['login'],
Password: account.credentials['password']
})
.then( (datas) => {
let result = datas['CheckCredentialsResponse']['CheckCredentialsResult']
if ('1' != result['Result']['_text'])
return Promise.reject(new WrongLoginPassword());
account.credentials['session_id'] = result['SessionId']['_text']
account.beConnected();
})
})
.then( (datas) => {
let result = datas['CheckCredentialsResponse']['CheckCredentialsResult']
if ('1' != result['Result']['_text'])
return Promise.reject(new WrongLoginPassword());
account.credentials['session_id'] = result['SessionId']['_text']
return result;
})
.then( (datas) => {
let form_data = {
BorrowerId: datas['BorrowerId']['_text'],
Category: datas['Category']['_text'],
CspSessionId: csp_session_id,
Digital: datas['Digital']['_text'],
Email: datas['Email']['_text'],
HomeLocation: datas['HomeLocation']['_text'],
Language: 'fre',
PasswordExpired: 0,
Profile: 'Iguana',
SessionId: datas['SessionId']['_text'],
tempList: '',
Token: datas['Token']['_text'],
UserAge: datas['UserAge']['_text'],
UserName: datas['UserName']['_text'],
ValidRequests: 'Welcome,CurrentLoans,LoanHistory,PaymentsHistory,CurrentReservations,Interests,ReadingLists,LinkedAccounts,SearchFilter,ViewId',
ViewId: ''
}
return this.request(account,
{
url: (new UrlParse(account.getUrl()).origin + '/iguana/Proxy.SetLogon.cls'),
method: 'POST',
content: this._encodeForm(form_data),
headers: {
headers: {"Content-Type": "application/x-www-form-urlencoded"}
}
})
})
.then( (response) => {
account.beConnected();
})
}
protected _soapRequest(account: Account, data: object): Promise<object> {
protected _soapRequest(account: Account, soap_action, data: object): Promise<object> {
let content = {}
content[soap_action] = data
let envelope = '<?xml version="1.0" encoding="UTF-8"?>'
+ '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">'
+ '<SOAP-ENV:Body>' + xmljs.js2xml(data, {compact: true}) + '</SOAP-ENV:Body>'
+ '<SOAP-ENV:Body>' + xmljs.js2xml(content, {compact: true}) + '</SOAP-ENV:Body>'
+ '</SOAP-ENV:Envelope>';
let base_url = new UrlParse(account.getUrl()).origin;
return this
.request(account,
{ url: account.getUrl() + '/iguana/Proxy.UserActivities.cls',
{ url: base_url + '/iguana/Proxy.UserActivities.cls',
method: 'POST',
content: envelope,
headers: {"Content-Type": "text/xml; charset=UTF-8"} })
headers: {"Content-Type": "text/xml; charset=UTF-8",
SOAPAction: soap_action} })
.then( (response) => {
console.dir([response.content.toString()]);
let datas = xmljs.xml2js(response.content.toString(),
{compact: true,
ignoreAttributes: true});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment