2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../../types/option';
|
|
|
|
import { IUnleashServices } from '../../types/services';
|
|
|
|
import EventService from '../../services/event-service';
|
2021-05-04 22:11:30 +02:00
|
|
|
import { ADMIN } from '../../types/permissions';
|
2021-02-17 15:24:43 +01:00
|
|
|
|
2018-11-30 10:11:36 +01:00
|
|
|
const Controller = require('../controller');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
|
|
|
const eventDiffer = require('../../event-differ');
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2016-09-05 16:50:52 +02:00
|
|
|
const version = 1;
|
2014-10-24 15:32:33 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
export default class EventController extends Controller {
|
|
|
|
private eventService: EventService;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
{ eventService }: Pick<IUnleashServices, 'eventService'>,
|
|
|
|
) {
|
2018-12-19 14:50:01 +01:00
|
|
|
super(config);
|
2021-04-22 10:07:10 +02:00
|
|
|
this.eventService = eventService;
|
2021-05-04 22:11:30 +02:00
|
|
|
this.get('/', this.getEvents, ADMIN);
|
2018-11-30 10:11:36 +01:00
|
|
|
this.get('/:name', this.getEventsForToggle);
|
|
|
|
}
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async getEvents(req, res): Promise<void> {
|
2021-09-20 12:13:38 +02:00
|
|
|
let events;
|
|
|
|
if (req.query?.project) {
|
|
|
|
events = await this.eventService.getEventsForProject(
|
|
|
|
req.query.project,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
events = await this.eventService.getEvents();
|
|
|
|
}
|
2021-08-13 10:36:19 +02:00
|
|
|
eventDiffer.addDiffs(events);
|
|
|
|
res.json({ version, events });
|
2018-11-30 10:11:36 +01:00
|
|
|
}
|
2014-11-14 07:13:08 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async getEventsForToggle(req, res): Promise<void> {
|
2016-12-05 17:58:29 +01:00
|
|
|
const toggleName = req.params.name;
|
2021-08-13 10:36:19 +02:00
|
|
|
const events = await this.eventService.getEventsForToggle(toggleName);
|
|
|
|
|
|
|
|
if (events) {
|
|
|
|
eventDiffer.addDiffs(events);
|
|
|
|
res.json({ toggleName, events });
|
|
|
|
} else {
|
|
|
|
res.status(404).json({ error: 'Could not find events' });
|
2018-11-30 10:11:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-30 10:11:36 +01:00
|
|
|
module.exports = EventController;
|