diff --git a/src/app/modules/library/account/library-account.service.ts b/src/app/modules/library/account/library-account.service.ts index 3499e12e..d7972a14 100644 --- a/src/app/modules/library/account/library-account.service.ts +++ b/src/app/modules/library/account/library-account.service.ts @@ -1,53 +1,63 @@ import {Injectable} from '@angular/core'; -// import {ConfigProvider} from '../../config/config.provider'; -import {PAIAAuthService} from '../../auth/paia/paia-auth.service'; import {JQueryRequestor, Requestor} from '@openid/appauth'; -import {SCFeatureConfigurationExtern, SCMap} from '@openstapps/core'; +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 { - // TODO: must come from backend (not that stable) - private _config: SCMap = { - profile: { - url: 'https://hds.hebis.de:8443/core/{patron}', - }, - items: { - url: 'https://hds.hebis.de:8443/core/{patron}/items', - }, - fees: { - url: 'https://hds.hebis.de:8443/core/{patron}/fees', - }, - }; + /** + * Base url of the external service + */ + baseUrl: string; + + /** + * Authorization provider type + */ + authType: SCAuthorizationProviderType; constructor( protected requestor: Requestor = new JQueryRequestor(), - private readonly paiaAuth: PAIAAuthService, private readonly hebisDataProvider: HebisDataProvider, - ) {} - - get config() { - return this._config; + private readonly authHelper: AuthHelperService, + readonly configProvider: ConfigProvider, + ) { + 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(this.config.profile.url); + return this.performRequest(`${this.baseUrl}/{patron}`); } async getItems() { - return this.performRequest(this.config.items.url); + return this.performRequest(`${this.baseUrl}/{patron}/items`); } async getFees() { - return this.performRequest(this.config.fees.url); + return this.performRequest(`${this.baseUrl}/{patron}/fees`); } private async performRequest(urlTemplate: string): Promise { - const token = await this.paiaAuth.getValidToken(); - const url = urlTemplate.replace('{patron}', token.patron); + 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',