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

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

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  59x           59x 59x                               142x 142x 142x   142x 142x 142x 142x 142x       1x 1x 1x             3x 3x 2x             3x 3x 3x   3x 1x       5x 5x 5x 4x             2x 2x 2x 1x     59x 59x  
import { Request, Response } from 'express';
import Controller from '../controller';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import { Logger } from '../../logger';
import AddonService from '../../services/addon-service';
 
import { extractUsername } from '../../util/extract-user';
import {
    CREATE_ADDON,
    UPDATE_ADDON,
    DELETE_ADDON,
} from '../../types/permissions';
import { IAuthRequest } from '../unleash-types';
 
class AddonController extends Controller {
    private logger: Logger;
 
    private addonService: AddonService;
 
    constructor(
        config: IUnleashConfig,
        { addonService }: Pick<IUnleashServices, 'addonService'>,
    ) {
        super(config);
        this.logger = config.getLogger('/admin-api/addon.ts');
        this.addonService = addonService;
 
        this.get('/', this.getAddons);
        this.post('/', this.createAddon, CREATE_ADDON);
        this.get('/:id', this.getAddon);
        this.put('/:id', this.updateAddon, UPDATE_ADDON);
        this.delete('/:id', this.deleteAddon, DELETE_ADDON);
    }
 
    async getAddons(req: Request, res: Response): Promise<void> {
        const addons = await this.addonService.getAddons();
        const providers = this.addonService.getProviderDefinitions();
        res.json({ addons, providers });
    }
 
    async getAddon(
        req: Request<{ id: number }, any, any, any>,
        res: Response,
    ): Promise<void> {
        const { id } = req.params;
        const addon = await this.addonService.getAddon(id);
        res.json(addon);
    }
 
    async updateAddon(
        req: IAuthRequest<{ id: number }, any, any, any>,
        res: Response,
    ): Promise<void> {
        const { id } = req.params;
        const createdBy = extractUsername(req);
        const data = req.body;
 
        const addon = await this.addonService.updateAddon(id, data, createdBy);
        res.status(200).json(addon);
    }
 
    async createAddon(req: IAuthRequest, res: Response): Promise<void> {
        const createdBy = extractUsername(req);
        const data = req.body;
        const addon = await this.addonService.createAddon(data, createdBy);
        res.status(201).json(addon);
    }
 
    async deleteAddon(
        req: IAuthRequest<{ id: number }, any, any, any>,
        res: Response,
    ): Promise<void> {
        const { id } = req.params;
        const username = extractUsername(req);
        await this.addonService.removeAddon(id, username);
        res.status(200).end();
    }
}
export default AddonController;
module.exports = AddonController;