summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/resolvers/viewEdit/viewEdit.resolver.ts
blob: d2f76e4f647daabf2982b741cb1427184f9cd390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {ActivatedRouteSnapshot, Resolve} from "@angular/router";
import {Injectable} from "@angular/core";
import {from, Observable} from "rxjs";
import {AaiService} from "../../services/aaiService/aai.service";
import {forkJoin} from "rxjs/observable/forkJoin";
import {AppState} from "../../store/reducers";
import {NgRedux} from "@angular-redux/store";
import {createServiceInstance} from "../../storeUtil/utils/service/service.actions";
import {ServiceInstance} from "../../models/serviceInstance";
import * as _ from "lodash";
import {ModelInfo} from "../../models/modelInfo";

@Injectable()
export class ViewEditResolver implements Resolve<Observable<boolean>> {

  constructor(private _aaiService: AaiService, private _store: NgRedux<AppState>) {
  }

  resolve(route: ActivatedRouteSnapshot): Observable<boolean> {
    const serviceModelId: string = route.queryParamMap.get("serviceModelId");
    const serviceInstanceId: string = route.queryParamMap.get("serviceInstanceId");
    const subscriberId: string = route.queryParamMap.get("subscriberId");
    const serviceType: string = route.queryParamMap.get("serviceType");
    let serviceModelApi = this._aaiService.getServiceModelById(serviceModelId);
    let serviceInstanceApi = this._aaiService.retrieveAndStoreServiceInstanceTopology(serviceInstanceId, subscriberId, serviceType, serviceModelId);
    let streams: Observable<any>[] = [serviceModelApi, serviceInstanceApi];
    streams = streams.filter( stream => stream !== undefined);
    return forkJoin(streams).switchMap(([serviceModel, serviceInstance]) => {
      return from(this.retrieveLatestVersionAndSetServiceInstance(serviceInstance.modelInfo.modelInvariantId).then((response)=>{
        this.setServiceLatestAvailableVersion(serviceInstance, response);
        this.applyRequestsResponsesToStateAndInitServiceInstance(serviceModelId, serviceInstance, serviceModel);
        return true;
      }));

    });
  }

  private retrieveLatestVersionAndSetServiceInstance(modelInvariantId: string) :Promise<ModelInfo>{
    return this._aaiService.retrieveServiceLatestUpdateableVersion(modelInvariantId).toPromise();
  }

  applyRequestsResponsesToStateAndInitServiceInstance(serviceModelId: string,
                                                      serviceInstance, serviceModel) {
    this.setIsALaCarte(serviceInstance, serviceModel.service.vidNotions.instantiationType);
    this.setTestApi(serviceInstance);
    this._store.dispatch(createServiceInstance(serviceInstance, serviceModelId));
  }

  setServiceLatestAvailableVersion(serviceInstance :ServiceInstance, modelInfoObject: ModelInfo) :void{
    if(!_.isNil(modelInfoObject) && !_.isNil(modelInfoObject.modelVersion)){
      serviceInstance.latestAvailableVersion = Number(modelInfoObject.modelVersion);
    }
    else {
      serviceInstance.latestAvailableVersion = 0;
    }
  }

  setTestApi(service: any) :void{
    if (this._store.getState().global.flags['FLAG_ADD_MSO_TESTAPI_FIELD'] && service.isALaCarte) {
      service.testApi = sessionStorage.getItem("msoRequestParametersTestApiValue");
    }
  };

  setIsALaCarte(service: any, instantiationType) :void{
    service.isALaCarte = instantiationType === 'ALaCarte';
  };

}