All files / src/lib/routes/admin-api context.ts

97.37% Statements 37/38
100% Branches 0/0
100% Functions 7/7
97.37% Lines 37/38

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 89 90 91 92 93 94 95 96 97 98 99 100    59x   59x   59x                                       142x 142x 142x   142x 142x 142x 142x         142x         142x       2x 2x       6x 6x 6x     6x             12x 12x   12x 6x       6x 6x 6x   6x   6x 3x       3x 3x   3x 2x       5x   5x 2x     59x 59x  
import { Request, Response } from 'express';
 
import Controller from '../controller';
 
import { extractUsername } from '../../util/extract-user';
 
import {
    CREATE_CONTEXT_FIELD,
    UPDATE_CONTEXT_FIELD,
    DELETE_CONTEXT_FIELD,
} from '../../types/permissions';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import ContextService from '../../services/context-service';
import { Logger } from '../../logger';
import { IAuthRequest } from '../unleash-types';
 
class ContextController extends Controller {
    private logger: Logger;
 
    private contextService: ContextService;
 
    constructor(
        config: IUnleashConfig,
        { contextService }: Pick<IUnleashServices, 'contextService'>,
    ) {
        super(config);
        this.logger = config.getLogger('/admin-api/context.ts');
        this.contextService = contextService;
 
        this.get('/', this.getContextFields);
        this.post('/', this.createContextField, CREATE_CONTEXT_FIELD);
        this.get('/:contextField', this.getContextField);
        this.put(
            '/:contextField',
            this.updateContextField,
            UPDATE_CONTEXT_FIELD,
        );
        this.delete(
            '/:contextField',
            this.deleteContextField,
            DELETE_CONTEXT_FIELD,
        );
        this.post('/validate', this.validate, UPDATE_CONTEXT_FIELD);
    }
 
    async getContextFields(req: Request, res: Response): Promise<void> {
        const fields = await this.contextService.getAll();
        res.status(200).json(fields).end();
    }
 
    async getContextField(req: Request, res: Response): Promise<void> {
        try {
            const name = req.params.contextField;
            const contextField = await this.contextService.getContextField(
                name,
            );
            res.json(contextField).end();
        } catch (err) {
            res.status(404).json({ error: 'Could not find context field' });
        }
    }
 
    async createContextField(req: IAuthRequest, res: Response): Promise<void> {
        const value = req.body;
        const userName = extractUsername(req);
 
        await this.contextService.createContextField(value, userName);
        res.status(201).end();
    }
 
    async updateContextField(req: IAuthRequest, res: Response): Promise<void> {
        const name = req.params.contextField;
        const userName = extractUsername(req);
        const contextField = req.body;
 
        contextField.name = name;
 
        await this.contextService.updateContextField(contextField, userName);
        res.status(200).end();
    }
 
    async deleteContextField(req: IAuthRequest, res: Response): Promise<void> {
        const name = req.params.contextField;
        const userName = extractUsername(req);
 
        await this.contextService.deleteContextField(name, userName);
        res.status(200).end();
    }
 
    async validate(req: Request, res: Response): Promise<void> {
        const { name } = req.body;
 
        await this.contextService.validateName(name);
        res.status(200).end();
    }
}
export default ContextController;
module.exports = ContextController;