diff --git a/backend/packages/Upgrade/src/api/services/SegmentService.ts b/backend/packages/Upgrade/src/api/services/SegmentService.ts index 14ceb02b78..b75e5f2c32 100644 --- a/backend/packages/Upgrade/src/api/services/SegmentService.ts +++ b/backend/packages/Upgrade/src/api/services/SegmentService.ts @@ -5,7 +5,7 @@ import { IndividualForSegmentRepository } from '../repositories/IndividualForSeg import { GroupForSegmentRepository } from '../repositories/GroupForSegmentRepository'; import { Segment } from '../models/Segment'; import { UpgradeLogger } from '../../lib/logger/UpgradeLogger'; -import { EXPERIMENT_STATE, SEGMENT_TYPE, SERVER_ERROR, SEGMENT_STATUS } from 'upgrade_types'; +import { SEGMENT_TYPE, SERVER_ERROR, SEGMENT_STATUS } from 'upgrade_types'; import { getConnection } from 'typeorm'; import uuid from 'uuid'; import { ErrorWithType } from '../errors/ErrorWithType'; @@ -81,42 +81,39 @@ export class SegmentService { public async getAllSegmentWithStatus(logger: UpgradeLogger): Promise { const segmentsData = await getConnection().transaction(async () => { - const segmentsData = await this.getAllSegments(logger); - const allExperimentSegmentsInclusion = await this.getExperimentSegmenInclusionData(); - const allExperimentSegmentsExclusion = await this.getExperimentSegmenExclusionData(); + const [segmentsData, allExperimentSegmentsInclusion, allExperimentSegmentsExclusion] = await Promise.all([ + this.getAllSegments(logger), + this.getExperimentSegmenInclusionData(), + this.getExperimentSegmenExclusionData(), + ]); - const segmentsUsedLockedList = []; - const segmentsUsedUnlockedList = []; + const segmentsUsedList = []; if (allExperimentSegmentsInclusion) { allExperimentSegmentsInclusion.forEach((ele) => { const subSegments = ele.segment.subSegments; - subSegments.forEach((subSegment) => { - ele.experiment.state === EXPERIMENT_STATE.ENROLLING - ? segmentsUsedLockedList.push(subSegment.id) - : segmentsUsedUnlockedList.push(subSegment.id); - }); + segmentsUsedList.push(...subSegments.map((subSegment) => subSegment.id)); }); } if (allExperimentSegmentsExclusion) { allExperimentSegmentsExclusion.forEach((ele) => { const subSegments = ele.segment.subSegments; - subSegments.forEach((subSegment) => { - ele.experiment.state === EXPERIMENT_STATE.ENROLLING - ? segmentsUsedLockedList.push(subSegment.id) - : segmentsUsedUnlockedList.push(subSegment.id); - }); + segmentsUsedList.push(...subSegments.map((subSegment) => subSegment.id)); }); } + segmentsData.forEach((segment) => { + if (segmentsUsedList.includes(segment.id)) { + segmentsUsedList.push(...segment.subSegments.map((subSegment) => subSegment.id)); + } + }); + const segmentsDataWithStatus = segmentsData.map((segment) => { if (segment.id === globalExcludeSegment.id) { return { ...segment, status: SEGMENT_STATUS.GLOBAL }; - } else if (segmentsUsedLockedList.find((segmentId) => segmentId === segment.id)) { - return { ...segment, status: SEGMENT_STATUS.USED }; // TODO change to locked - } else if (segmentsUsedUnlockedList.find((segmentId) => segmentId === segment.id)) { - return { ...segment, status: SEGMENT_STATUS.USED }; // TODO change to unlocked + } else if (segmentsUsedList.includes(segment.id)) { + return { ...segment, status: SEGMENT_STATUS.USED }; } else { return { ...segment, status: SEGMENT_STATUS.UNUSED }; }