mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-23 13:46:45 +02:00
feat: incorporate change request filters
This commit is contained in:
parent
aef8840ad8
commit
4bb9737d07
@ -355,7 +355,7 @@ export class SegmentsController extends Controller {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Remove unnecessary IFeatureStrategy fields from the response.
|
// Remove unnecessary IFeatureStrategy fields from the response.
|
||||||
const segmentStrategies = strategies.map((strategy) => ({
|
const segmentStrategies = strategies.strategies.map((strategy) => ({
|
||||||
id: strategy.id,
|
id: strategy.id,
|
||||||
projectId: strategy.projectId,
|
projectId: strategy.projectId,
|
||||||
featureName: strategy.featureName,
|
featureName: strategy.featureName,
|
||||||
@ -379,7 +379,7 @@ export class SegmentsController extends Controller {
|
|||||||
segmentIsInUse = await this.segmentService.isInUse(id);
|
segmentIsInUse = await this.segmentService.isInUse(id);
|
||||||
} else {
|
} else {
|
||||||
const strategies = await this.segmentService.getAllStrategies(id);
|
const strategies = await this.segmentService.getAllStrategies(id);
|
||||||
segmentIsInUse = strategies.length > 0;
|
segmentIsInUse = strategies.strategies.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (segmentIsInUse) {
|
if (segmentIsInUse) {
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
|
import { ChangeRequestStrategy } from 'lib/features/change-request-segment-usage-service/change-request-segment-usage-read-model';
|
||||||
import { UpsertSegmentSchema } from 'lib/openapi';
|
import { UpsertSegmentSchema } from 'lib/openapi';
|
||||||
import { IClientSegment, IFeatureStrategy, ISegment, IUser } from 'lib/types';
|
import { IClientSegment, IFeatureStrategy, ISegment, IUser } from 'lib/types';
|
||||||
|
|
||||||
|
export type UsedStrategies = {
|
||||||
|
strategies: IFeatureStrategy[];
|
||||||
|
changeRequestStrategies: ChangeRequestStrategy[];
|
||||||
|
};
|
||||||
|
|
||||||
export interface ISegmentService {
|
export interface ISegmentService {
|
||||||
updateStrategySegments: (
|
updateStrategySegments: (
|
||||||
strategyId: string,
|
strategyId: string,
|
||||||
@ -18,12 +24,9 @@ export interface ISegmentService {
|
|||||||
* This is NOT considering the private projects
|
* This is NOT considering the private projects
|
||||||
* For most use cases, use `getVisibleStrategies`
|
* For most use cases, use `getVisibleStrategies`
|
||||||
*/
|
*/
|
||||||
getAllStrategies(id: number): Promise<IFeatureStrategy[]>;
|
getAllStrategies(id: number): Promise<UsedStrategies>;
|
||||||
|
|
||||||
getVisibleStrategies(
|
getVisibleStrategies(id: number, userId: number): Promise<UsedStrategies>;
|
||||||
id: number,
|
|
||||||
userId: number,
|
|
||||||
): Promise<IFeatureStrategy[]>;
|
|
||||||
|
|
||||||
validateName(name: string): Promise<void>;
|
validateName(name: string): Promise<void>;
|
||||||
|
|
||||||
|
@ -18,7 +18,10 @@ import {
|
|||||||
import User from '../types/user';
|
import User from '../types/user';
|
||||||
import { IFeatureStrategiesStore } from '../features/feature-toggle/types/feature-toggle-strategies-store-type';
|
import { IFeatureStrategiesStore } from '../features/feature-toggle/types/feature-toggle-strategies-store-type';
|
||||||
import BadDataError from '../error/bad-data-error';
|
import BadDataError from '../error/bad-data-error';
|
||||||
import { ISegmentService } from '../segments/segment-service-interface';
|
import {
|
||||||
|
ISegmentService,
|
||||||
|
UsedStrategies,
|
||||||
|
} from '../segments/segment-service-interface';
|
||||||
import { PermissionError } from '../error';
|
import { PermissionError } from '../error';
|
||||||
import { IChangeRequestAccessReadModel } from '../features/change-request-access-service/change-request-access-read-model';
|
import { IChangeRequestAccessReadModel } from '../features/change-request-access-service/change-request-access-read-model';
|
||||||
import { IPrivateProjectChecker } from '../features/private-project/privateProjectCheckerType';
|
import { IPrivateProjectChecker } from '../features/private-project/privateProjectCheckerType';
|
||||||
@ -90,7 +93,7 @@ export class SegmentService implements ISegmentService {
|
|||||||
async getVisibleStrategies(
|
async getVisibleStrategies(
|
||||||
id: number,
|
id: number,
|
||||||
userId: number,
|
userId: number,
|
||||||
): Promise<IFeatureStrategy[]> {
|
): Promise<UsedStrategies> {
|
||||||
const strategies = await this.getAllStrategies(id);
|
const strategies = await this.getAllStrategies(id);
|
||||||
if (this.flagResolver.isEnabled('privateProjects')) {
|
if (this.flagResolver.isEnabled('privateProjects')) {
|
||||||
const accessibleProjects =
|
const accessibleProjects =
|
||||||
@ -100,29 +103,34 @@ export class SegmentService implements ISegmentService {
|
|||||||
if (accessibleProjects.mode === 'all') {
|
if (accessibleProjects.mode === 'all') {
|
||||||
return strategies;
|
return strategies;
|
||||||
} else {
|
} else {
|
||||||
return strategies.filter((strategy) =>
|
const filter = (strategy) =>
|
||||||
accessibleProjects.projects.includes(strategy.projectId),
|
accessibleProjects.projects.includes(strategy.projectId);
|
||||||
);
|
return {
|
||||||
|
strategies: strategies.strategies.filter(filter),
|
||||||
|
changeRequestStrategies:
|
||||||
|
strategies.changeRequestStrategies.filter(filter),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return strategies;
|
return strategies;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllStrategies(id: number): Promise<IFeatureStrategy[]> {
|
async getAllStrategies(id: number): Promise<UsedStrategies> {
|
||||||
const strategies =
|
const strategies =
|
||||||
await this.featureStrategiesStore.getStrategiesBySegment(id);
|
await this.featureStrategiesStore.getStrategiesBySegment(id);
|
||||||
return strategies;
|
|
||||||
|
const changeRequestStrategies =
|
||||||
|
await this.changeRequestSegmentUsageReadModel.getStrategiesUsedInActiveChangeRequests(
|
||||||
|
id,
|
||||||
|
);
|
||||||
|
return { strategies, changeRequestStrategies };
|
||||||
}
|
}
|
||||||
|
|
||||||
async isInUse(id: number): Promise<boolean> {
|
async isInUse(id: number): Promise<boolean> {
|
||||||
const strategies = await this.getAllStrategies(id);
|
const { strategies, changeRequestStrategies } =
|
||||||
if (strategies.length > 0) {
|
await this.getAllStrategies(id);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return await this.changeRequestSegmentUsageReadModel.isSegmentUsedInActiveChangeRequests(
|
return strategies.length > 0 || changeRequestStrategies.length > 0;
|
||||||
id,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(
|
async create(
|
||||||
@ -300,11 +308,13 @@ export class SegmentService implements ISegmentService {
|
|||||||
id: number,
|
id: number,
|
||||||
segment: Omit<ISegment, 'id'>,
|
segment: Omit<ISegment, 'id'>,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const strategies =
|
const { strategies, changeRequestStrategies } =
|
||||||
await this.featureStrategiesStore.getStrategiesBySegment(id);
|
await this.getAllStrategies(id);
|
||||||
|
|
||||||
const projectsUsed = new Set(
|
const projectsUsed = new Set(
|
||||||
strategies.map((strategy) => strategy.projectId),
|
[strategies, changeRequestStrategies].flatMap((strats) =>
|
||||||
|
strats.map((strategy) => strategy.projectId),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
Loading…
Reference in New Issue
Block a user