Skip to content

Commit

Permalink
feat: delete all feature dependencies (#4832)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Sep 26, 2023
1 parent 2b96782 commit d49ff03
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
Expand Up @@ -117,6 +117,26 @@ export default class DependentFeaturesController extends Controller {
}),
],
});

this.route({
method: 'delete',
path: PATH_DEPENDENCIES,
handler: this.deleteFeatureDependencies,
permission: UPDATE_FEATURE,
acceptAnyContentType: true,
middleware: [
openApiService.validPath({
tags: ['Features'],
summary: 'Deletes feature dependencies.',
description: 'Remove dependencies to all parent features.',
operationId: 'deleteFeatureDependencies',
responses: {
200: emptyResponse,
...getStandardResponses(401, 403, 404),
},
}),
],
});
}

async addFeatureDependency(
Expand Down Expand Up @@ -162,4 +182,22 @@ export default class DependentFeaturesController extends Controller {
);
}
}

async deleteFeatureDependencies(
req: IAuthRequest<FeatureParams, any, any>,
res: Response,
): Promise<void> {
const { child } = req.params;

if (this.config.flagResolver.isEnabled('dependentFeatures')) {
await this.dependentFeaturesService.deleteFeatureDependencies(
child,
);
res.status(200).end();
} else {
throw new InvalidOperationError(
'Dependent features are not enabled',
);
}
}
}
Expand Up @@ -44,4 +44,8 @@ export class DependentFeaturesService {
): Promise<void> {
await this.dependentFeaturesStore.delete(dependency);
}

async deleteFeatureDependencies(feature: string): Promise<void> {
await this.dependentFeaturesStore.deleteAll(feature);
}
}
Expand Up @@ -4,4 +4,5 @@ export interface IDependentFeaturesStore {
upsert(featureDependency: FeatureDependency): Promise<void>;
getChildren(parent: string): Promise<string[]>;
delete(dependency: FeatureDependencyId): Promise<void>;
deleteAll(child: string): Promise<void>;
}
Expand Up @@ -44,4 +44,8 @@ export class DependentFeaturesStore implements IDependentFeaturesStore {
.andWhere('child', dependency.child)
.del();
}

async deleteAll(feature: string): Promise<void> {
await this.db('dependent_features').andWhere('child', feature).del();
}
}
16 changes: 14 additions & 2 deletions src/lib/features/dependent-features/dependent.features.e2e.test.ts
Expand Up @@ -56,7 +56,18 @@ const deleteFeatureDependency = async (
.expect(expectedCode);
};

test('should add feature dependency', async () => {
const deleteFeatureDependencies = async (
childFeature: string,
expectedCode = 200,
) => {
return app.request
.delete(
`/api/admin/projects/default/features/${childFeature}/dependencies`,
)
.expect(expectedCode);
};

test('should add and delete feature dependencies', async () => {
const parent = uuidv4();
const child = uuidv4();
await app.createFeature(parent);
Expand All @@ -73,7 +84,8 @@ test('should add feature dependency', async () => {
variants: ['variantB'],
});

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

test('should not allow to add a parent dependency to a feature that already has children', async () => {
Expand Down
Expand Up @@ -12,4 +12,8 @@ export class FakeDependentFeaturesStore implements IDependentFeaturesStore {
delete(): Promise<void> {
return Promise.resolve();
}

deleteAll(): Promise<void> {
return Promise.resolve();
}
}

0 comments on commit d49ff03

Please sign in to comment.