-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds local fields on document, item and holdings (serial type). * Adds holdings informations on the detail screen. Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
- Loading branch information
1 parent
080c958
commit 30cf42f
Showing
23 changed files
with
877 additions
and
139 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
projects/admin/src/app/api/local-field-api.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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 { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { RecordService, RecordUiService } from '@rero/ng-core'; | ||
import { of } from 'rxjs'; | ||
import { LocalFieldApiService } from './local-field-api.service'; | ||
|
||
describe('Service: LocalFieldApi', () => { | ||
|
||
let localFieldApiService: LocalFieldApiService; | ||
|
||
const emptyRecords = { | ||
aggregations: {}, | ||
hits: { | ||
total: { | ||
relation: 'eq', | ||
value: 0 | ||
}, | ||
hits: [] | ||
}, | ||
links: {} | ||
}; | ||
|
||
const recordServiceSpy = jasmine.createSpyObj('RecordService', ['getRecords', 'totalHits']); | ||
recordServiceSpy.getRecords.and.returnValue(of(emptyRecords)); | ||
recordServiceSpy.totalHits.and.returnValue(0); | ||
|
||
const recordUiServiceSpy = jasmine.createSpyObj('RecordUiService', ['deleteRecord']); | ||
recordUiServiceSpy.deleteRecord.and.returnValue(of(true)); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
HttpClientTestingModule, | ||
RouterTestingModule | ||
], | ||
providers: [ | ||
LocalFieldApiService, | ||
{ provide: RecordService, useValue: recordServiceSpy }, | ||
{ provide: RecordUiService, useValue: recordUiServiceSpy }, | ||
] | ||
}); | ||
localFieldApiService = TestBed.get(LocalFieldApiService); | ||
}); | ||
|
||
it('should create a service', () => { | ||
expect(localFieldApiService).toBeTruthy(); | ||
}); | ||
|
||
it('should return an empty object', () => { | ||
localFieldApiService | ||
.getByResourceTypeAndResourcePidAndOrganisationId('doc', '1', '1') | ||
.subscribe(result => { | ||
expect('metadata' in result).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
it('should return true on the deletion of the record', () => { | ||
localFieldApiService | ||
.deleteByPid('1').subscribe((success: boolean) => { | ||
expect(success).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, RecordUiService } from '@rero/ng-core'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class LocalFieldApiService { | ||
|
||
/** | ||
* Constructor | ||
* @param _recordService - RecordService | ||
*/ | ||
constructor( | ||
private _recordService: RecordService, | ||
private _recordUiService: RecordUiService | ||
) { } | ||
|
||
/** | ||
* Get Local field for current resource and organisation user | ||
* @param resourceType - string, type of resource | ||
* @param resourcePid - string, pid of resource | ||
* @param organisationPid - string, pid of organisation | ||
* @return Observable | ||
*/ | ||
getByResourceTypeAndResourcePidAndOrganisationId( | ||
resourceType: string, | ||
resourcePid: string, | ||
organisationPid: string | ||
) { | ||
const query = [ | ||
`parent.type:${resourceType}`, | ||
`parent.pid:${resourcePid}`, | ||
`organisation.pid:${organisationPid}`, | ||
].join(' AND '); | ||
|
||
return this._recordService.getRecords('local_fields', query, 1, 1).pipe( | ||
map((result: any) => { | ||
return this._recordService.totalHits(result.hits.total) > 0 | ||
? result.hits.hits[0] | ||
: {}; | ||
}) | ||
); | ||
} | ||
|
||
/** | ||
* Delete local fields by resource pid | ||
* @param resourcePid - string, pid of resource | ||
* @return Observable | ||
*/ | ||
deleteByPid(resourcePid: string) { | ||
return this._recordUiService.deleteRecord('local_fields', resourcePid); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
projects/admin/src/app/guard/can-add-local-fields.guard.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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 { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { LocalStorageService } from '@rero/ng-core'; | ||
import { of } from 'rxjs'; | ||
import { LocalFieldApiService } from '../api/local-field-api.service'; | ||
import { CanAddLocalFieldsGuard } from './can-add-local-fields.guard'; | ||
|
||
|
||
describe('CanAddLocalFieldsGuard', () => { | ||
|
||
let canAddLocalFieldsGuard: CanAddLocalFieldsGuard; | ||
|
||
const localFieldsApiServiceSpy = jasmine.createSpyObj( | ||
'LocalFieldApiService', [ | ||
'getByResourceTypeAndResourcePidAndOrganisationId' | ||
] | ||
); | ||
localFieldsApiServiceSpy | ||
.getByResourceTypeAndResourcePidAndOrganisationId | ||
.and.returnValue(of({})); | ||
|
||
const localStorageServiceSpy = jasmine.createSpyObj('LocalStorageService', ['get']); | ||
localStorageServiceSpy.get.and.returnValue({ | ||
library: { | ||
organisation: { | ||
pid: 1 | ||
} | ||
} | ||
}); | ||
|
||
const activatedRouteSnapshotSpy = jasmine.createSpyObj('ActivatedRouteSnapshot', ['']); | ||
activatedRouteSnapshotSpy.queryParams = {type: 'documents', ref: '240'}; | ||
|
||
const routerStateSnapshotSpy = jasmine.createSpyObj('RouterStateSnapshot', ['']); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
HttpClientTestingModule, | ||
RouterTestingModule | ||
], | ||
providers: [ | ||
CanAddLocalFieldsGuard, | ||
{ provide: LocalFieldApiService, useValue: localFieldsApiServiceSpy }, | ||
{ provide: LocalStorageService, useValue: localStorageServiceSpy }, | ||
] | ||
}); | ||
canAddLocalFieldsGuard = TestBed.get(CanAddLocalFieldsGuard); | ||
}); | ||
|
||
it('should create a service', () => { | ||
expect(canAddLocalFieldsGuard).toBeTruthy(); | ||
}); | ||
|
||
it('should return true if the url parameters are right', () => { | ||
expect(canAddLocalFieldsGuard.canActivate( | ||
activatedRouteSnapshotSpy, routerStateSnapshotSpy | ||
)).toBeTruthy(); | ||
}); | ||
}); |
96 changes: 96 additions & 0 deletions
96
projects/admin/src/app/guard/can-add-local-fields.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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 { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; | ||
import { LocalStorageService } from '@rero/ng-core'; | ||
import { User } from '@rero/shared'; | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { LocalFieldApiService } from '../api/local-field-api.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class CanAddLocalFieldsGuard implements CanActivate { | ||
|
||
/** Available type of document to add local fields */ | ||
private types = { | ||
documents: 'doc', | ||
holdings: 'hold', | ||
items: 'item' | ||
}; | ||
|
||
/** | ||
* Constructor | ||
* @param _localeStorageService - LocalStorageService | ||
* @param _localFieldsApiService - LocalFieldApiService | ||
* @param _router - Router | ||
*/ | ||
constructor( | ||
private _localeStorageService: LocalStorageService, | ||
private _localFieldsApiService: LocalFieldApiService, | ||
private _router: Router | ||
) {} | ||
|
||
/** | ||
* Can activate | ||
* @param next - ActivatedRouteSnapshot | ||
* @param state - RouterStateSnapshot | ||
*/ | ||
canActivate( | ||
next: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { | ||
|
||
const params = next.queryParams; | ||
if (params.type && params.ref) { | ||
/* | ||
* Use of User local storage, because the loading of the routing | ||
* is done before the load of the application. | ||
*/ | ||
const userLocale: User = this._localeStorageService.get(User.STORAGE_KEY); | ||
if (userLocale) { | ||
const organisationPid = userLocale.library.organisation.pid; | ||
return this._localFieldsApiService.getByResourceTypeAndResourcePidAndOrganisationId( | ||
this._translateType(params.type), | ||
params.ref, | ||
organisationPid | ||
).pipe(map(record => { | ||
return record.metadata ? false : true; | ||
})); | ||
} | ||
this._router.navigate(['/errors/401'], { skipLocationChange: true }); | ||
} else { | ||
this._router.navigate(['/errors/400'], { skipLocationChange: true }); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Translate type with a symbol | ||
* @param type - string, resource type | ||
* @return string - translated type | ||
* @throws redirect to error 400 | ||
*/ | ||
private _translateType(type: string) { | ||
if (type in this.types) { | ||
return this.types[type]; | ||
} | ||
this._router.navigate(['/errors/400'], { skipLocationChange: true }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.