app/core/guards/opc-shortlink-guard.ts
This guard is used to resolve a OPC short link url and redirect to the given product's Product Detail Page.
Properties |
Methods |
|
constructor(router: Router, searchFactory: SearchFactory, opcService: OpcService, cacheService: ClientSideCacheService, constants: ConstantsService, util: UtilService)
|
|||||||||||||||||||||
Defined in app/core/guards/opc-shortlink-guard.ts:20
|
|||||||||||||||||||||
Parameters :
|
canActivate | |||||||||
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot)
|
|||||||||
Defined in app/core/guards/opc-shortlink-guard.ts:33
|
|||||||||
Parameters :
Returns :
Promise<boolean>
|
Private getProductFamilyId | ||||
getProductFamilyId(designation)
|
||||
Defined in app/core/guards/opc-shortlink-guard.ts:79
|
||||
Search for given designation and extract it's product family ID
Parameters :
Returns :
Promise<string>
|
Private goToSiteSearch | ||||
goToSiteSearch(designation)
|
||||
Defined in app/core/guards/opc-shortlink-guard.ts:99
|
||||
Parameters :
Returns :
void
|
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { SearchFactory, SearchService, HttpParams, SimpleSearchResult } from 'skf-search-angular-service';
import { AppConfig } from 'src/app/app.config';
import { OpcService } from '../services/opc-service/opc-service.service';
import { ClientSideCacheService } from '../client-side-cache/client-side-cache.service';
import { ConstantsService } from 'src/app/shared/constants/constants.service';
import { UtilService } from '../services/util-service/util.service';
import { PublicationService } from '../services/publication-service/publication.service';
/**
* This guard is used to resolve a OPC short link url and redirect to the given product's Product Detail Page.
*/
@Injectable()
export class OpcShortLinkGuard implements CanActivate {
opcSearcher: SearchService;
language: string;
pubPath: string;
system: string;
constructor(
private router: Router,
private searchFactory: SearchFactory,
private opcService: OpcService,
private cacheService: ClientSideCacheService,
private constants: ConstantsService,
private util: UtilService
) {
this.opcSearcher = this.searchFactory.get(`opc-${AppConfig.settings.searchEnvironment.name}`);
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
const designation = next.queryParams['designation'];
this.language = next.queryParams['language'] || 'en'; // Default to english if no language is given.
this.system = next.queryParams['system'] || 'metric'; // Default to metric.
this.pubPath = this.constants.getCountry(this.language);
return new Promise<boolean>((resolve, reject) => {
this.getProductFamilyId(designation).then(res => {
const productFamilyId = res;
if (!res) {
this.goToSiteSearch(designation);
}
// Get publication ID
this.cacheService.getValue(`${this.pubPath}-pubId`, `content/api/publication?path=${this.pubPath}`)
.toPromise().then(pubId => {
if(!pubId) {
this.goToSiteSearch(designation);
reject(false);
}
this.opcService.getPGPUrlWithPath(productFamilyId, this.pubPath, pubId).then(url => {
if (url) {
const des = encodeURIComponent(designation);
this.router.navigate([`${url}/productid-${des}`], {
queryParams: { system: this.system }
});
} else {
// If family group page was not found, redirect to Site Search on group site.
this.goToSiteSearch(designation);
}
resolve(true);
}, err => {
// If family group page was not found, redirect to Site Search on group site.
this.goToSiteSearch(designation);
});
});
});
});
}
/** Search for given designation and extract it's product family ID */
private getProductFamilyId(designation): Promise<string> {
const opcString = `designation=${designation}&searcher=details&language=${this.language}&system=${this.system}`;
return new Promise((resolve, reject) => {
this.opcSearcher.result.subscribe(res => {
const documents = this.util.extract(res, 'documentList', 'documents') || [];
if (documents.length === 0) { // Redirect to site search page if search gives no result.
this.goToSiteSearch(designation);
} else {
const familyId = documents[0].structure_group_id;
resolve(familyId);
}
});
this.opcSearcher.doSearchWithParameters({
params: new HttpParams({ 'fromString': opcString }),
});
});
}
private goToSiteSearch(designation) {
this.router.navigate([`${this.pubPath}/search-results`], {
queryParams: { 'q': designation, 'system': this.system }
});
}
}