aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/service.control.generator.ts
blob: 539f848d5d0b7f9166d57426a98d92268afaa66f (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import {Injectable} from "@angular/core";
import {GenericFormService} from "../generic-form.service";
import {NgRedux} from "@angular-redux/store";
import {AppState} from "../../../store/reducers";
import {
  FormControlModel,
  ValidatorModel,
  ValidatorOptions
} from "../../../models/formControlModels/formControl.model";
import {DropdownFormControl} from "../../../models/formControlModels/dropdownFormControl.model";
import * as _ from 'lodash';
import {BasicControlGenerator} from "./basic.control.generator";
import {AaiService} from "../../../services/aaiService/aai.service";
import {FormGroup} from "@angular/forms";
import {FormControlType} from "../../../models/formControlModels/formControlTypes.enum";
import {HttpClient} from "@angular/common/http";
import {SelectOption} from "../../../models/selectOption";
import {Observable} from "rxjs";
import {LogService} from "../../../utils/log/log.service";
import {ServiceModel} from "../../../models/serviceModel";
import {of} from "rxjs";

import {CheckboxFormControl} from "../../../models/formControlModels/checkboxFormControl.model";

export enum FormControlNames {
  INSTANCE_NAME = 'instanceName',
  GLOBAL_SUBSCRIBER_ID = 'globalSubscriberId',
  SUBSCRIPTION_SERVICE_TYPE = 'subscriptionServiceType',
  PRODUCT_FAMILY_ID = 'productFamilyId',
  LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
  TENANT_ID = 'tenantId',
  AICZONE_ID = 'aicZoneId',
  PROJECT_NAME = 'projectName',
  OWNING_ENTITY_ID = 'owningEntityId',
  ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
  PAUSE = 'pause'
}

@Injectable()
export class  ServiceControlGenerator {
  aaiService : AaiService;
  constructor(private genericFormService : GenericFormService,
              private _basicControlGenerator : BasicControlGenerator,
              private store: NgRedux<AppState>,
              private http: HttpClient,
              private _aaiService : AaiService,
              private _logService : LogService){
    this.aaiService = _aaiService;
  }

  getServiceInstance = (serviceId : string) : any => {
    let serviceInstance = null;
    if (_.has(this.store.getState().service.serviceInstance, serviceId)) {
      serviceInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId]);
    }

    return serviceInstance;
  };

  getAlaCartControls(serviceId: string, dynamicInputs?: any[]) : FormControlModel[] {
    if(_.isNil(serviceId)){
      this._logService.error('should provide serviceId', serviceId);
      return [];
    }
    const serviceInstance = this.getServiceInstance(serviceId);

    let result : FormControlModel[] = [];

    const serviceModel = new ServiceModel(this.store.getState().service.serviceHierarchy[serviceId]);
    if(!_.isNil(serviceModel)){
      result.push(this._basicControlGenerator.getInstanceName(serviceInstance, serviceId, serviceModel.isEcompGeneratedNaming));
      result.push(this.getGlobalSubscriberControl(serviceInstance, result));
      result.push(this.getServiceTypeControl(serviceInstance, result, false));
      result.push(this.getOwningEntityControl(serviceInstance, result));
      result.push(this.getProjectControl(serviceInstance, result));
      result.push(this.getRollbackOnFailureControl(serviceInstance, result));
    }

    this._logService.info('Generate dynamic service controls, is edit mode: ' + serviceInstance != null , result);
    return result;
  }

  getMacroFormControls(serviceId: string, dynamicInputs?: any[]) : FormControlModel[] {
    if(_.isNil(serviceId)){
      this._logService.error('should provide serviceId', serviceId);
      return [];
    }

    const serviceInstance = this.getServiceInstance(serviceId);

    let result : FormControlModel[] = [];
    const serviceModel = new ServiceModel(this.store.getState().service.serviceHierarchy[serviceId]);
    if(!_.isNil(serviceModel)){
      result.push(this._basicControlGenerator.getInstanceName(serviceInstance, serviceId, serviceModel.isEcompGeneratedNaming));
      result.push(this.getGlobalSubscriberControl(serviceInstance, result));
      result.push(this.getServiceTypeControl(serviceInstance, result, true));
      result.push(this.getOwningEntityControl(serviceInstance, result));
      result.push(this.getProductFamilyControl(serviceInstance, result));
      result.push(this.getLcpRegionControl(serviceInstance, result));
      result.push(this.getTenantControl(serviceInstance, result),);
      result.push(this.getAICZoneControl(serviceInstance, result));

      if(serviceModel.isMultiStepDesign){
        result.push(new CheckboxFormControl({
          controlName : FormControlNames.PAUSE,
          displayName : 'Pause on pause points',
          dataTestId : 'Pause',
          isDisabled : false,
          validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
          value : serviceInstance ? serviceInstance.pause : null,
        }));
      }

      result.push(this.getProjectControl(serviceInstance, result));
      result.push(this.getRollbackOnFailureControl(serviceInstance, result));
    }


    this._logService.info('Generate dynamic service controls, is edit mode: ' + serviceInstance != null , result);
    return result;
  }

  getRollBackOnFailureOptions = () : Observable<SelectOption[]> =>{
    return of([
      new SelectOption({id: 'true', name: 'Rollback'}),
      new SelectOption({id: 'false', name: 'Don\'t Rollback'})
    ]);
  };

  getGlobalSubscriberControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl  => {
    return new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : FormControlNames.GLOBAL_SUBSCRIBER_ID,
      displayName : 'Subscriber name',
      dataTestId : 'subscriberName',
      placeHolder : 'Select Subscriber Name',
      isDisabled : false,
      name : "subscriber-name-select",
      value : serviceInstance ? serviceInstance.globalSubscriberId : null,
      validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
      onInit : this._basicControlGenerator.getSubscribeInitResult.bind(this._aaiService, this.aaiService.getSubscribers),
      onChange :  (param: string, form : FormGroup) => {
        form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].reset();
        if(!_.isNil(param)){
          form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].enable();
          this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getServiceTypes(param).subscribe(res =>{
            controls.find(item => item.controlName === FormControlNames.SUBSCRIPTION_SERVICE_TYPE)['options$'] = res;
          }));
        }
        else {
          form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].disable();
        }
      }
    })
  };

  getServiceTypeControl = (serviceInstance : any, controls : FormControlModel[], isMacro?: boolean) : DropdownFormControl => {
    return new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : FormControlNames.SUBSCRIPTION_SERVICE_TYPE,
      displayName : 'Service type',
      dataTestId : 'serviceType',
      placeHolder : 'Select Service Type',
      selectedField : 'name',
      name : "service-type",
      isDisabled : _.isNil(serviceInstance),
      value : serviceInstance ? serviceInstance.subscriptionServiceType : null,
      validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
      onInit : serviceInstance ? this._basicControlGenerator.getSubscribeInitResult.bind(
        this._aaiService,
        this.aaiService.getServiceTypes.bind(this, serviceInstance.globalSubscriberId)) : ()=>{},
      onChange :  (param: string, form : FormGroup) => {
        if(isMacro){
          form.controls[FormControlNames.LCPCLOUD_REGION_ID].reset();
          if(!_.isNil(param)) {
            form.controls[FormControlNames.LCPCLOUD_REGION_ID].enable();
            const globalCustomerId: string = form.controls[FormControlNames.GLOBAL_SUBSCRIBER_ID].value;
            if (!_.isNil(globalCustomerId)) {
              this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, param).subscribe(res => {
                controls.find(item => item.controlName === FormControlNames.LCPCLOUD_REGION_ID)['options$'] = res.lcpRegionList;
              }));
            }
          }
          else {
            form.controls[FormControlNames.LCPCLOUD_REGION_ID].disable();
          }
        }

      }
    })
  };

  getOwningEntityControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
    return new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : FormControlNames.OWNING_ENTITY_ID,
      displayName : 'Owning entity',
      dataTestId : 'owningEntity',
      placeHolder : 'Select Owning Entity',
      name :"owningEntity",
      isDisabled : false,
      validations : [new ValidatorModel(ValidatorOptions.required, 'is required'),],
      onInitSelectedField : ['owningEntityList'],
      value : serviceInstance ? serviceInstance.owningEntityId : null,
      onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
    })
  };

  getProductFamilyControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
    return new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : FormControlNames.PRODUCT_FAMILY_ID,
      displayName : 'Product family',
      dataTestId : 'productFamily',
      placeHolder : 'Select Product Family',
      isDisabled : false,
      name : "product-family-select",
      value : serviceInstance ? serviceInstance.productFamilyId : null,
      validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
      onInit : this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getProductFamilies),
    })
  };

  getLcpRegionControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
    return new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : FormControlNames.LCPCLOUD_REGION_ID,
      displayName : 'LCP region',
      dataTestId : 'lcpRegion',
      placeHolder : 'Select LCP Region',
      name : "lcpRegion",
      isDisabled : _.isNil(serviceInstance),
      value : serviceInstance ? serviceInstance.lcpCloudRegionId : null,
      validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
      onInitSelectedField : ['lcpRegionList'],
      onInit : serviceInstance ? this._basicControlGenerator.getSubscribeInitResult.bind(
        this._aaiService,
        this.aaiService.getLcpRegionsAndTenants.bind(this, serviceInstance.globalSubscriberId, serviceInstance.subscriptionServiceType)) : ()=>{},
      onChange :  (param: string, form : FormGroup) => {
        form.controls[FormControlNames.TENANT_ID].reset();
        if(param) {
          form.controls[FormControlNames.TENANT_ID].enable();
        }
        else {
          form.controls[FormControlNames.TENANT_ID].disable();
        }
        const globalCustomerId : string = form.controls[FormControlNames.GLOBAL_SUBSCRIBER_ID].value;
        const serviceType : string = form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].value;
        if(!_.isNil(globalCustomerId) && !_.isNil(serviceType)){
          this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res =>{
            controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
          }));
        }
      }
    })
  };

  getTenantControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
    return  new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : FormControlNames.TENANT_ID,
      displayName : 'Tenant',
      dataTestId : 'tenant',
      placeHolder : 'Select Tenant',
      name : "tenant",
      isDisabled : _.isNil(serviceInstance),
      onInitSelectedField :serviceInstance ?  ['lcpRegionsTenantsMap', serviceInstance.lcpCloudRegionId] : null,
      onInit : serviceInstance ? this._basicControlGenerator.getSubscribeInitResult.bind(
        this._aaiService,
        this.aaiService.getLcpRegionsAndTenants.bind(this, serviceInstance.globalSubscriberId, serviceInstance.subscriptionServiceType)) : ()=>{},
      value : serviceInstance ? serviceInstance.tenantId : null,
      validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
    })
  };

  getAICZoneControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
    return new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : FormControlNames.AICZONE_ID,
      displayName : 'AIC zone',
      dataTestId : 'aic_zone',
      placeHolder : 'Select AIC zone',
      name : "aicZone",
      value : serviceInstance ? serviceInstance.aicZoneId : null,
      isDisabled : false,
      validations : [],
      onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getAicZones)
    })
  };

  getPauseControl = (serviceInstance : any, controls : FormControlModel[]) :CheckboxFormControl => {
    return  new CheckboxFormControl({
      controlName : FormControlNames.PAUSE,
      displayName : 'Pause on pause points',
      dataTestId : 'Pause',
      isDisabled : false,
      value : serviceInstance ? serviceInstance.pause : null,
    })
  };

  getProjectControl = (serviceInstance : any, controls : FormControlModel[]) :DropdownFormControl =>{
    return new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : FormControlNames.PROJECT_NAME,
      displayName : 'Project',
      dataTestId : 'project',
      placeHolder : 'Select Project',
      name : "project",
      isDisabled : false,
      validations : [],
      value : serviceInstance ? serviceInstance.projectName : null,
      onInitSelectedField : ['projectList'],
      onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
    })
  };

  getRollbackOnFailureControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
    return new DropdownFormControl({
      type : FormControlType.DROPDOWN,
      controlName : FormControlNames.ROLLBACK_ON_FAILURE,
      displayName : 'Rollback on failure',
      dataTestId : 'rollback',
      isDisabled : false,
      validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
      value : serviceInstance ? serviceInstance.rollbackOnFailure : 'true',
      onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
    })
  };
}