All files / src/lib/routes/admin-api tag-type.ts

100% Statements 34/34
100% Branches 0/0
100% Functions 7/7
100% Lines 34/34

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  59x   59x 59x             59x                     141x 141x 141x 141x 141x 141x 141x 141x 141x       1x 1x       2x 1x       5x 5x       3x       2x 2x 2x   2x       1x       7x   7x 5x       1x 1x 1x 1x     59x 59x  
import { Request, Response } from 'express';
import Controller from '../controller';
 
import { DELETE_TAG_TYPE, UPDATE_TAG_TYPE } from '../../types/permissions';
import { extractUsername } from '../../util/extract-user';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import TagTypeService from '../../services/tag-type-service';
import { Logger } from '../../logger';
import { IAuthRequest } from '../unleash-types';
 
const version = 1;
 
class TagTypeController extends Controller {
    private logger: Logger;
 
    private tagTypeService: TagTypeService;
 
    constructor(
        config: IUnleashConfig,
        { tagTypeService }: Pick<IUnleashServices, 'tagTypeService'>,
    ) {
        super(config);
        this.logger = config.getLogger('/admin-api/tag-type.js');
        this.tagTypeService = tagTypeService;
        this.get('/', this.getTagTypes);
        this.post('/', this.createTagType, UPDATE_TAG_TYPE);
        this.post('/validate', this.validate, UPDATE_TAG_TYPE);
        this.get('/:name', this.getTagType);
        this.put('/:name', this.updateTagType, UPDATE_TAG_TYPE);
        this.delete('/:name', this.deleteTagType, DELETE_TAG_TYPE);
    }
 
    async getTagTypes(req: Request, res: Response): Promise<void> {
        const tagTypes = await this.tagTypeService.getAll();
        res.json({ version, tagTypes });
    }
 
    async validate(req: Request, res: Response): Promise<void> {
        await this.tagTypeService.validate(req.body);
        res.status(200).json({ valid: true, tagType: req.body });
    }
 
    async createTagType(req: IAuthRequest, res: Response): Promise<void> {
        const userName = extractUsername(req);
        const tagType = await this.tagTypeService.createTagType(
            req.body,
            userName,
        );
        res.status(201).json(tagType);
    }
 
    async updateTagType(req: IAuthRequest, res: Response): Promise<void> {
        const { description, icon } = req.body;
        const { name } = req.params;
        const userName = extractUsername(req);
 
        await this.tagTypeService.updateTagType(
            { name, description, icon },
            userName,
        );
        res.status(200).end();
    }
 
    async getTagType(req: Request, res: Response): Promise<void> {
        const { name } = req.params;
 
        const tagType = await this.tagTypeService.getTagType(name);
        res.json({ version, tagType });
    }
 
    async deleteTagType(req: IAuthRequest, res: Response): Promise<void> {
        const { name } = req.params;
        const userName = extractUsername(req);
        await this.tagTypeService.deleteTagType(name, userName);
        res.status(200).end();
    }
}
export default TagTypeController;
module.exports = TagTypeController;