Skip to content

Commit

Permalink
Merge pull request #1999 from CarnegieLearningWeb/bugfix/1918-reopen-…
Browse files Browse the repository at this point in the history
…warning-error

Import will remove segments with Warning
  • Loading branch information
ppratikcr7 authored Oct 3, 2024
2 parents f86feda + 9062c71 commit 981ecc7
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions backend/packages/Upgrade/src/api/services/FeatureFlagService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export class FeatureFlagService {
@InjectRepository() private experimentAuditLogRepository: ExperimentAuditLogRepository,
@InjectDataSource() private dataSource: DataSource,
public experimentAssignmentService: ExperimentAssignmentService,
public segmentService: SegmentService
public segmentService: SegmentService,
public segmentArray = []
) {}

public find(logger: UpgradeLogger): Promise<FeatureFlag[]> {
Expand Down Expand Up @@ -825,14 +826,43 @@ export class FeatureFlagService {

return {
fileName: file.fileName,
error: isCompatible ? null : FF_COMPATIBILITY_TYPE.INCOMPATIBLE,
error: isCompatible ? validation.compatibilityType : FF_COMPATIBILITY_TYPE.INCOMPATIBLE,
};
});

const validFiles: FeatureFlagImportDataValidation[] = fileStatusArray
.filter((fileStatus) => fileStatus.error === null)
.filter((fileStatus) => fileStatus.error !== FF_COMPATIBILITY_TYPE.INCOMPATIBLE)
.map((fileStatus) => {
const featureFlagFile = featureFlagFiles.find((file) => file.fileName === fileStatus.fileName);
const segments = this.segmentArray;

if (fileStatus.error === FF_COMPATIBILITY_TYPE.WARNING) {
const flag = JSON.parse(featureFlagFile.fileContent as string);

// remove elements from featureFlagSegmentInclusion and featureFlagSegmentExclusion if segment is not found or context is not same
flag.featureFlagSegmentInclusion = flag.featureFlagSegmentInclusion.filter((segmentInclusion) => {
const subSegments = segmentInclusion.segment.subSegments;
const subSegmentIds = subSegments.map((subSegment) => subSegment.id);

// check if each subsegment if found in segments array and has the same context else remove segmentInclusion
return subSegmentIds.every((subSegmentId) => {
const segmentFound = segments.find((segment) => segment.id === subSegmentId);
return segmentFound && segmentFound.context === flag.context[0];
});
});

flag.featureFlagSegmentExclusion = flag.featureFlagSegmentExclusion.filter((segmentExclusion) => {
const subSegments = segmentExclusion.segment.subSegments;
const subSegmentIds = subSegments.map((subSegment) => subSegment.id);

// check if each subsegment if found in segments array and has the same context else remove segmentExclusion
return subSegmentIds.every((subSegmentId) => {
const segmentFound = segments.find((segment) => segment.id === subSegmentId);
return segmentFound && segmentFound.context === flag.context[0];
});
});
return flag;
}
return JSON.parse(featureFlagFile.fileContent as string);
});

Expand Down Expand Up @@ -917,6 +947,12 @@ export class FeatureFlagService {
createdFlags.push(createdFlag);
}
logger.info({ message: 'Imported feature flags', details: createdFlags });

fileStatusArray.forEach((fileStatus) => {
if (fileStatus.error !== FF_COMPATIBILITY_TYPE.INCOMPATIBLE) {
fileStatus.error = null;
}
});
return fileStatusArray;
}

Expand Down Expand Up @@ -1020,13 +1056,14 @@ export class FeatureFlagService {

const segmentIds = Array.from(segmentIdsSet);
const segments = await this.segmentService.getSegmentByIds(segmentIds);
this.segmentArray = segments;

if (segmentIds.length !== segments.length) {
compatibilityType = FF_COMPATIBILITY_TYPE.WARNING;
}

segments.forEach((segment) => {
if (segment == undefined) {
if (segment == undefined || segment.context !== flag.context[0]) {
compatibilityType = FF_COMPATIBILITY_TYPE.WARNING;
}
});
Expand Down

0 comments on commit 981ecc7

Please sign in to comment.