Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

import { Injectable } from '@angular/core';
import deepDiff from 'return-deep-diff';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class OhiInfoService {


differences = new Subject();
firstClaimObject;
secondClaimObject;

fullObject1;
fullObject2;

samePdiLoaded: boolean;


constructor() { }


getInstance(instance) {
const myNewInstance = Object.assign({}, instance);
myNewInstance.serviceLinesService = null;
if (myNewInstance) {
if (this.firstClaimObject) {
this.secondClaimObject = myNewInstance;
this.fullObject2 = myNewInstance;
} else {
this.firstClaimObject = myNewInstance;
this.fullObject1 = myNewInstance;
}

if (this.secondClaimObject) {
this.compareServiceLine();
}
}
}






compareServiceLine() {
const obj1 = this.firstClaimObject.ohiInfo.quickviewServiceLineOhiOutputRows;
const obj2 = this.secondClaimObject.ohiInfo.quickviewServiceLineOhiOutputRows;

let lineDiffs = deepDiff(obj1, obj2);

if (JSON.stringify(obj1) === JSON.stringify(obj2)) {
lineDiffs = 'equal';
}


const answer = {};

obj1.map((o1, index) => {
obj2.map((o2) => {
if (o1.lineNumber === o2.lineNumber) {
const diffs = deepDiff(o1, o2);
if (diffs && o1.lineNumber) {
diffs['lineNumber'] = o1.lineNumber;
answer[o1.lineNumber] = diffs;
}
}
});
});



const objToBeDiffed1 = {};
const objToBeDiffed2 = {};

// each service line comes that comes in can have any number of rows w/o service line numbers.
// if a row has a line number, push all the rows w/o line numbers after it into an objToBeDiffed
// until you reach the next for containing a lineNumber.
obj1.map((elem1, i1) => {
if (elem1['lineNumber']) {
let indexForWhileLoop = i1;
objToBeDiffed1[elem1['lineNumber']] = [];

while (obj1[indexForWhileLoop + 1] && obj1[indexForWhileLoop + 1]['lineNumber'] === null) {
objToBeDiffed1[elem1['lineNumber']].push(obj1[indexForWhileLoop + 1]);
indexForWhileLoop++;
}
}
obj2.map((elem2, i2) => {
if (elem2['lineNumber']) {
let indexForWhileLoop = i2;
objToBeDiffed2[elem2['lineNumber']] = [];

while (obj2[indexForWhileLoop + 1] && obj2[indexForWhileLoop + 1]['lineNumber'] === null) {
objToBeDiffed2[elem2['lineNumber']].push(obj2[indexForWhileLoop + 1]);
indexForWhileLoop++;
}
}
});
});

const rowsWithoutLineNumbersDiffs = deepDiff(objToBeDiffed1, objToBeDiffed2);

// creates an array out of the answer object
const arr = Object.keys(answer).map((k) => answer[k]);


// if the element of the array has a linenumber, insert the rows w/o lineNumbers (that we found the diffs for) after it.
for (let i = 0; i < arr.length; i++) {
if ( arr[i] && arr[i]['lineNumber'] ) {
rowsWithoutLineNumbersDiffs[arr[i]['lineNumber']].reverse().map((elem) => {
arr.splice(i + 1, 0, elem);
});
}
}
// Here is an example of what the above output should look like:
// [
// {lineNumber: 1, data: 'data'}, each object represents a row, note that some dont have lineNumbers
// {data: 'data'}, but are associated with whatever row had a lineNumber before it.
// {data: 'data'},
// {lineNumber: 2, data: 'data'},
// {data: 'data'},
// ]

this.differences.next([answer, arr]);
}
}