All files / src/lib/routes/admin-api/project variants.ts

100% Statements 22/22
50% Branches 1/2
100% Functions 4/4
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88    59x         59x   59x     59x                   59x                     141x 141x 141x 141x 141x 141x             8x 8x 7x             17x 17x   17x           12x                   7x 7x 7x           4x            
import FeatureToggleService from '../../../services/feature-toggle-service';
import { Logger } from '../../../logger';
import Controller from '../../controller';
import { IUnleashConfig } from '../../../types/option';
import { IUnleashServices } from '../../../types';
import { Request, Response } from 'express';
import { Operation } from 'fast-json-patch';
import { UPDATE_FEATURE_VARIANTS } from '../../../types/permissions';
import { IVariant } from '../../../types/model';
import { extractUsername } from '../../../util/extract-user';
import { IAuthRequest } from '../../unleash-types';
 
const PREFIX = '/:projectId/features/:featureName/variants';
 
interface FeatureParams extends ProjectParam {
    featureName: string;
}
 
interface ProjectParam {
    projectId: string;
}
 
export default class VariantsController extends Controller {
    private logger: Logger;
 
    private featureService: FeatureToggleService;
 
    constructor(
        config: IUnleashConfig,
        {
            featureToggleService,
        }: Pick<IUnleashServices, 'featureToggleService'>,
    ) {
        super(config);
        this.logger = config.getLogger('admin-api/project/variants.ts');
        this.featureService = featureToggleService;
        this.get(PREFIX, this.getVariants);
        this.patch(PREFIX, this.patchVariants, UPDATE_FEATURE_VARIANTS);
        this.put(PREFIX, this.overwriteVariants, UPDATE_FEATURE_VARIANTS);
    }
 
    async getVariants(
        req: Request<FeatureParams, any, any, any>,
        res: Response,
    ): Promise<void> {
        const { featureName } = req.params;
        const variants = await this.featureService.getVariants(featureName);
        res.status(200).json({ version: '1', variants: variants || [] });
    }
 
    async patchVariants(
        req: IAuthRequest<FeatureParams, any, Operation[]>,
        res: Response,
    ): Promise<void> {
        const { projectId, featureName } = req.params;
        const userName = extractUsername(req);
 
        const updatedFeature = await this.featureService.updateVariants(
            featureName,
            projectId,
            req.body,
            userName,
        );
        res.status(200).json({
            version: '1',
            variants: updatedFeature.variants,
        });
    }
 
    async overwriteVariants(
        req: IAuthRequest<FeatureParams, any, IVariant[], any>,
        res: Response,
    ): Promise<void> {
        const { projectId, featureName } = req.params;
        const userName = extractUsername(req);
        const updatedFeature = await this.featureService.saveVariants(
            featureName,
            projectId,
            req.body,
            userName,
        );
        res.status(200).json({
            version: '1',
            variants: updatedFeature.variants,
        });
    }
}