mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-27 13:49:10 +02:00
feat: fix impl
This commit is contained in:
parent
a549f9a017
commit
8666955eab
@ -113,7 +113,7 @@ describe.each([
|
|||||||
])('Should handle %s changes correctly', (_, addOrUpdateStrategy) => {
|
])('Should handle %s changes correctly', (_, addOrUpdateStrategy) => {
|
||||||
test.each([
|
test.each([
|
||||||
['Draft', true],
|
['Draft', true],
|
||||||
['In Review', true],
|
['In review', true],
|
||||||
['Scheduled', true],
|
['Scheduled', true],
|
||||||
['Approved', true],
|
['Approved', true],
|
||||||
['Rejected', false],
|
['Rejected', false],
|
||||||
@ -137,7 +137,7 @@ describe.each([
|
|||||||
describe('addStrategy events should show up in used strategies correctly', () => {
|
describe('addStrategy events should show up in used strategies correctly', () => {
|
||||||
test.each([
|
test.each([
|
||||||
['Draft', true],
|
['Draft', true],
|
||||||
['In Review', true],
|
['In review', true],
|
||||||
['Scheduled', true],
|
['Scheduled', true],
|
||||||
['Approved', true],
|
['Approved', true],
|
||||||
['Rejected', false],
|
['Rejected', false],
|
||||||
@ -152,18 +152,21 @@ describe('addStrategy events should show up in used strategies correctly', () =>
|
|||||||
|
|
||||||
await addStrategyToCr(segmentId, FLAG_NAME);
|
await addStrategyToCr(segmentId, FLAG_NAME);
|
||||||
|
|
||||||
const result = await readModel.isSegmentUsedInActiveChangeRequests(
|
const result =
|
||||||
segmentId,
|
await readModel.getStrategiesUsedInActiveChangeRequests(
|
||||||
);
|
segmentId,
|
||||||
|
);
|
||||||
if (shouldShow) {
|
if (shouldShow) {
|
||||||
expect(result).toBe({
|
expect(result).toStrictEqual([
|
||||||
projectId: 'default',
|
{
|
||||||
strategyName: 'flexibleRollout',
|
projectId: 'default',
|
||||||
environment: 'default',
|
strategyName: 'flexibleRollout',
|
||||||
featureName: FLAG_NAME,
|
environment: 'default',
|
||||||
});
|
featureName: FLAG_NAME,
|
||||||
|
},
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
expect(result).toBe([]);
|
expect(result).toStrictEqual([]);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -172,7 +175,7 @@ describe('addStrategy events should show up in used strategies correctly', () =>
|
|||||||
describe('updateStrategy events should show up in used strategies correctly', () => {
|
describe('updateStrategy events should show up in used strategies correctly', () => {
|
||||||
test.each([
|
test.each([
|
||||||
['Draft', true],
|
['Draft', true],
|
||||||
['In Review', true],
|
['In review', true],
|
||||||
['Scheduled', true],
|
['Scheduled', true],
|
||||||
['Approved', true],
|
['Approved', true],
|
||||||
['Rejected', false],
|
['Rejected', false],
|
||||||
@ -204,7 +207,7 @@ describe('updateStrategy events should show up in used strategies correctly', ()
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
expect(result).toBe([]);
|
expect(result).toStrictEqual([]);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -7,7 +7,7 @@ type NewStrategy = {
|
|||||||
|
|
||||||
type ExistingStrategy = NewStrategy & { id?: string };
|
type ExistingStrategy = NewStrategy & { id?: string };
|
||||||
|
|
||||||
type ChangeRequestStrategy = NewStrategy | ExistingStrategy;
|
export type ChangeRequestStrategy = NewStrategy | ExistingStrategy;
|
||||||
|
|
||||||
export interface IChangeRequestSegmentUsageReadModel {
|
export interface IChangeRequestSegmentUsageReadModel {
|
||||||
isSegmentUsedInActiveChangeRequests(segmentId: number): Promise<boolean>;
|
isSegmentUsedInActiveChangeRequests(segmentId: number): Promise<boolean>;
|
||||||
|
@ -1,16 +1,32 @@
|
|||||||
import { IChangeRequestSegmentUsageReadModel } from './change-request-segment-usage-read-model';
|
import {
|
||||||
|
ChangeRequestStrategy,
|
||||||
|
IChangeRequestSegmentUsageReadModel,
|
||||||
|
} from './change-request-segment-usage-read-model';
|
||||||
|
|
||||||
export class FakeChangeRequestSegmentUsageReadModel
|
export class FakeChangeRequestSegmentUsageReadModel
|
||||||
implements IChangeRequestSegmentUsageReadModel
|
implements IChangeRequestSegmentUsageReadModel
|
||||||
{
|
{
|
||||||
private isSegmentUsedInActiveChangeRequestsValue: boolean;
|
private isSegmentUsedInActiveChangeRequestsValue: boolean;
|
||||||
|
strategiesUsedInActiveChangeRequests: ChangeRequestStrategy[];
|
||||||
|
|
||||||
constructor(isSegmentUsedInActiveChangeRequests = false) {
|
constructor(
|
||||||
|
isSegmentUsedInActiveChangeRequests = false,
|
||||||
|
strategiesUsedInActiveChangeRequests = [],
|
||||||
|
) {
|
||||||
this.isSegmentUsedInActiveChangeRequestsValue =
|
this.isSegmentUsedInActiveChangeRequestsValue =
|
||||||
isSegmentUsedInActiveChangeRequests;
|
isSegmentUsedInActiveChangeRequests;
|
||||||
|
|
||||||
|
this.strategiesUsedInActiveChangeRequests =
|
||||||
|
strategiesUsedInActiveChangeRequests;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async isSegmentUsedInActiveChangeRequests(): Promise<boolean> {
|
public async isSegmentUsedInActiveChangeRequests(): Promise<boolean> {
|
||||||
return this.isSegmentUsedInActiveChangeRequestsValue;
|
return this.isSegmentUsedInActiveChangeRequestsValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getStrategiesUsedInActiveChangeRequests(): Promise<
|
||||||
|
ChangeRequestStrategy[]
|
||||||
|
> {
|
||||||
|
return this.strategiesUsedInActiveChangeRequests;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import { Db } from '../../db/db';
|
import { Db } from '../../db/db';
|
||||||
import { IChangeRequestSegmentUsageReadModel } from './change-request-segment-usage-read-model';
|
import {
|
||||||
|
ChangeRequestStrategy,
|
||||||
|
IChangeRequestSegmentUsageReadModel,
|
||||||
|
} from './change-request-segment-usage-read-model';
|
||||||
|
|
||||||
export class ChangeRequestSegmentUsageReadModel
|
export class ChangeRequestSegmentUsageReadModel
|
||||||
implements IChangeRequestSegmentUsageReadModel
|
implements IChangeRequestSegmentUsageReadModel
|
||||||
@ -9,7 +12,6 @@ export class ChangeRequestSegmentUsageReadModel
|
|||||||
constructor(db: Db) {
|
constructor(db: Db) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async isSegmentUsedInActiveChangeRequests(
|
public async isSegmentUsedInActiveChangeRequests(
|
||||||
segmentId: number,
|
segmentId: number,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
@ -17,7 +19,7 @@ export class ChangeRequestSegmentUsageReadModel
|
|||||||
`SELECT events.*
|
`SELECT events.*
|
||||||
FROM change_request_events events
|
FROM change_request_events events
|
||||||
JOIN change_requests cr ON events.change_request_id = cr.id
|
JOIN change_requests cr ON events.change_request_id = cr.id
|
||||||
WHERE cr.state IN ('Draft', 'In Review', 'Scheduled', 'Approved')
|
WHERE cr.state IN ('Draft', 'In review', 'Scheduled', 'Approved')
|
||||||
AND events.action IN ('updateStrategy', 'addStrategy');`,
|
AND events.action IN ('updateStrategy', 'addStrategy');`,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -27,4 +29,34 @@ export class ChangeRequestSegmentUsageReadModel
|
|||||||
|
|
||||||
return isUsed;
|
return isUsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mapRow = (row): ChangeRequestStrategy => {
|
||||||
|
const { payload, project, environment, feature } = row;
|
||||||
|
return {
|
||||||
|
projectId: project,
|
||||||
|
featureName: feature,
|
||||||
|
environment: environment,
|
||||||
|
strategyName: payload.name,
|
||||||
|
...(payload.id ? { id: payload.id } : {}),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
public async getStrategiesUsedInActiveChangeRequests(
|
||||||
|
segmentId: number,
|
||||||
|
): Promise<ChangeRequestStrategy[]> {
|
||||||
|
const query = this.db.raw(
|
||||||
|
`SELECT events.*, cr.project, cr.environment
|
||||||
|
FROM change_request_events events
|
||||||
|
JOIN change_requests cr ON events.change_request_id = cr.id
|
||||||
|
WHERE cr.state NOT IN ('Applied', 'Cancelled', 'Rejected')
|
||||||
|
AND events.action IN ('updateStrategy', 'addStrategy');`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const queryResult = await query;
|
||||||
|
const strategies = queryResult.rows
|
||||||
|
.filter((row) => row.payload?.segments?.includes(segmentId))
|
||||||
|
.map(this.mapRow);
|
||||||
|
|
||||||
|
return strategies;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user