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

94.44% Statements 17/18
75% Branches 3/4
100% Functions 3/3
94.44% Lines 17/18

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        59x   59x   59x   59x             142x 142x 142x 142x             4x   4x 1x   3x   4x             3x 3x   3x 3x            
import { Request, Response } from 'express';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import EventService from '../../services/event-service';
import { ADMIN } from '../../types/permissions';
import { IEvent } from '../../types/events';
import Controller from '../controller';
 
const version = 1;
 
export default class EventController extends Controller {
    private eventService: EventService;
 
    constructor(
        config: IUnleashConfig,
        { eventService }: Pick<IUnleashServices, 'eventService'>,
    ) {
        super(config);
        this.eventService = eventService;
        this.get('/', this.getEvents, ADMIN);
        this.get('/:name', this.getEventsForToggle);
    }
 
    async getEvents(
        req: Request<any, any, any, { project?: string }>,
        res: Response,
    ): Promise<void> {
        const { project } = req.query;
        let events: IEvent[];
        if (project) {
            events = await this.eventService.getEventsForProject(project);
        } else {
            events = await this.eventService.getEvents();
        }
        res.json({ version, events });
    }
 
    async getEventsForToggle(
        req: Request<{ name: string }>,
        res: Response,
    ): Promise<void> {
        const toggleName = req.params.name;
        const events = await this.eventService.getEventsForToggle(toggleName);
 
        if (events) {
            res.json({ toggleName, events });
        } else E{
            res.status(404).json({ error: 'Could not find events' });
        }
    }
}