2023-09-19 11:23:21 +02:00
|
|
|
import { Response } from 'express';
|
|
|
|
import Controller from '../../routes/controller';
|
|
|
|
import { OpenApiService } from '../../services';
|
|
|
|
import {
|
|
|
|
CREATE_FEATURE,
|
|
|
|
IFlagResolver,
|
|
|
|
IUnleashConfig,
|
|
|
|
IUnleashServices,
|
|
|
|
} from '../../types';
|
|
|
|
import { Logger } from '../../logger';
|
|
|
|
import {
|
|
|
|
CreateDependentFeatureSchema,
|
|
|
|
createRequestSchema,
|
|
|
|
emptyResponse,
|
|
|
|
getStandardResponses,
|
|
|
|
} from '../../openapi';
|
|
|
|
import { IAuthRequest } from '../../routes/unleash-types';
|
|
|
|
import { InvalidOperationError } from '../../error';
|
|
|
|
import { DependentFeaturesService } from './dependent-features-service';
|
2023-09-25 10:12:32 +02:00
|
|
|
import { TransactionCreator, UnleashTransaction } from '../../db/transaction';
|
2023-09-19 11:23:21 +02:00
|
|
|
|
|
|
|
interface FeatureParams {
|
|
|
|
featureName: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PATH = '/:projectId/features';
|
|
|
|
const PATH_FEATURE = `${PATH}/:featureName`;
|
|
|
|
const PATH_DEPENDENCIES = `${PATH_FEATURE}/dependencies`;
|
|
|
|
|
|
|
|
type DependentFeaturesServices = Pick<
|
|
|
|
IUnleashServices,
|
2023-09-25 10:12:32 +02:00
|
|
|
'transactionalDependentFeaturesService' | 'openApiService'
|
2023-09-19 11:23:21 +02:00
|
|
|
>;
|
|
|
|
|
|
|
|
export default class DependentFeaturesController extends Controller {
|
2023-09-25 10:12:32 +02:00
|
|
|
private transactionalDependentFeaturesService: (
|
|
|
|
db: UnleashTransaction,
|
|
|
|
) => DependentFeaturesService;
|
|
|
|
|
|
|
|
private readonly startTransaction: TransactionCreator<UnleashTransaction>;
|
2023-09-19 11:23:21 +02:00
|
|
|
|
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
|
|
|
private flagResolver: IFlagResolver;
|
|
|
|
|
|
|
|
private readonly logger: Logger;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
2023-09-25 10:12:32 +02:00
|
|
|
{
|
|
|
|
transactionalDependentFeaturesService,
|
|
|
|
openApiService,
|
|
|
|
}: DependentFeaturesServices,
|
|
|
|
startTransaction: TransactionCreator<UnleashTransaction>,
|
2023-09-19 11:23:21 +02:00
|
|
|
) {
|
|
|
|
super(config);
|
2023-09-25 10:12:32 +02:00
|
|
|
this.transactionalDependentFeaturesService =
|
|
|
|
transactionalDependentFeaturesService;
|
2023-09-19 11:23:21 +02:00
|
|
|
this.openApiService = openApiService;
|
|
|
|
this.flagResolver = config.flagResolver;
|
2023-09-25 10:12:32 +02:00
|
|
|
this.startTransaction = startTransaction;
|
2023-09-19 11:23:21 +02:00
|
|
|
this.logger = config.getLogger(
|
|
|
|
'/dependent-features/dependent-feature-service.ts',
|
|
|
|
);
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: PATH_DEPENDENCIES,
|
|
|
|
handler: this.addFeatureDependency,
|
|
|
|
permission: CREATE_FEATURE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['Features'],
|
|
|
|
summary: 'Add a feature dependency.',
|
|
|
|
description:
|
|
|
|
'Add a dependency to a parent feature. Each environment will resolve corresponding dependency independently.',
|
|
|
|
operationId: 'addFeatureDependency',
|
|
|
|
requestBody: createRequestSchema(
|
|
|
|
'createDependentFeatureSchema',
|
|
|
|
),
|
|
|
|
responses: {
|
|
|
|
200: emptyResponse,
|
|
|
|
...getStandardResponses(401, 403, 404),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async addFeatureDependency(
|
|
|
|
req: IAuthRequest<FeatureParams, any, CreateDependentFeatureSchema>,
|
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
|
|
|
const { featureName } = req.params;
|
|
|
|
const { variants, enabled, feature } = req.body;
|
|
|
|
|
|
|
|
if (this.config.flagResolver.isEnabled('dependentFeatures')) {
|
2023-09-25 10:12:32 +02:00
|
|
|
await this.startTransaction(async (tx) =>
|
|
|
|
this.transactionalDependentFeaturesService(
|
|
|
|
tx,
|
|
|
|
).upsertFeatureDependency(featureName, {
|
2023-09-19 11:23:21 +02:00
|
|
|
variants,
|
|
|
|
enabled,
|
|
|
|
feature,
|
2023-09-25 10:12:32 +02:00
|
|
|
}),
|
2023-09-19 11:23:21 +02:00
|
|
|
);
|
|
|
|
res.status(200).end();
|
|
|
|
} else {
|
|
|
|
throw new InvalidOperationError(
|
|
|
|
'Dependent features are not enabled',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|