Commit 8941b3ed authored by Michail Tzanatos's avatar Michail Tzanatos
Browse files

fixed product spec linking in product offering update/creation

parent 79baecb5
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
@@ -151,6 +151,52 @@
                                </div>
                                <!-- END OF Main Properties Tab -->

                                <!-- Linked Product Specification Characteristics Tab -->
                                <div class="tab-pane active"
                                    *ngIf="activeListItem === 'Linked Product Specification Characteristics'"
                                    role="tabpanel" [@fadeIn]>
                                    <form>
                                        <div class="card mat-elevation-z1 bg-light">
                                            <div class="card-body">
                                                <div class="row align-items-center">

                                                    <div *ngIf="offering?.productSpecification">

                                                        <div class="col-12 mb-3">
                                                            <h5>Characteristics from linked Product
                                                                Specification:
                                                                <dfn><b>{{offering?.productSpecification?.name}}</b></dfn>
                                                            </h5>
                                                        </div>

                                                        <!-- <div class="col-12"
                                                            *ngFor="let char of linkedSpecCharacteristics">
                                                            <mat-checkbox color="primary"
                                                                [checked]="isCharacteristicLinked(char)"
                                                                (change)="characteristicCheckboxChanged($event, char)">
                                                                {{char.name}}
                                                            </mat-checkbox>
                                                        </div> -->

                                                    </div>

                                                </div>

                                                <div class="container-fluid">
                                                    <!-- <div class="row justify-content-end">
                                                        <button type="submit" class="btn btn-primary"
                                                            (click)="updateOfferingCharacteristics()"><i
                                                                class="fas fa-check mr-2"></i>Submit</button>
                                                    </div> -->
                                                </div>
                                            </div>
                                            <div class="card-footer"></div>
                                        </div>
                                    </form>
                                </div>

                                <!-- END OF Linked Product Specification Characteristics Tab -->

                            </div>
                        </div>
                    </div>
+103 −39
Original line number Diff line number Diff line
import { trigger } from '@angular/animations';
import { SelectionChange } from '@angular/cdk/collections';
import { Component, OnInit } from '@angular/core';
import { UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms';
import { MatOptionSelectionChange } from '@angular/material/core';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { MatSelectChange } from '@angular/material/select';
import { ActivatedRoute, ActivationEnd, Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { relative } from 'path';
import { Subscription } from 'rxjs';
import { ProductOffering, ProductOfferingCreate, ProductOfferingUpdate, ProductSpecification } from 'src/app/openApis/productCatalogManagement/models';
import { ProductOfferingService, ProductSpecificationService } from 'src/app/openApis/productCatalogManagement/services';
@@ -40,7 +37,7 @@ export class EditProductOfferingsComponent implements OnInit {

  productSpecifications: ProductSpecification[]

  listItems = ["Main Properties"]
  listItems = ["Main Properties", "Linked Product Specification Characteristics"]
  activeListItem = "Main Properties"

  editForm =  new UntypedFormGroup({
@@ -160,44 +157,111 @@ export class EditProductOfferingsComponent implements OnInit {

  updateOfferingGeneral() {
    if (this.editForm.valid) {
      const updateObj: ProductOfferingCreate | ProductOfferingUpdate = {
        description: this.editForm.value.description,
        lifecycleStatus: this.editForm.value.lifecycleStatus,
        name: this.editForm.value.name,
        validFor: this.editForm.value.validFor,
        version: this.editForm.value.version
      const formValue = this.editForm.value;
      const specRef = formValue.productSpecification;

      if (specRef && specRef.id) {
        // retrieve full specification details
        this.specService.retrieveProductSpecification({ id: specRef.id }).subscribe(
          (fullSpec: ProductSpecification) => {
            const payload = this.constructPayload(formValue, fullSpec);
            this.submitOfferingToApi(payload);
          },
          (error) => {
            console.error(error);
            this.toastrService.error("Could not retrieve full Product Specification details.");
          }
        );
      } else {
        // no spec selected
        const payload = this.constructPayload(formValue, null);
        this.submitOfferingToApi(payload);
      }
    }
  }

  private constructPayload(formValue: any, linkedSpec: ProductSpecification | null): ProductOfferingCreate | ProductOfferingUpdate {
    
    let productSpecRef: any = undefined;
    if (linkedSpec) {
      productSpecRef = { id: linkedSpec.id, name: linkedSpec.name };
      if (linkedSpec.version) productSpecRef.version = linkedSpec.version;
    }

    return {
      name: formValue.name,
      description: formValue.description,
      lifecycleStatus: formValue.lifecycleStatus,
      version: formValue.version,
      validFor: formValue.validFor,
      isSellable: true, 
      statusReason: '',

      productSpecification: productSpecRef,

      
      isBundle: linkedSpec?.isBundle || false,
      attachment: linkedSpec?.attachment || [],
      
      prodSpecCharValueUse: [],

      serviceCandidate: (linkedSpec && (linkedSpec as any).serviceSpecification && (linkedSpec as any).serviceSpecification.length > 0) 
        ? { 
            id: (linkedSpec as any).serviceSpecification[0].id, 
            name: (linkedSpec as any).serviceSpecification[0].name,
            version: (linkedSpec as any).serviceSpecification[0].version,
            '@type': 'ServiceCandidateRef' 
          } 
        : undefined,

      resourceCandidate: (linkedSpec && (linkedSpec as any).resourceSpecification && (linkedSpec as any).resourceSpecification.length > 0) 
        ? { 
            id: (linkedSpec as any).resourceSpecification[0].id, 
            name: (linkedSpec as any).resourceSpecification[0].name, 
            version: (linkedSpec as any).resourceSpecification[0].version,
            '@type': 'ResourceCandidateRef' 
          } 
        : undefined,

      if (this.editForm.value.productSpecification) {
        updateObj.productSpecification = {name: this.editForm.value.productSpecification.name, id: this.editForm.value.productSpecification.id}
      serviceLevelAgreement: (linkedSpec as any).serviceLevelAgreement || undefined,

      agreement: [],
      bundledProductOffering: [], 
      category: [],
      channel: [],
      marketSegment: [],
      place: [],
      productOfferingPrice: [],
      productOfferingTerm: [],
      
      '@type': 'ProductOffering'
    };
  }

      let updatedOffering: ProductOffering
  private submitOfferingToApi(updateObj: ProductOfferingCreate | ProductOfferingUpdate) {
    let updatedOffering: ProductOffering;
  
    if (this.newOffering) {
      this.offeringService.createProductOffering(updateObj).subscribe(
        data => { updatedOffering = data },
          error => console.error(error),
        error => { console.error(error); this.toastrService.error("Failed to create offering"); },
        () => {
            this.newOffering = false
            this.router.navigate([updatedOffering.id], {relativeTo: this.activatedRoute})
            this.toastrService.success("Product Offering was successfully created")
            this.refreshProductOffering(updatedOffering)
          }
        )
          this.newOffering = false;
          this.router.navigate([updatedOffering.id], {relativeTo: this.activatedRoute});
          this.toastrService.success("Product Offering was successfully created");
          this.refreshProductOffering(updatedOffering);
        }
      else {
      );
    } else {
      this.offeringService.patchProductOffering({ id: this.offeringID, productOffering: updateObj }).subscribe(
        data => { updatedOffering = data },
          error => console.error(error),
        error => { console.error(error); this.toastrService.error("Failed to update offering"); },
        () => {
            this.toastrService.success("Product Offering was successfully updated")
            this.refreshProductOffering(updatedOffering)
          }
        )
          this.toastrService.success("Product Offering was successfully updated");
          this.refreshProductOffering(updatedOffering);
        }
      );
    }
    
  }

  refreshProductOffering(updatedOffering : ProductOffering) {