/
event-service.ts
57 lines (48 loc) · 1.62 KB
/
event-service.ts
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
import { IUnleashConfig } from '../types/option';
import { IUnleashStores } from '../types/stores';
import { Logger } from '../logger';
import { IEventStore } from '../types/stores/event-store';
import { IEventList } from '../types/events';
import { SearchEventsSchema } from '../openapi/spec/search-events-schema';
export default class EventService {
private logger: Logger;
private eventStore: IEventStore;
private revisionId: number;
constructor(
{ eventStore }: Pick<IUnleashStores, 'eventStore'>,
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
) {
this.logger = getLogger('services/event-service.ts');
this.eventStore = eventStore;
}
async getEvents(): Promise<IEventList> {
let totalEvents = await this.eventStore.count();
let events = await this.eventStore.getEvents();
return {
events,
totalEvents,
};
}
async searchEvents(search: SearchEventsSchema): Promise<IEventList> {
let totalEvents = await this.eventStore.filteredCount(search);
let events = await this.eventStore.searchEvents(search);
return {
events,
totalEvents,
};
}
async getMaxRevisionId(): Promise<number> {
if (this.revisionId) {
return this.revisionId;
} else {
return this.updateMaxRevisionId();
}
}
async updateMaxRevisionId(): Promise<number> {
this.revisionId = await this.eventStore.getMaxRevisionId(
this.revisionId,
);
return this.revisionId;
}
}
module.exports = EventService;