All files / src/lib/routes/admin-api environments-controller.ts

100% Statements 24/24
100% Branches 0/0
100% Functions 6/6
100% Lines 24/24

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  59x           59x           59x                 142x 142x 142x 142x 142x 142x 142x 142x       2x 2x             2x 1x             2x 2x 1x             2x 2x 1x             2x   2x 1x      
import { Request, Response } from 'express';
import Controller from '../controller';
import { IUnleashServices } from '../../types/services';
import { IUnleashConfig } from '../../types/option';
import { ISortOrder } from '../../types/model';
import EnvironmentService from '../../services/environment-service';
import { Logger } from '../../logger';
import { ADMIN } from '../../types/permissions';
 
interface EnvironmentParam {
    name: string;
}
 
export class EnvironmentsController extends Controller {
    private logger: Logger;
 
    private service: EnvironmentService;
 
    constructor(
        config: IUnleashConfig,
        { environmentService }: Pick<IUnleashServices, 'environmentService'>,
    ) {
        super(config);
        this.logger = config.getLogger('admin-api/environments-controller.ts');
        this.service = environmentService;
        this.get('/', this.getAll);
        this.put('/sort-order', this.updateSortOrder, ADMIN);
        this.get('/:name', this.getEnv);
        this.post('/:name/on', this.toggleEnvironmentOn, ADMIN);
        this.post('/:name/off', this.toggleEnvironmentOff, ADMIN);
    }
 
    async getAll(req: Request, res: Response): Promise<void> {
        const environments = await this.service.getAll();
        res.status(200).json({ version: 1, environments });
    }
 
    async updateSortOrder(
        req: Request<any, any, ISortOrder, any>,
        res: Response,
    ): Promise<void> {
        await this.service.updateSortOrder(req.body);
        res.status(200).end();
    }
 
    async toggleEnvironmentOn(
        req: Request<EnvironmentParam, any, any, any>,
        res: Response,
    ): Promise<void> {
        const { name } = req.params;
        await this.service.toggleEnvironment(name, true);
        res.status(204).end();
    }
 
    async toggleEnvironmentOff(
        req: Request<EnvironmentParam, any, any, any>,
        res: Response,
    ): Promise<void> {
        const { name } = req.params;
        await this.service.toggleEnvironment(name, false);
        res.status(204).end();
    }
 
    async getEnv(
        req: Request<EnvironmentParam, any, any, any>,
        res: Response,
    ): Promise<void> {
        const { name } = req.params;
 
        const env = await this.service.get(name);
        res.status(200).json(env);
    }
}