mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-01 13:47:27 +02:00
feat: update query to include CR titles
This commit is contained in:
parent
548700e924
commit
8169ea6996
@ -10,6 +10,9 @@ let user: IUser;
|
|||||||
|
|
||||||
const CR_ID = 123456;
|
const CR_ID = 123456;
|
||||||
const CR_ID_2 = 234567;
|
const CR_ID_2 = 234567;
|
||||||
|
|
||||||
|
const CR_TITLE = 'My change request';
|
||||||
|
|
||||||
const FLAG_NAME = 'crarm-test-flag';
|
const FLAG_NAME = 'crarm-test-flag';
|
||||||
|
|
||||||
let readModel: IChangeRequestSegmentUsageReadModel;
|
let readModel: IChangeRequestSegmentUsageReadModel;
|
||||||
@ -46,7 +49,11 @@ afterEach(async () => {
|
|||||||
.delete();
|
.delete();
|
||||||
});
|
});
|
||||||
|
|
||||||
const createCR = async (state, changeRequestId = CR_ID) => {
|
const createCR = async (
|
||||||
|
state,
|
||||||
|
changeRequestId = CR_ID,
|
||||||
|
changeRequestTitle: string | null = CR_TITLE,
|
||||||
|
) => {
|
||||||
await db.rawDatabase.table('change_requests').insert({
|
await db.rawDatabase.table('change_requests').insert({
|
||||||
id: changeRequestId,
|
id: changeRequestId,
|
||||||
environment: 'default',
|
environment: 'default',
|
||||||
@ -55,7 +62,7 @@ const createCR = async (state, changeRequestId = CR_ID) => {
|
|||||||
created_by: user.id,
|
created_by: user.id,
|
||||||
created_at: '2023-01-01 00:00:00',
|
created_at: '2023-01-01 00:00:00',
|
||||||
min_approvals: 1,
|
min_approvals: 1,
|
||||||
title: 'My change request',
|
title: changeRequestTitle,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -154,7 +161,7 @@ test.each([
|
|||||||
strategyName: 'flexibleRollout',
|
strategyName: 'flexibleRollout',
|
||||||
environment: 'default',
|
environment: 'default',
|
||||||
featureName: FLAG_NAME,
|
featureName: FLAG_NAME,
|
||||||
changeRequestIds: [CR_ID],
|
changeRequests: [{ id: CR_ID, title: CR_TITLE }],
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
@ -193,7 +200,7 @@ test.each([
|
|||||||
strategyName: 'flexibleRollout',
|
strategyName: 'flexibleRollout',
|
||||||
environment: 'default',
|
environment: 'default',
|
||||||
featureName: FLAG_NAME,
|
featureName: FLAG_NAME,
|
||||||
changeRequestIds: [CR_ID],
|
changeRequests: [{ id: CR_ID, title: CR_TITLE }],
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
@ -203,8 +210,8 @@ test.each([
|
|||||||
);
|
);
|
||||||
|
|
||||||
test(`If the same strategy appears in multiple CRs with the same segment, they should all be listed in its changeRequestIds`, async () => {
|
test(`If the same strategy appears in multiple CRs with the same segment, they should all be listed in its changeRequestIds`, async () => {
|
||||||
await createCR('In review', CR_ID);
|
await createCR('In review', CR_ID, CR_TITLE);
|
||||||
await createCR('In review', CR_ID_2);
|
await createCR('In review', CR_ID_2, null);
|
||||||
|
|
||||||
const segmentId = 3;
|
const segmentId = 3;
|
||||||
const strategyId = randomId();
|
const strategyId = randomId();
|
||||||
@ -228,8 +235,8 @@ test(`If the same strategy appears in multiple CRs with the same segment, they s
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const crIds = result[0].changeRequestIds;
|
const crData = result[0].changeRequests;
|
||||||
expect(crIds).toContain(CR_ID);
|
expect(crData).toContainEqual({ id: CR_ID, title: CR_TITLE });
|
||||||
expect(crIds).toContain(CR_ID_2);
|
expect(crData).toContainEqual({ id: CR_ID_2, title: null });
|
||||||
expect(crIds).toHaveLength(2);
|
expect(crData).toHaveLength(2);
|
||||||
});
|
});
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
|
type ChangeRequestInfo = { id: string; title: string | null };
|
||||||
|
|
||||||
type NewStrategy = {
|
type NewStrategy = {
|
||||||
projectId: string;
|
projectId: string;
|
||||||
featureName: string;
|
featureName: string;
|
||||||
strategyName: string;
|
strategyName: string;
|
||||||
environment: string;
|
environment: string;
|
||||||
changeRequestIds: [string, ...string[]];
|
changeRequests: [ChangeRequestInfo, ...ChangeRequestInfo[]];
|
||||||
};
|
};
|
||||||
|
|
||||||
type ExistingStrategy = NewStrategy & { id: string };
|
type ExistingStrategy = NewStrategy & { id: string };
|
||||||
|
@ -17,7 +17,7 @@ export class ChangeRequestSegmentUsageReadModel
|
|||||||
segmentId: number,
|
segmentId: number,
|
||||||
): Promise<ChangeRequestStrategy[]> {
|
): Promise<ChangeRequestStrategy[]> {
|
||||||
const query = this.db.raw(
|
const query = this.db.raw(
|
||||||
`SELECT events.*, cr.project, cr.environment
|
`SELECT events.*, cr.project, cr.environment, cr.title
|
||||||
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 NOT IN ('Applied', 'Cancelled', 'Rejected')
|
WHERE cr.state NOT IN ('Applied', 'Cancelled', 'Rejected')
|
||||||
@ -35,21 +35,24 @@ export class ChangeRequestSegmentUsageReadModel
|
|||||||
environment: environment,
|
environment: environment,
|
||||||
strategyName: payload.name,
|
strategyName: payload.name,
|
||||||
...(payload.id ? { id: payload.id } : {}),
|
...(payload.id ? { id: payload.id } : {}),
|
||||||
changeRequestId: row.change_request_id,
|
changeRequest: {
|
||||||
|
id: row.change_request_id,
|
||||||
|
title: row.title || null,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const deduped = strategies.reduce((acc, strategy) => {
|
const deduped = strategies.reduce((acc, strategy) => {
|
||||||
const { changeRequestId, ...rest } = strategy;
|
const { changeRequest, ...rest } = strategy;
|
||||||
|
|
||||||
const existingData = acc[strategy.id];
|
const existingData = acc[strategy.id];
|
||||||
|
|
||||||
if (existingData) {
|
if (existingData) {
|
||||||
existingData.changeRequestIds.push(strategy.changeRequestId);
|
existingData.changeRequests.push(changeRequest);
|
||||||
} else {
|
} else {
|
||||||
acc[strategy.id] = {
|
acc[strategy.id] = {
|
||||||
...rest,
|
...rest,
|
||||||
changeRequestIds: [strategy.changeRequestId],
|
changeRequests: [changeRequest],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user