Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 1 | import {ActivatedRouteSnapshot, Resolve} from "@angular/router"; |
| 2 | import {Injectable} from "@angular/core"; |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame^] | 3 | import {from, Observable} from "rxjs"; |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 4 | import {AaiService} from "../../services/aaiService/aai.service"; |
| 5 | import {forkJoin} from "rxjs/observable/forkJoin"; |
| 6 | import {AppState} from "../../store/reducers"; |
| 7 | import {NgRedux} from "@angular-redux/store"; |
| 8 | import {createServiceInstance} from "../../storeUtil/utils/service/service.actions"; |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame^] | 9 | import {ServiceInstance} from "../../models/serviceInstance"; |
| 10 | import * as _ from "lodash"; |
| 11 | import {ModelInfo} from "../../models/modelInfo"; |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 12 | |
| 13 | @Injectable() |
| 14 | export class ViewEditResolver implements Resolve<Observable<boolean>> { |
| 15 | |
| 16 | constructor(private _aaiService: AaiService, private _store: NgRedux<AppState>) { |
| 17 | } |
| 18 | |
| 19 | resolve(route: ActivatedRouteSnapshot): Observable<boolean> { |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame^] | 20 | const serviceModelId: string = route.queryParamMap.get("serviceModelId"); |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 21 | const serviceInstanceId: string = route.queryParamMap.get("serviceInstanceId"); |
| 22 | const subscriberId: string = route.queryParamMap.get("subscriberId"); |
| 23 | const serviceType: string = route.queryParamMap.get("serviceType"); |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame^] | 24 | let serviceModelApi = this._aaiService.getServiceModelById(serviceModelId); |
| 25 | let serviceInstanceApi = this._aaiService.retrieveAndStoreServiceInstanceTopology(serviceInstanceId, subscriberId, serviceType, serviceModelId); |
| 26 | let streams: Observable<any>[] = [serviceModelApi, serviceInstanceApi]; |
| 27 | streams = streams.filter( stream => stream !== undefined); |
| 28 | return forkJoin(streams).switchMap(([serviceModel, serviceInstance]) => { |
| 29 | return from(this.retrieveLatestVersionAndSetServiceInstance(serviceInstance.modelInfo.modelInvariantId).then((response)=>{ |
| 30 | this.setServiceLatestAvailableVersion(serviceInstance, response); |
| 31 | this.applyRequestsResponsesToStateAndInitServiceInstance(serviceModelId, serviceInstance, serviceModel); |
| 32 | return true; |
| 33 | })); |
| 34 | |
| 35 | }); |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 36 | } |
| 37 | |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame^] | 38 | private retrieveLatestVersionAndSetServiceInstance(modelInvariantId: string) :Promise<ModelInfo>{ |
| 39 | return this._aaiService.retrieveServiceLatestUpdateableVersion(modelInvariantId).toPromise(); |
| 40 | } |
| 41 | |
| 42 | applyRequestsResponsesToStateAndInitServiceInstance(serviceModelId: string, |
| 43 | serviceInstance, serviceModel) { |
| 44 | this.setIsALaCarte(serviceInstance, serviceModel.service.vidNotions.instantiationType); |
| 45 | this.setTestApi(serviceInstance); |
| 46 | this._store.dispatch(createServiceInstance(serviceInstance, serviceModelId)); |
| 47 | } |
| 48 | |
| 49 | setServiceLatestAvailableVersion(serviceInstance :ServiceInstance, modelInfoObject: ModelInfo) :void{ |
| 50 | if(!_.isNil(modelInfoObject) && !_.isNil(modelInfoObject.modelVersion)){ |
| 51 | serviceInstance.latestAvailableVersion = Number(modelInfoObject.modelVersion); |
| 52 | } |
| 53 | else { |
| 54 | serviceInstance.latestAvailableVersion = 0; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | setTestApi(service: any) :void{ |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 59 | if (this._store.getState().global.flags['FLAG_ADD_MSO_TESTAPI_FIELD'] && service.isALaCarte) { |
| 60 | service.testApi = sessionStorage.getItem("msoRequestParametersTestApiValue"); |
| 61 | } |
| 62 | }; |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame^] | 63 | |
| 64 | setIsALaCarte(service: any, instantiationType) :void{ |
Ittay Stern | f792671 | 2019-07-07 19:23:03 +0300 | [diff] [blame] | 65 | service.isALaCarte = instantiationType === 'ALaCarte'; |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } |