1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/src/lib/routes/admin-api/event.ts

68 lines
2.0 KiB
TypeScript
Raw Normal View History

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';
2022-05-30 16:44:37 +02:00
import { anonymise } from '../../util/anonymise';
const version = 1;
export default class EventController extends Controller {
private eventService: EventService;
2022-05-30 16:44:37 +02:00
private anonymise: boolean = false;
constructor(
config: IUnleashConfig,
{ eventService }: Pick<IUnleashServices, 'eventService'>,
) {
super(config);
this.eventService = eventService;
2022-05-30 16:44:37 +02:00
this.anonymise = config.experimental?.anonymiseEventLog;
this.get('/', this.getEvents, ADMIN);
this.get('/:name', this.getEventsForToggle);
}
2022-05-30 16:44:37 +02:00
fixEvents(events: IEvent[]): IEvent[] {
if (this.anonymise) {
return events.map((e: IEvent) => ({
...e,
createdBy: anonymise(e.createdBy),
}));
}
return events;
}
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();
}
2022-05-30 16:44:37 +02:00
res.json({
version,
events: this.fixEvents(events),
});
}
2014-11-14 07:13:08 +01:00
async getEventsForToggle(
req: Request<{ name: string }>,
res: Response,
): Promise<void> {
2016-12-05 17:58:29 +01:00
const toggleName = req.params.name;
const events = await this.eventService.getEventsForToggle(toggleName);
2022-05-30 16:44:37 +02:00
res.json({
version,
toggleName,
events: this.fixEvents(events),
});
}
}