2018-07-11 20:43:00 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
Input,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-20 18:30:04 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2018-07-11 20:43:00 +02:00
|
|
|
|
|
|
|
import { EventService } from '../../services/event.service';
|
|
|
|
|
2021-07-19 10:47:34 +02:00
|
|
|
import { UserNamePipe } from 'jslib-angular/pipes/user-name.pipe';
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { EventResponse } from 'jslib-common/models/response/eventResponse';
|
|
|
|
import { ListResponse } from 'jslib-common/models/response/listResponse';
|
2018-07-11 20:43:00 +02:00
|
|
|
|
|
|
|
@Component({
|
2018-07-11 21:22:55 +02:00
|
|
|
selector: 'app-entity-events',
|
|
|
|
templateUrl: 'entity-events.component.html',
|
2018-07-11 20:43:00 +02:00
|
|
|
})
|
2018-07-11 21:22:55 +02:00
|
|
|
export class EntityEventsComponent implements OnInit {
|
2018-07-11 20:43:00 +02:00
|
|
|
@Input() name: string;
|
2018-07-11 21:22:55 +02:00
|
|
|
@Input() entity: 'user' | 'cipher';
|
|
|
|
@Input() entityId: string;
|
2018-07-11 20:43:00 +02:00
|
|
|
@Input() organizationId: string;
|
2021-07-21 11:32:27 +02:00
|
|
|
@Input() providerId: string;
|
2018-07-11 21:22:55 +02:00
|
|
|
@Input() showUser = false;
|
2018-07-11 20:43:00 +02:00
|
|
|
|
|
|
|
loading = true;
|
|
|
|
loaded = false;
|
|
|
|
events: any[];
|
|
|
|
start: string;
|
|
|
|
end: string;
|
|
|
|
continuationToken: string;
|
|
|
|
refreshPromise: Promise<any>;
|
|
|
|
morePromise: Promise<any>;
|
|
|
|
|
2018-07-11 21:22:55 +02:00
|
|
|
private orgUsersUserIdMap = new Map<string, any>();
|
|
|
|
private orgUsersIdMap = new Map<string, any>();
|
|
|
|
|
2018-07-11 20:43:00 +02:00
|
|
|
constructor(private apiService: ApiService, private i18nService: I18nService,
|
2021-07-19 10:47:34 +02:00
|
|
|
private eventService: EventService, private toasterService: ToasterService,
|
2021-10-20 18:30:04 +02:00
|
|
|
private userNamePipe: UserNamePipe, private logService: LogService) { }
|
2018-07-11 20:43:00 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
const defaultDates = this.eventService.getDefaultDateFilters();
|
|
|
|
this.start = defaultDates[0];
|
|
|
|
this.end = defaultDates[1];
|
2018-07-11 21:22:55 +02:00
|
|
|
await this.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
if (this.showUser) {
|
|
|
|
const response = await this.apiService.getOrganizationUsers(this.organizationId);
|
2021-02-03 18:41:33 +01:00
|
|
|
response.data.forEach(u => {
|
2021-07-19 10:47:34 +02:00
|
|
|
const name = this.userNamePipe.transform(u);
|
2018-07-11 21:22:55 +02:00
|
|
|
this.orgUsersIdMap.set(u.id, { name: name, email: u.email });
|
|
|
|
this.orgUsersUserIdMap.set(u.userId, { name: name, email: u.email });
|
|
|
|
});
|
|
|
|
}
|
2018-07-11 20:43:00 +02:00
|
|
|
await this.loadEvents(true);
|
|
|
|
this.loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadEvents(clearExisting: boolean) {
|
|
|
|
if (this.refreshPromise != null || this.morePromise != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let dates: string[] = null;
|
|
|
|
try {
|
|
|
|
dates = this.eventService.formatDateFilters(this.start, this.end);
|
|
|
|
} catch (e) {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('invalidDateRange'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loading = true;
|
|
|
|
let response: ListResponse<EventResponse>;
|
|
|
|
try {
|
2018-07-11 21:22:55 +02:00
|
|
|
let promise: Promise<any>;
|
2021-07-21 11:32:27 +02:00
|
|
|
if (this.entity === 'user' && this.providerId) {
|
|
|
|
promise = this.apiService.getEventsProviderUser(this.providerId, this.entityId,
|
|
|
|
dates[0], dates[1], clearExisting ? null : this.continuationToken);
|
|
|
|
} else if (this.entity === 'user') {
|
2018-07-11 21:22:55 +02:00
|
|
|
promise = this.apiService.getEventsOrganizationUser(this.organizationId, this.entityId,
|
|
|
|
dates[0], dates[1], clearExisting ? null : this.continuationToken);
|
|
|
|
} else {
|
|
|
|
promise = this.apiService.getEventsCipher(this.entityId,
|
|
|
|
dates[0], dates[1], clearExisting ? null : this.continuationToken);
|
|
|
|
}
|
2018-07-11 20:43:00 +02:00
|
|
|
if (clearExisting) {
|
|
|
|
this.refreshPromise = promise;
|
|
|
|
} else {
|
|
|
|
this.morePromise = promise;
|
|
|
|
}
|
|
|
|
response = await promise;
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2018-07-11 20:43:00 +02:00
|
|
|
|
|
|
|
this.continuationToken = response.continuationToken;
|
2021-05-14 01:39:53 +02:00
|
|
|
const events = await Promise.all(response.data.map(async r => {
|
2018-07-11 20:43:00 +02:00
|
|
|
const userId = r.actingUserId == null ? r.userId : r.actingUserId;
|
2021-05-14 01:39:53 +02:00
|
|
|
const eventInfo = await this.eventService.getEventInfo(r);
|
2018-07-11 21:22:55 +02:00
|
|
|
const user = this.showUser && userId != null && this.orgUsersUserIdMap.has(userId) ?
|
|
|
|
this.orgUsersUserIdMap.get(userId) : null;
|
2018-07-11 20:43:00 +02:00
|
|
|
return {
|
|
|
|
message: eventInfo.message,
|
|
|
|
appIcon: eventInfo.appIcon,
|
|
|
|
appName: eventInfo.appName,
|
|
|
|
userId: userId,
|
2018-07-11 21:22:55 +02:00
|
|
|
userName: user != null ? user.name : this.showUser ? this.i18nService.t('unknown') : null,
|
|
|
|
userEmail: user != null ? user.email : this.showUser ? '' : null,
|
2018-07-11 20:43:00 +02:00
|
|
|
date: r.date,
|
|
|
|
ip: r.ipAddress,
|
|
|
|
type: r.type,
|
|
|
|
};
|
2021-05-14 01:39:53 +02:00
|
|
|
}));
|
2018-07-11 20:43:00 +02:00
|
|
|
|
|
|
|
if (!clearExisting && this.events != null && this.events.length > 0) {
|
|
|
|
this.events = this.events.concat(events);
|
|
|
|
} else {
|
|
|
|
this.events = events;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loading = false;
|
|
|
|
this.morePromise = null;
|
|
|
|
this.refreshPromise = null;
|
|
|
|
}
|
|
|
|
}
|