mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 17:12:43 +00:00
123 lines
3.6 KiB
TypeScript
123 lines
3.6 KiB
TypeScript
/*
|
|
* Copyright (C) 2022 StApps
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
* Software Foundation, version 3.
|
|
*
|
|
* 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 General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import {Injectable} from '@angular/core';
|
|
import {JQueryRequestor, Requestor} from '@openid/appauth';
|
|
import {
|
|
SCAuthorizationProviderType,
|
|
SCFeatureConfiguration,
|
|
SCFeatureConfigurationExtern,
|
|
} from '@openstapps/core';
|
|
import {PAIADocumentStatus, PAIAFees, PAIAItems, PAIAPatron} from '../types';
|
|
import {HebisDataProvider} from '../../hebis/hebis-data.provider';
|
|
import {ConfigProvider} from '../../config/config.provider';
|
|
import {AuthHelperService} from '../../auth/auth-helper.service';
|
|
import {PAIATokenResponse} from '../../auth/paia/paia-token-response';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class LibraryAccountService {
|
|
/**
|
|
* Base url of the external service
|
|
*/
|
|
baseUrl: string;
|
|
|
|
/**
|
|
* Authorization provider type
|
|
*/
|
|
authType: SCAuthorizationProviderType;
|
|
|
|
constructor(
|
|
protected requestor: Requestor = new JQueryRequestor(),
|
|
private readonly hebisDataProvider: HebisDataProvider,
|
|
private readonly authHelper: AuthHelperService,
|
|
readonly configProvider: ConfigProvider,
|
|
) {
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
const config: SCFeatureConfigurationExtern = (
|
|
configProvider.getValue('features') as SCFeatureConfiguration
|
|
).extern!.paia;
|
|
this.baseUrl = config.url;
|
|
this.authType = config.authProvider as SCAuthorizationProviderType;
|
|
}
|
|
|
|
async getProfile() {
|
|
return this.performRequest<PAIAPatron>(`${this.baseUrl}/{patron}`);
|
|
}
|
|
|
|
async getItems() {
|
|
return this.performRequest<PAIAItems>(`${this.baseUrl}/{patron}/items`);
|
|
}
|
|
|
|
async getFees() {
|
|
return this.performRequest<PAIAFees>(`${this.baseUrl}/{patron}/fees`);
|
|
}
|
|
|
|
private async performRequest<T>(urlTemplate: string): Promise<T> {
|
|
const token = await this.authHelper
|
|
.getProvider(this.authType)
|
|
.getValidToken();
|
|
const url = urlTemplate.replace(
|
|
'{patron}',
|
|
(token as PAIATokenResponse).patron,
|
|
);
|
|
const settings: JQueryAjaxSettings = {
|
|
url: url,
|
|
dataType: 'json',
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': `Bearer: ${token.accessToken}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
};
|
|
|
|
return this.requestor.xhr(settings);
|
|
}
|
|
|
|
getRawId(input: string) {
|
|
return input.split(':').pop();
|
|
}
|
|
|
|
async getHoldsAndReservations() {
|
|
return (await this.getItems()).doc.filter(document => {
|
|
return [PAIADocumentStatus.Reserved, PAIADocumentStatus.Ordered].includes(
|
|
Number(document.status),
|
|
);
|
|
});
|
|
}
|
|
|
|
async getCheckedOutItems() {
|
|
return (await this.getItems()).doc.filter(document => {
|
|
return PAIADocumentStatus.Held === Number(document.status);
|
|
});
|
|
}
|
|
|
|
async getDocumentFromHDS(edition: string) {
|
|
if (typeof edition === 'undefined') {
|
|
return;
|
|
}
|
|
const response = await this.hebisDataProvider.hebisSearch(
|
|
{
|
|
query: this.getRawId(edition) as string,
|
|
page: 0,
|
|
},
|
|
{addPrefix: true},
|
|
);
|
|
|
|
return response.data[0];
|
|
}
|
|
}
|