bitwarden-estensione-browser/src/app/organizations/manage/events.component.ts

124 lines
4.4 KiB
TypeScript
Raw Normal View History

2018-07-07 05:08:10 +02:00
import {
Component,
OnInit,
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
2018-07-07 05:08:10 +02:00
2018-07-09 18:22:32 +02:00
import { ToasterService } from 'angular2-toaster';
2018-07-07 05:08:10 +02:00
import { ApiService } from 'jslib/abstractions/api.service';
2018-07-09 18:22:32 +02:00
import { I18nService } from 'jslib/abstractions/i18n.service';
import { UserService } from 'jslib/abstractions/user.service';
2018-07-07 05:08:10 +02:00
import { EventService } from '../../services/event.service';
2018-07-06 16:21:08 +02:00
2018-07-09 17:47:57 +02:00
import { EventResponse } from 'jslib/models/response/eventResponse';
import { ListResponse } from 'jslib/models/response/listResponse';
2018-07-06 16:21:08 +02:00
@Component({
selector: 'app-org-events',
templateUrl: 'events.component.html',
})
2018-07-07 05:08:10 +02:00
export class EventsComponent implements OnInit {
loading = true;
loaded = false;
organizationId: string;
events: any[];
start: string;
end: string;
continuationToken: string;
refreshPromise: Promise<any>;
morePromise: Promise<any>;
2018-07-09 18:22:32 +02:00
private orgUsersUserIdMap = new Map<string, any>();
private orgUsersIdMap = new Map<string, any>();
2018-07-07 05:08:10 +02:00
constructor(private apiService: ApiService, private route: ActivatedRoute,
2018-07-09 18:22:32 +02:00
private eventService: EventService, private i18nService: I18nService,
private toasterService: ToasterService, private userService: UserService,
private router: Router) { }
2018-07-07 05:08:10 +02:00
async ngOnInit() {
this.route.parent.parent.params.subscribe(async (params) => {
this.organizationId = params.organizationId;
const organization = await this.userService.getOrganization(this.organizationId);
if (organization == null || !organization.useEvents) {
this.router.navigate(['/organizations', this.organizationId]);
return;
}
2018-07-07 05:08:10 +02:00
const defaultDates = this.eventService.getDefaultDateFilters();
this.start = defaultDates[0];
this.end = defaultDates[1];
await this.load();
});
}
async load() {
2018-07-09 18:22:32 +02:00
const response = await this.apiService.getOrganizationUsers(this.organizationId);
response.data.forEach((u) => {
const name = u.name == null || u.name.trim() === '' ? u.email : u.name;
this.orgUsersIdMap.set(u.id, { name: name, email: u.email });
this.orgUsersUserIdMap.set(u.userId, { name: name, email: u.email });
});
2018-07-07 05:08:10 +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) {
2018-07-09 18:22:32 +02:00
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('invalidDateRange'));
2018-07-07 05:08:10 +02:00
return;
}
this.loading = true;
2018-07-09 17:47:57 +02:00
let response: ListResponse<EventResponse>;
2018-07-07 05:08:10 +02:00
try {
const promise = this.apiService.getEventsOrganization(this.organizationId, dates[0], dates[1],
clearExisting ? null : this.continuationToken);
if (clearExisting) {
this.refreshPromise = promise;
} else {
this.morePromise = promise;
}
2018-07-09 17:47:57 +02:00
response = await promise;
2018-07-07 05:08:10 +02:00
} catch { }
2018-07-09 17:47:57 +02:00
this.continuationToken = response.continuationToken;
const events = response.data.map((r) => {
const userId = r.actingUserId == null ? r.userId : r.actingUserId;
const eventInfo = this.eventService.getEventInfo(r);
2018-07-09 18:22:32 +02:00
const user = userId != null && this.orgUsersUserIdMap.has(userId) ?
this.orgUsersUserIdMap.get(userId) : null;
2018-07-09 17:47:57 +02:00
return {
message: eventInfo.message,
appIcon: eventInfo.appIcon,
appName: eventInfo.appName,
userId: userId,
2018-07-09 18:22:32 +02:00
userName: user != null ? user.name : this.i18nService.t('unknown'),
userEmail: user != null ? user.email : '',
2018-07-09 17:47:57 +02:00
date: r.date,
ip: r.ipAddress,
2018-07-10 14:39:05 +02:00
type: r.type,
2018-07-09 17:47:57 +02:00
};
});
if (!clearExisting && this.events != null && this.events.length > 0) {
this.events = this.events.concat(events);
} else {
this.events = events;
}
2018-07-07 05:08:10 +02:00
this.loading = false;
this.morePromise = null;
this.refreshPromise = null;
}
}