Commit 6c2cbe13 authored by Laurent's avatar Laurent

synchronize portals descriptions from MBManager

parent bc6941dd
......@@ -40,8 +40,15 @@ export class LibraryController extends Controller {
public constructor(account: Account) {
super();
this._account = account;
this._portal = new Portal();
// (new Manager())
// .connect(account)
// .then( () => {
// Database.current().save(this._account);
// });
this._portal = new Portal();
this._loadLoansAndHolds();
this.set('loans', this._loans);
this.set('holds', this._holds);
......
import { HTTPClient, NSHTTPClient, Account } from '../models';
import { HTTPClient,
NSHTTPClient,
Account,
DataSource } from '../models';
import * as UrlParse from 'url-parse';
import {MBPortal} from './manager/mbportal';
export {MBPortal};
......@@ -25,16 +29,22 @@ export class Manager {
}
public connect(account:Account, callback:any): Promise<any> {
return this._http
.request({ url: Manager._url + '/api/configuration',
method: "POST",
headers: { "Content-Type": "application/json" },
content: JSON.stringify( { url: account.getUrl() } )
})
public connect(db: DataSource, account:Account): Promise<MBPortal> {
let portal = MBPortal.findByURL(db, account.getUrl());
if (portal == undefined)
portal = new MBPortal().setURL(account.getUrl());
return this._http
.request({ url: Manager._url + '/api/portal/' + portal.getKey(),
headers: { "Content-Type": "application/json" } })
.then( (response) => {
let datas = response.content.toJSON();
callback(datas);
portal
.setLabel(datas.label)
.setURL(datas.url)
.setFeatures(datas.features);
db.save(portal);
return portal;
});
}
......@@ -45,7 +55,9 @@ export class Manager {
.then( (response) => {
let datas = response.content.toJSON();
let portals = datas.map( (portal) => {
return new MBPortal(portal.label, portal.url);
return new MBPortal()
.setLabel(portal.label)
.setURL(portal.url);
});
return Promise.resolve(portals);
......
export class MBPortal {
protected _label: string;
protected _url: string;
import {Persistable,
Serializable,
DataSource} from '../';
import * as UrlParse from 'url-parse';
export class MBPortal extends Persistable {
protected _label: string = '';
protected _url: string = '';
protected _features: Array<Object> = new Array<Object>();
public static findByURL(db:DataSource, url:string): MBPortal {
return db.findAll(MBPortal).find( (portal:MBPortal) => {
return portal.keyMatches(url);
});
}
public constructor(label:string, url:string) {
public getLabel(): string {
return this._label;
}
public getKey(): string {
return this._hostname(this._url);
}
protected _hostname(url: string): string {
return (new UrlParse(url)).host;
}
public keyMatches(url: string): boolean {
return this.getKey() == this._hostname(url);
}
public setLabel(label:string): this {
this._label = label;
return this;
}
public setURL(url:string): this {
this._url = url;
return this;
}
public getURL(): string {
return this._url;
}
public setFeatures(features:Array<Object>): this {
this._features = features;
return this;
}
get label(): string {
return this._label;
}
get sip_loan_feature(): boolean {
return undefined != this._features.find( (feature) => {
return feature['code'] == 'SIP_LOAN';
});
}
get url(): string {
return this._url;
}
public serializeOn(serializable: Serializable) {
serializable
.set('label', this._label)
.set('url', this._url)
}
public materializeFrom(serializable: Serializable) {
this._label = serializable.get('label');
this._url = serializable.get('url');
}
}
declare var describe, expect, it, before, beforeEach, chai: any;
import { Manager } from '../models/';
import { Manager, Database, MBPortal, Account } from '../models/';
import { HTTPScenario } from './portal/http-scenario';
export class ManagerWithTwoPortals extends HTTPScenario {
......@@ -30,15 +30,69 @@ export class ManagerWithTwoPortals extends HTTPScenario {
export class ManagerConnectAnnecy extends HTTPScenario {
public constructor(base_url: string) {
super();
this
.expect({ url: base_url + '/api/portal/library.annecy.fr',
headers: { "Content-Type": "application/json" } },
{ content: this.annecyJSON() });
}
public annecyJSON(): string {
let annecy = {
"label": "Annecy",
"url": "http://library.annecy.fr",
"features": [{ "code": "SIP_LOAN" }]
};
return JSON.stringify(annecy);
}
}
describe('Manager', () => {
let manager: Manager;
let db: Database;
beforeEach(() => {
db = Database.open('testdb').clear();
Manager.url('http://mbam.org');
manager = new Manager();
});
describe('on annecy account', () => {
let annecy: Account;
beforeEach( () => {
annecy = (new Account()).setUrl('http://library.annecy.fr');
manager.setHTTP(new ManagerConnectAnnecy('http://mbam.org'));
});
it('with no existing portal it should create it with its features', () => {
return manager.connect(db, annecy).then( (portal) => {
expect(portal.getLabel()).to.equals('Annecy');
expect(portal.getURL()).to.equals('http://library.annecy.fr');
expect(portal.getKey()).to.equals('library.annecy.fr');
expect(portal.sip_loan_feature).to.be.true;
});
})
it('with existing portal assigned it should update it', () => {
let portal = new MBPortal().setURL('http://library.annecy.fr');
db.save(portal);
return manager.connect(db, annecy).then( (updated) => {
expect(updated.getId()).to.equals(portal.getId());
});
})
});
describe('on server with two portals', () => {
beforeEach( () => {
let http = new ManagerWithTwoPortals('http://mbam.org');
......
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