1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-12 01:17:04 +02:00
unleash.unleash/src/lib/features/change-request-segment-usage-service/sql-change-request-segment-usage-read-model.ts
Thomas Heartman ece5a634bf
feat: API prevents you from deleting segments in crs (#5308)
This PR hooks up the changes introduced in #5301 to the API and puts
them behind a feature flag. A new test has been added and the test setup
has been slightly tweaked to allow this test.

When the flag is enabled, the API will now not let you delete a segment
that's used in any active CRs.
2023-11-09 12:09:39 +01:00

31 lines
917 B
TypeScript

import { Db } from '../../db/db';
import { IChangeRequestSegmentUsageReadModel } from './change-request-segment-usage-read-model';
export class ChangeRequestSegmentUsageReadModel
implements IChangeRequestSegmentUsageReadModel
{
private db: Db;
constructor(db: Db) {
this.db = db;
}
public async isSegmentUsedInActiveChangeRequests(
segmentId: number,
): Promise<boolean> {
const result = await this.db.raw(
`SELECT events.*
FROM change_request_events events
JOIN change_requests cr ON events.change_request_id = cr.id
WHERE cr.state IN ('Draft', 'In Review', 'Scheduled', 'Approved')
AND events.action IN ('updateStrategy', 'addStrategy');`,
);
const isUsed = result.rows.some((row) =>
row.payload?.segments?.includes(segmentId),
);
return isUsed;
}
}