Commit dcee9cc0 authored by Laurent's avatar Laurent

WIP: Add adapter for new InMedia

parent 5a68a8aa
......@@ -171,7 +171,6 @@ export class LibraryController extends Controller {
return this._portal.fetchItem(this._account, barcode);
})
.then( (item: Item) => {
console.dir(item);
this._turnBusyIndicator('off');
this.set('item', item);
this.navigate({ moduleName: "views/library/loan_item",
......
......@@ -64,4 +64,6 @@
<string name="do_loan_barcode">Loan : %s</string>
<string name="enter_password">To finish the request, enter your password</string>
<string name="loan_title_date_due">%s, bring back for %s</string>
<string name="hold_not_available">not available</string>
<string name="hold_available">available</string>
</resources>
......@@ -64,4 +64,6 @@
<string name="do_loan_barcode">Prestamo : %s</string>
<string name="enter_password">Para finalizar la solicitud, ingrese su contraseña</string>
<string name="loan_title_date_due">%s, para regresar a más tardar %s</string>
<string name="hold_not_available">no disponible</string>
<string name="hold_available">disponible</string>
</resources>
......@@ -64,4 +64,6 @@
<string name="do_loan_barcode">Emprunt : %s</string>
<string name="enter_password">Pour finaliser la demande, veuillez saisir votre mot de passe</string>
<string name="loan_title_date_due">%s, à retourner au plus tard le %s</string>
<string name="hold_not_available">pas encore disponible</string>
<string name="hold_available">disponible</string>
</resources>
......@@ -30,8 +30,6 @@ export class Manager {
return this._http
.request(params)
.then( (response) => {
console.dir(response.content.toString());
let datas = response.content.toJSON();
return datas;
})
......
import * as http from 'http';
import {PortalAdapter} from './adapter';
import {Database, Account, Hold, Loan, Record, Item} from '../../models';
import {WrongLoginPassword} from './wrong-login-password';
export class InMediaV2 extends PortalAdapter {
public getIdentifier(): string {
return 'inmedia-v2';
}
public getLabel(): string {
return 'InMedia (V2)';
}
public canHandleWebsite($): boolean {
return $('boddy>div#app').length;
}
protected _signIn(account): Promise<any> {
if (!(account.credentials['login'] && account.credentials['password']))
return Promise.reject(new WrongLoginPassword());
return this
.request(account,
{ url: account.getUrl() + '/in/rest/api/authenticate',
method: "POST",
content: "locale=fr&username=" + account.credentials['login'] + "&password=" + account.credentials['password'],
headers: {"Content-Type": "application/json"} })
.then( (response) => {
return this._handleSignIn(account, response)
} )
}
protected _handleSignIn(account: Account, response: http.HttpResponse): Promise<any> {
let datas = response.content.toJSON()
account.credentials['token'] = datas.token
account.beConnected()
return Promise.resolve()
}
public requestUser(account: Account, action: string): Promise<any> {
return this
.request(account,
{ url: account.getUrl() + '/in/rest/api/accountPage?type=' + action,
headers: { authorization: 'Bearer ' + account.credentials['token'] }})
.then( (response) => {
let datas = response.content.toJSON();
return datas;
});
}
public _fetchLoans(account: Account): Promise<Array<Loan>> {
return this
.requestUser(account, 'loans&pageNo=1&pageSize=100')
.then( (datas) => {
return datas['items'].map( (loan) => {
let date_due = loan['data']['returnDate'];
return new Loan()
.setLoanId(loan['data']['seqNo'])
.setTitle(loan['data']['title'])
.setAuthor(loan['data']['author'])
.setDateDue([date_due.substr(0,4), date_due.substr(4,2), date_due.substr(6,2)].join('-'))
.setLibrary(loan['data']['branch']['desc'])
.setRecordThumbnail(account.getUrl() + '/in/rest/Thumb/image?isPhysical=1&isbn=' + loan['data']['isbn13'])
})
})
}
public _fetchHolds(account: Account): Promise<Array<Hold>> {
return this
.requestUser(account, 'reservations&pageNo=1&pageSize=100')
.then( (datas) => {
return datas['items'].map( (loan) => {
return new Hold()
.setHoldId(loan['data']['seqNo'])
.setTitle(loan['data']['title'])
.setAuthor(loan['data']['author'])
.setStatus(this._holdStatus(loan['data']['statusCode']))
.setLibrary(loan['data']['branch']['desc'])
.setRecordThumbnail(account.getUrl() + '/in/rest/Thumb/image?isPhysical=1&isbn=' + loan['data']['isbn13'])
})
})
}
protected _holdStatus(code: string): string {
let codes = {RESV_NOT_AVAILABLE: L('hold_not_available'),
RESV_AVAILABLE: L('hold_available')};
return codes[code] ? codes[code] : L('unknown');
}
}
import {HTTPScenario} from './http-scenario';
import {HTTPScenario} from './http-scenario'
export class InmediaV2SignInWithoutToken extends HTTPScenario {
export class InMediaV2SignIn extends HTTPScenario {
public constructor(base_url: string) {
super();
this.setup(base_url);
super()
this.setup(base_url)
}
......@@ -13,7 +11,7 @@ export class InmediaV2SignInWithoutToken extends HTTPScenario {
this
.expect({ url: base_url + '/in/rest/api/authenticate',
method: "POST",
content: 'locale=fr&password=polo&username=marco',
content: 'locale=fr&username=marco&password=polo',
headers: {"Content-Type": "application/json"} },
{ content: JSON.stringify(
......@@ -23,14 +21,7 @@ export class InmediaV2SignInWithoutToken extends HTTPScenario {
"token":"zetoken",
"userid":"123456"}
)
});
}
}
abstract class InmediaV2WithValidToken extends HTTPScenario {
public constructor(base_url: string) {
super();
})
}
......@@ -105,38 +96,138 @@ abstract class InmediaV2WithValidToken extends HTTPScenario {
}
]
}
);
)
}
public loansJSON(): string {
return JSON.stringify(
{"items":[],
"total":0}
);
{
"items": [
{
"data": {
"author": "Iwamura, Kazuo, 1939-....",
"authorExplore": "au: \"Iwamura, Kazuo, 1939-....\"",
"branch": {
"branchCode": "PARTDI",
"desc": "Part-Dieu"
},
"canChangeDueDate": false,
"copyProblem": false,
"dateFormat": "yyyyMMdd",
"documentNumber": "37001034868856",
"hasAuthor": true,
"hasAuthorExplore": true,
"hasCharge": false,
"hasIsbn": true,
"hasIssueCaption": false,
"hasTitle": true,
"hasVolume": false,
"isLate": false,
"isRenewable": true,
"isbn": "978-2-211-20102-5",
"isbn10": "2211201024",
"isbn13": "9782211201025",
"lateNoticeDates": [],
"loanDate": "20180626",
"lost": false,
"nbLateNoticeSent": 0,
"recalled": false,
"renewalCondition": "OK",
"renewalConditionMessage": "globalErrorLeg_list.OK",
"reserved": false,
"returnDate": "20180807",
"returnHour": "23:00:00",
"seqNo": "1884668",
"suppressPrintLate": false,
"title": "La pomme rouge [Livre] / Kazuo Iwamura",
"userRenewed": false
},
"ilsType": "Portfolio",
"posInSet": 0,
"type": "loans"
}
],
"total": 1
}
)
}
public holdsJSON(): string {
return JSON.stringify(
{
"items": [
{
"data": {
"assigned": false,
"author": "Jullien, Jean, 1983-....",
"authorExplore": "au: \"Jullien, Jean, 1983-....\"",
"available": false,
"branch": {
"branchCode": "PARTDI",
"desc": "Part-Dieu"
},
"canCancel": true,
"canceled": false,
"displayExpiryDate": false,
"expiryDate": "",
"hasAuthor": true,
"hasAuthorExplore": true,
"hasIsbn": true,
"hasIssueCaption": false,
"hasTitle": true,
"hasVolume": false,
"isbn": "978-0-7148-7429-6",
"isbn10": "0714874299",
"isbn13": "9780714874296",
"issueId": "0",
"noticePrinted": false,
"omnidexId": "1717390",
"printStatus": "",
"priority": "10",
"rank": "1",
"resvDate": "20180626",
"seqNo": "2538241",
"statusCode": "RESV_NOT_AVAILABLE",
"subscriber": "27001003989263",
"title": "Avant-après [Livre] / Jean Jullien",
"volume": ""
},
"ilsType": "Portfolio",
"posInSet": 0,
"type": "reservations"
}
],
"total": 1
})
}
}
export class InmediaV2AccountInfoWithValidToken extends InmediaV2WithValidToken {
export class InMediaV2AccountInfo extends InMediaV2SignIn {
public setup(base_url) {
this
.expect({ url: base_url + '/in/rest/api/accountSummary',
headers: {"authorization": "Bearer zetoken"} },
{ content: this.accountInfoJSON() });
{ content: this.accountInfoJSON() })
}
}
export class InmediaV2LoansWithValidToken extends InmediaV2WithValidToken {
public setup(base_url: string) {
export class InMediaV2LoansAndHolds extends InMediaV2SignIn {
public setup(base_url) {
super.setup(base_url)
this
.expect({ url: base_url + '/in/rest/api/accountPage?type=loans&pageNo=1&pageSize=10&',
.expect({ url: base_url + '/in/rest/api/accountPage?type=loans&pageNo=1&pageSize=100',
headers: {"authorization": "Bearer zetoken"} },
{ content: this.loansJSON() });
{ content: this.loansJSON() })
.then({ url: base_url + '/in/rest/api/accountPage?type=reservations&pageNo=1&pageSize=100',
headers: {"authorization": "Bearer zetoken"} },
{ content: this.holdsJSON() })
}
}
declare var describe, expect, it, before, beforeEach, chai: any
import {Loan, Hold, Portal, Account, Database} from '../../models/'
import {InMediaV2} from '../../models/portal/inmedia-v2'
import {InMediaV2SignIn,
InMediaV2AccountInfo,
InMediaV2LoansAndHolds} from './inmedia-v2-fixtures'
describe('Account on InMedia V2', () => {
let db: Database
let account: Account
beforeEach( () => {
db = Database.open('testdb').clear();
account = (new Account())
.setUrl('https://inmediav2.fr')
.setCredentials({login: 'marco',
password: 'polo',
portal: 'inmedia-v2'})
db.save(account)
})
describe('refresh account', () => {
it('db should have one loan', () => {
let http = new InMediaV2LoansAndHolds('https://inmediav2.fr')
return (new InMediaV2()).setHTTP(http).refresh(account).then(() => {
let loans = db.findAll(Loan);
expect(loans).to.have.lengthOf(1)
expect(loans[0].getLoanId()).to.equals('1884668')
expect(loans[0].getTitle()).to.equals('La pomme rouge [Livre] / Kazuo Iwamura')
expect(loans[0].getAuthor()).to.equals('Iwamura, Kazuo, 1939-....')
expect(loans[0].getDateDue()).to.equals('2018-08-07')
expect(loans[0].getLibrary()).to.equals('Part-Dieu')
expect(loans[0].getRecordThumbnail()).to.equals('https://inmediav2.fr/in/rest/Thumb/image?isPhysical=1&isbn=9782211201025')
})
})
it('db should have one hold', () => {
let http = new InMediaV2LoansAndHolds('https://inmediav2.fr')
return (new InMediaV2()).setHTTP(http).refresh(account).then(() => {
let holds = db.findAll(Hold);
expect(holds).to.have.lengthOf(1)
expect(holds[0].getHoldId()).to.equals('2538241')
expect(holds[0].getTitle()).to.equals('Avant-après [Livre] / Jean Jullien')
expect(holds[0].getAuthor()).to.equals('Jullien, Jean, 1983-....')
expect(holds[0].getLibrary()).to.equals('Part-Dieu')
expect(holds[0].getStatus()).to.equals(L('hold_not_available'))
expect(holds[0].getRecordThumbnail()).to.equals('https://inmediav2.fr/in/rest/Thumb/image?isPhysical=1&isbn=9780714874296')
})
})
})
})
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