|
|
@ -156,10 +156,11 @@ export async function findIntersectedSegments( |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ |
|
|
|
export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ |
|
|
|
addedIds: SegmentId[], |
|
|
|
addedIds: SegmentId[], |
|
|
|
removedIds: SegmentId[] |
|
|
|
removedIds: SegmentId[], |
|
|
|
|
|
|
|
anyNewSegments: boolean |
|
|
|
}> { |
|
|
|
}> { |
|
|
|
if(_.isEmpty(segments)) { |
|
|
|
if(_.isEmpty(segments)) { |
|
|
|
return { addedIds: [], removedIds: [] }; |
|
|
|
return { addedIds: [], removedIds: [], anyNewSegments: false }; |
|
|
|
} |
|
|
|
} |
|
|
|
const analyticUnitId: AnalyticUnitId = segments[0].analyticUnitId; |
|
|
|
const analyticUnitId: AnalyticUnitId = segments[0].analyticUnitId; |
|
|
|
const unit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
const unit = await AnalyticUnit.findById(analyticUnitId); |
|
|
@ -173,6 +174,7 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ |
|
|
|
let segmentIdsToRemove: SegmentId[] = []; |
|
|
|
let segmentIdsToRemove: SegmentId[] = []; |
|
|
|
let segmentsToInsert: Segment[] = []; |
|
|
|
let segmentsToInsert: Segment[] = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let anyNewSegments = false; |
|
|
|
for(let segment of segments) { |
|
|
|
for(let segment of segments) { |
|
|
|
if(await isIntersectedWithExistingLabeled(segment)) { |
|
|
|
if(await isIntersectedWithExistingLabeled(segment)) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
@ -229,6 +231,7 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ |
|
|
|
segmentIdsToRemove = segmentIdsToRemove.concat(_.compact(intersectedIds)); |
|
|
|
segmentIdsToRemove = segmentIdsToRemove.concat(_.compact(intersectedIds)); |
|
|
|
segmentsToInsert.push(newSegment); |
|
|
|
segmentsToInsert.push(newSegment); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
anyNewSegments = true; |
|
|
|
segmentsToInsert.push(segment); |
|
|
|
segmentsToInsert.push(segment); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -237,7 +240,8 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ |
|
|
|
const addedIds = await db.insertMany(segmentsToInsert.map(s => s.toObject())); |
|
|
|
const addedIds = await db.insertMany(segmentsToInsert.map(s => s.toObject())); |
|
|
|
return { |
|
|
|
return { |
|
|
|
addedIds, |
|
|
|
addedIds, |
|
|
|
removedIds: segmentIdsToRemove |
|
|
|
removedIds: segmentIdsToRemove, |
|
|
|
|
|
|
|
anyNewSegments |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|