Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service: add new service to load organisation before application start #114

Merged
merged 1 commit into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions projects/admin/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Router, NavigationStart } from '@angular/router';
import { filter } from 'rxjs/operators';
import { LibrarySwitchService } from './service/library-switch.service';
import { Subscription } from 'rxjs';
import { OrganisationService } from './service/organisation.service';

@Component({
selector: 'admin-root',
Expand Down Expand Up @@ -61,7 +62,8 @@ export class AppComponent implements OnInit, OnDestroy {
private translateService: TranslateService,
private localStorageService: LocalStorageService,
private librarySwitchService: LibrarySwitchService,
private router: Router
private router: Router,
private organisationService: OrganisationService
) {
this.initializeEvents();
}
Expand Down Expand Up @@ -101,21 +103,28 @@ export class AppComponent implements OnInit, OnDestroy {
);
}
if (this.access) {
if (!this.localStorageService.has(User.STORAGE_KEY)) {
this.localStorageService.set(User.STORAGE_KEY, user);
} else {
const userLocal = this.localStorageService.get(User.STORAGE_KEY);
if (userLocal.pid !== user.pid) {
this.organisationService.loadOrganisationByPid(
user.library.organisation.pid
);
this.organisationService.onOrganisationLoaded.subscribe(() => {
if (!this.localStorageService.has(User.STORAGE_KEY)) {
this.localStorageService.set(User.STORAGE_KEY, user);
} else {
const userLocal = this.localStorageService.get(User.STORAGE_KEY);
if (userLocal.pid !== user.pid) {
this.localStorageService.set(User.STORAGE_KEY, user);
}
const locale = this.localStorageService.get(User.STORAGE_KEY);
user.setCurrentLibrary(locale.currentLibrary);
}
const locale = this.localStorageService.get(User.STORAGE_KEY);
user.setCurrentLibrary(locale.currentLibrary);
}
this.librarySwitchService.switch(
user.getCurrentLibrary()
);
this.librarySwitchService.switch(
user.getCurrentLibrary()
);
this.user = user;
});
} else {
this.user = user;
}
this.user = user;
}));

this._subcription.add(this.router.events.pipe(
Expand Down
33 changes: 33 additions & 0 deletions projects/admin/src/app/service/organisation.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* RERO ILS UI
* Copyright (C) 2020 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { TestBed } from '@angular/core/testing';

import { OrganisationService } from './organisation.service';
import { HttpClientModule } from '@angular/common/http';

describe('OrganisationService', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [
HttpClientModule
]
}));

it('should be created', () => {
const service: OrganisationService = TestBed.get(OrganisationService);
expect(service).toBeTruthy();
});
});
71 changes: 71 additions & 0 deletions projects/admin/src/app/service/organisation.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* RERO ILS UI
* Copyright (C) 2020 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Injectable } from '@angular/core';
import { RecordService } from '@rero/ng-core';
import { Subject } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
})
export class OrganisationService {

/**
* Observable on Record Organisation
*/
private _onOrganisationLoaded: Subject<any> = new Subject();

/**
* Organisation record
*/
private _record: any;

/**
* return observable of organisation
*/
get onOrganisationLoaded() {
return this._onOrganisationLoaded.asObservable();
}

/**
* get current organisation
*/
get organisation() {
return this._record;
}

/**
* Constructor
* @param recordService - RecordService
*/
constructor(private _recordService: RecordService) { }

/**
* Load organisation record
* @param pid - string
*/
loadOrganisationByPid(pid: string) {
this._recordService.getRecord('organisations', pid)
.pipe(
map((record: any) => this._record = record.metadata)
)
.subscribe((organisation: any) => {
this._record = organisation;
this._onOrganisationLoaded.next(organisation);
});
}
}