Skip to content

Commit

Permalink
fix: feature-dependencies-removed event should not be created always (#…
Browse files Browse the repository at this point in the history
…9100)

Currently, every time you archived feature, it created
feature-dependencies-removed event.

This PR adds a check to only create events for those features that have
dependency.
  • Loading branch information
sjaanus authored Jan 15, 2025
1 parent d6ec0f1 commit 3a50750
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
29 changes: 19 additions & 10 deletions src/lib/features/dependent-features/dependent-features-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,26 @@ export class DependentFeaturesService {
projectId: string,
auditUser: IAuditUser,
): Promise<void> {
await this.dependentFeaturesStore.deleteAll(features);
await this.eventService.storeEvents(
features.map(
(feature) =>
new FeatureDependenciesRemovedEvent({
project: projectId,
featureName: feature,
auditUser,
}),
),
const dependencies =
await this.dependentFeaturesReadModel.getDependencies(features);
const featuresWithDependencies = dependencies.map(
(dependency) => dependency.feature,
);
if (featuresWithDependencies.length > 0) {
await this.dependentFeaturesStore.deleteAll(
featuresWithDependencies,
);
await this.eventService.storeEvents(
featuresWithDependencies.map(
(feature) =>
new FeatureDependenciesRemovedEvent({
project: projectId,
featureName: feature,
auditUser,
}),
),
);
}
}

async getPossibleParentFeatures(feature: string): Promise<string[]> {
Expand Down
48 changes: 45 additions & 3 deletions src/lib/features/dependent-features/dependent.features.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ beforeEach(async () => {
await db.stores.dependentFeaturesStore.deleteAll();
await db.stores.featureToggleStore.deleteAll();
await db.stores.featureEnvironmentStore.deleteAll();
await db.stores.eventStore.deleteAll();
});

const addFeatureDependency = async (
Expand Down Expand Up @@ -168,11 +169,13 @@ const checkDependenciesExist = async (expectedCode = 200) => {
test('should add and delete feature dependencies', async () => {
const parent = uuidv4();
const child = uuidv4();
const child2 = uuidv4();
await app.createFeature(parent);
await app.createFeature(child);
await app.createFeature(child2);

const { body: options } = await getPossibleParentFeatures(child);
expect(options).toStrictEqual([parent]);
expect(options).toMatchObject([parent, child2].sort());

// save explicit enabled and variants
await addFeatureDependency(child, {
Expand All @@ -185,14 +188,21 @@ test('should add and delete feature dependencies', async () => {
variants: ['variantB'],
});

await addFeatureDependency(child2, {
feature: parent,
enabled: false,
});

await deleteFeatureDependency(child, parent); // single
await deleteFeatureDependencies(child); // all
await deleteFeatureDependencies(child2); // all

expect(await getRecordedEventTypesForDependencies()).toStrictEqual([
const eventTypes = await getRecordedEventTypesForDependencies();
expect(eventTypes).toStrictEqual([
FEATURE_DEPENDENCIES_REMOVED,
FEATURE_DEPENDENCY_REMOVED,
FEATURE_DEPENDENCY_ADDED,
FEATURE_DEPENDENCY_ADDED,
FEATURE_DEPENDENCY_ADDED,
]);
});

Expand Down Expand Up @@ -338,3 +348,35 @@ test('should not allow to add dependency to feature from another project', async
403,
);
});
test('should create feature-dependency-removed when archiving and has dependency', async () => {
const child = uuidv4();
const parent = uuidv4();
await app.createFeature(parent);
await app.createFeature(child);

await addFeatureDependency(child, {
feature: parent,
});
await app.archiveFeature(child);
const events = await eventStore.getEvents();
expect(events).toEqual(
expect.arrayContaining([
expect.objectContaining({ type: 'feature-dependencies-removed' }),
]),
);
});

test('should not create feature-dependency-removed when archiving and no dependency', async () => {
const child = uuidv4();
const parent = uuidv4();
await app.createFeature(parent);
await app.createFeature(child);

await app.archiveFeature(child);
const events = await eventStore.getEvents();
expect(events).not.toEqual(
expect.arrayContaining([
expect.objectContaining({ type: 'feature-dependencies-removed' }),
]),
);
});

0 comments on commit 3a50750

Please sign in to comment.