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"; |
Eylon Malin | 5438f8b | 2019-09-26 10:04:17 +0300 | [diff] [blame] | 3 | import {from, Observable, of} 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"; |
Eylon Malin | 5438f8b | 2019-09-26 10:04:17 +0300 | [diff] [blame] | 12 | import {FeatureFlagsService, Features} from "../../services/featureFlag/feature-flags.service"; |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 13 | |
| 14 | @Injectable() |
| 15 | export class ViewEditResolver implements Resolve<Observable<boolean>> { |
| 16 | |
Eylon Malin | 5438f8b | 2019-09-26 10:04:17 +0300 | [diff] [blame] | 17 | constructor(private _aaiService: AaiService, |
| 18 | private featureFlagsService:FeatureFlagsService, |
| 19 | private _store: NgRedux<AppState>) { |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | resolve(route: ActivatedRouteSnapshot): Observable<boolean> { |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame] | 23 | const serviceModelId: string = route.queryParamMap.get("serviceModelId"); |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 24 | const serviceInstanceId: string = route.queryParamMap.get("serviceInstanceId"); |
| 25 | const subscriberId: string = route.queryParamMap.get("subscriberId"); |
| 26 | const serviceType: string = route.queryParamMap.get("serviceType"); |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame] | 27 | let serviceModelApi = this._aaiService.getServiceModelById(serviceModelId); |
| 28 | let serviceInstanceApi = this._aaiService.retrieveAndStoreServiceInstanceTopology(serviceInstanceId, subscriberId, serviceType, serviceModelId); |
| 29 | let streams: Observable<any>[] = [serviceModelApi, serviceInstanceApi]; |
| 30 | streams = streams.filter( stream => stream !== undefined); |
| 31 | return forkJoin(streams).switchMap(([serviceModel, serviceInstance]) => { |
Eylon Malin | 5438f8b | 2019-09-26 10:04:17 +0300 | [diff] [blame] | 32 | if (this.featureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE)) { |
| 33 | return from(this.retrieveLatestVersionAndSetServiceInstance(serviceInstance.modelInfo.modelInvariantId).then((response) => { |
| 34 | this.setServiceLatestAvailableVersion(serviceInstance, response); |
| 35 | this.applyRequestsResponsesToStateAndInitServiceInstance(serviceModelId, serviceInstance, serviceModel); |
| 36 | return true; |
| 37 | })); |
| 38 | } |
| 39 | else { |
| 40 | return of(true); |
| 41 | } |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame] | 42 | }); |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 43 | } |
| 44 | |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame] | 45 | private retrieveLatestVersionAndSetServiceInstance(modelInvariantId: string) :Promise<ModelInfo>{ |
| 46 | return this._aaiService.retrieveServiceLatestUpdateableVersion(modelInvariantId).toPromise(); |
| 47 | } |
| 48 | |
| 49 | applyRequestsResponsesToStateAndInitServiceInstance(serviceModelId: string, |
| 50 | serviceInstance, serviceModel) { |
| 51 | this.setIsALaCarte(serviceInstance, serviceModel.service.vidNotions.instantiationType); |
| 52 | this.setTestApi(serviceInstance); |
| 53 | this._store.dispatch(createServiceInstance(serviceInstance, serviceModelId)); |
| 54 | } |
| 55 | |
| 56 | setServiceLatestAvailableVersion(serviceInstance :ServiceInstance, modelInfoObject: ModelInfo) :void{ |
| 57 | if(!_.isNil(modelInfoObject) && !_.isNil(modelInfoObject.modelVersion)){ |
| 58 | serviceInstance.latestAvailableVersion = Number(modelInfoObject.modelVersion); |
| 59 | } |
| 60 | else { |
| 61 | serviceInstance.latestAvailableVersion = 0; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | setTestApi(service: any) :void{ |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 66 | if (this._store.getState().global.flags['FLAG_ADD_MSO_TESTAPI_FIELD'] && service.isALaCarte) { |
| 67 | service.testApi = sessionStorage.getItem("msoRequestParametersTestApiValue"); |
| 68 | } |
| 69 | }; |
Einat Vinouze | e1f7974 | 2019-08-27 16:01:01 +0300 | [diff] [blame] | 70 | |
| 71 | setIsALaCarte(service: any, instantiationType) :void{ |
Ittay Stern | f792671 | 2019-07-07 19:23:03 +0300 | [diff] [blame] | 72 | service.isALaCarte = instantiationType === 'ALaCarte'; |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } |