Commit 1696f524 authored by Michail Tzanatos's avatar Michail Tzanatos
Browse files

fixed copy characteristic bug when it already exists

parent 05a33135
Loading
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
@@ -165,7 +165,7 @@ export class AssignServiceSpecificationComponent implements OnInit {
      });
    }
    else {
      this.executeAssignment(false);
      this.executeAssignment(null);
    }
  }

@@ -251,7 +251,6 @@ export class AssignServiceSpecificationComponent implements OnInit {
  }

  removeUnwantedCharacteristics(spec: ProductSpecification) {
    // identify characteristics to remove (from CFSS) if user clicks no
    const unwantedNames: string[] = [];
    
    this.selectedSpecs.forEach(s => {
@@ -262,11 +261,26 @@ export class AssignServiceSpecificationComponent implements OnInit {
      }
    });

    const originalNames = (this.data.productSpec.productSpecCharacteristic || []).map(c => c.name);

    const currentChars = spec.productSpecCharacteristic || [];
    const newSpecCharacteristicArray = currentChars.filter(c => !unwantedNames.includes(c.name));
    
    // If there is nothing to change, just close
    const newSpecCharacteristicArray = currentChars.filter(char => {
      const isUnwanted = unwantedNames.includes(char.name);
      const wasPreExisting = originalNames.includes(char.name);

      // If it is NOT in the unwanted list, keep it.
      if (!isUnwanted) return true;

      // If it IS in the unwanted list, BUT it existed before we started, keep it.
      if (wasPreExisting) return true;

      return false;
    });

    // If there is nothing to change (lengths match), just close
    if (newSpecCharacteristicArray.length === currentChars.length) {
      this.toast.success('Product Specification updated successfully');
      this.dialogRef.close('updated');
      return;
    }
@@ -281,10 +295,12 @@ export class AssignServiceSpecificationComponent implements OnInit {
    }).subscribe(
      data => {}, 
      error => {
        console.error(error);
        this.toast.warning('Service assigned, but failed to clean up auto-assigned characteristics.');
        this.dialogRef.close('updated'); 
      },
      () => {
        this.toast.success("Product Specification Characteristics list was successfully updated");
        this.dialogRef.close('updated');
      }
    )