1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-17 13:46:47 +02:00
unleash.unleash/src/lib/features/change-request-segment-usage-service/sql-change-request-segment-usage-read-model.ts
Thomas Heartman f46d5a9269
chore: update segment cr return values (#5405)
This PR updates the returned value about segments to also include the CR
title and to be one list item per strategy per change request. This
means that if the same strategy is used multiple times in multiple
change requests, they each get their own line (as has been discussed
with Nicolae).

Because of this, this pr removes a collection step in the query and
fixes some test cases.
2023-11-27 11:20:39 +01:00

48 lines
1.6 KiB
TypeScript

import { Db } from '../../db/db';
import {
ChangeRequestStrategy,
IChangeRequestSegmentUsageReadModel,
} from './change-request-segment-usage-read-model';
export class ChangeRequestSegmentUsageReadModel
implements IChangeRequestSegmentUsageReadModel
{
private db: Db;
constructor(db: Db) {
this.db = db;
}
public async getStrategiesUsedInActiveChangeRequests(
segmentId: number,
): Promise<ChangeRequestStrategy[]> {
const query = this.db.raw(
`SELECT events.*, cr.project, cr.environment, cr.title
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((row) => {
const { payload, project, environment, feature } = row;
return {
projectId: project,
featureName: feature,
environment: environment,
strategyName: payload.name,
...(payload.id ? { id: payload.id } : {}),
changeRequest: {
id: row.change_request_id,
title: row.title || null,
},
};
});
return strategies;
}
}