Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
import { Component, OnInit, AfterViewInit, OnDestroy } from '@angular/core';
import { WindowRefService } from '../../window-ref.service';
import { MessageModel } from './../pdf-reference/pdf-reference.model';
import { EwvViewerService } from '../ewv-viewer/ewv-viewer.service';
import { MenuModel } from '../../shared/menu/menu.component.model';
import { Validators, FormGroup, FormBuilder } from '@angular/forms';
import {
TableColumnModel,
TableSettings,
PaginationSettings
} from '../../shared/table';
import {
UserRequestModel,
PaginationModel,
UserModel,
SearchModel
} from '../../ars/user-admin/user-admin.model';
import { Subscription } from 'rxjs/Subscription';
import { AuthenticationService } from '../../auth/auth.service';
import { EwvAdminService } from './ewv-admin.service';
import { AppSettingsService } from './../../shared/app-settings/app-settings.service';
import { CreateUserRequest } from './ewv-admin-model';
import { PdfReferenceService } from '../pdf-reference/pdf-reference.service';
const emailRx = /[a-zA-Z0-9._%+-]+@[a-zA-Z.-]{2,}[.][a-zA-Z]{2,}/;
@Component({
selector: 'app-ewv-admin',
templateUrl: './ewv-admin.component.html',
styleUrls: ['./ewv-admin.component.scss']
})
export class EwvAdminComponent implements OnInit, AfterViewInit, OnDestroy {
constructor(
private ewvViewerService: EwvViewerService,
private fb: FormBuilder,
private userAdminService: EwvAdminService,
private authenticationService: AuthenticationService,
private _window: WindowRefService,
private appSettingsService: AppSettingsService,
private pdfReferenceService: PdfReferenceService
) {}
title = 'EWV Administration';
ewvMenu: MenuModel;
errorStatus = false;
errorMsg = '';
successStatus = false;
successMsg = '';
deleteEvent;
archiveSuccessMsg = '';
infoStatus = false;
infoMsg = '';
archiveErrorMsg = '';
archiveSuccessStatus = false;
archiveErrorStatus = false;
modalClosed = true;
showResults = false;
searchUserClicked = false;
createForm: FormGroup;
searchForm: FormGroup;
modifyForm: FormGroup;
requestModel: CreateUserRequest = new CreateUserRequest();
editRequestModel: UserRequestModel = new UserRequestModel();
paginationModel: PaginationModel = new PaginationModel();
searchModel: SearchModel = new SearchModel();
searchResults: UserModel[] = [];
modalError = false;
loading = true;
subscription: Subscription = new Subscription();
enableEdit = false;
enableExport = false;
isAdmin = false;
status = true;
modifyEnableEdit = false;
modifyEnableExport = false;
modifyIsAdmin = false;
modifyStatus = true;
userValueBeforeChange: UserRequestModel;
tabIdx = '0';
tableColumns: TableColumnModel[] = [
new TableColumnModel('UserName', 'userName'),
new TableColumnModel('Domain ', 'domain'),
new TableColumnModel('Last Name', 'lastName'),
new TableColumnModel('First Name', 'firstName'),
new TableColumnModel('Phone Number', 'phone'),
new TableColumnModel('Email', 'email'),
new TableColumnModel('Status', 'status'),
new TableColumnModel('Admin', 'admin')
];
tableSettings: TableSettings = new TableSettings();
paginationSettings: PaginationSettings = new PaginationSettings();
ngOnInit() {
this.tableSettings.buttonShow = true;
this.tableSettings.buttonNames = ['Modify'];
this.formInit();
this.ewvViewerService.tabIdxNotifier$.subscribe(
tabIdx => (this.tabIdx = tabIdx)
);
}
ngAfterViewInit() {
setTimeout(() => {
this.setMenu();
});
}
setMenu() {
this.appSettingsService
.getMenu('EWV_MENU')
.subscribe(menu => (this.ewvMenu = menu));
this.pdfReferenceService.populatePdfReferenceMenu().subscribe(data => {
this.ewvMenu.dropDownGroups[1].dropdowns = data;
});
}
onUploadSuccess(uploadMsg: MessageModel) {
this.successMsg = uploadMsg.msg;
this.successStatus = uploadMsg.status;
this.setMenu();
}
onUploadError(uploadMsg: MessageModel) {
this.errorMsg = uploadMsg.msg;
this.errorStatus = uploadMsg.status;
}
onArchiveSuccess(archiveMsg: MessageModel) {
this.archiveSuccessMsg = archiveMsg.msg;
this.archiveSuccessStatus = archiveMsg.status;
this.setMenu();
}
onArchiveError(archiveMsg: MessageModel) {
this.archiveErrorMsg = archiveMsg.msg;
this.archiveErrorStatus = archiveMsg.status;
}
formInit() {
this.createForm = this.fb.group({
userName: [
this.requestModel.userName,
[Validators.required]
],
domain: this.requestModel.domain,
admin: this.isAdmin,
phone: [this.requestModel.phone, Validators.pattern('[0-9]*')],
email: [
this.requestModel.email,
[Validators.required, Validators.pattern(emailRx)]
],
firstName: this.requestModel.firstName,
lastName: this.requestModel.lastName,
status: this.status
});
this.searchForm = this.fb.group({
userName: [
this.searchModel.userName,
[Validators.required]
],
includeInactive: this.paginationModel.inActiveUsers
});
this.modifyForm = this.fb.group({
editDomain: this.editRequestModel.domain,
editAdmin: this.modifyIsAdmin,
editPhone: [this.editRequestModel.phone, Validators.pattern('[0-9]*')],
editEmail: [
this.editRequestModel.email,
[Validators.pattern(emailRx), Validators.required]
],
editFirstName: this.editRequestModel.firstName,
editLastName: this.editRequestModel.lastName,
editStatus: this.modifyStatus
});
}
search() {
this.searchUserClicked = true;
this.paginationSettings = new PaginationSettings();
this.fetchData();
}
fetchData() {
this.loading = true;
this.errorMsg = '';
this.errorStatus = false;
this.infoStatus = false;
this.infoMsg = '';
if (this.searchUserClicked) {
const searchUserSubscription = this.userAdminService
.getUserInfo(this.searchModel.userName.trim())
.subscribe(
data => {
this.showResults = true;
this.searchResults = [];
this.setUserRoles(data);
this.tableSettings.pagination = false;
this.errorMsg = '';
this.errorStatus = false;
this.infoStatus = false;
this.loading = false;
},
error => {
if (error.status === 400) {
this.infoStatus = true;
this.infoMsg = error.error.message;
} else {
this.errorMsg = error.error.message;
this.errorStatus = true;
}
this.showResults = false;
this.loading = false;
}
);
this.subscription.add(searchUserSubscription);
} else {
this.formInit();
this.searchModel = new SearchModel();
this.paginationModel.pageSize = this.paginationSettings.pageSize;
this.paginationModel.pageNumber = this.paginationSettings.currentPage;
this.paginationModel.descending = this.paginationSettings.descending;
this.paginationModel.sortColumn = this.paginationSettings.sortColumn;
this.paginationModel.system = 'EWV';
const allUserSubscription = this.userAdminService
.getAllUsers(this.paginationModel)
.subscribe(
data => {
this.showResults = true;
this.tableSettings.pagination = true;
this.setUsersRoles(data.response);
this.errorMsg = '';
this.errorStatus = false;
this.loading = false;
this.paginationSettings = {
currentPage: data.pageNumber,
pageSize: data.pageSize,
totalPages: Math.ceil(data.totalNumberOfResults / data.pageSize),
totalResults: data.totalNumberOfResults,
sortColumn: data.sortColumn,
descending: this.paginationModel.descending
};
},
error => {
this.loading = false;
this.errorMsg = error.statusText;
this.errorStatus = true;
}
);
this.subscription.add(allUserSubscription);
}
}
setUsersRoles(usersInfo: UserModel[]) {
this.searchResults = [];
usersInfo.forEach(user => {
user.admin = user.userRoles.some(role => role === 'EWV_ADMIN')
? 'Y'
: 'N';
user.status = user.userRoles.some(role => role === 'EWV_INACTIVE')
? 'Inactive'
: 'Active';
});
this.searchResults = usersInfo;
}
setUserRoles(user: UserModel) {
this.searchResults = [];
user.admin = user.userRoles.some(role => role === 'EWV_ADMIN') ? 'Y' : 'N';
user.status = user.userRoles.some(role => role === 'EWV_INACTIVE')
? 'Inactive'
: 'Active';
this.searchResults.push(user);
}
updateTable() {
this.fetchData();
}
listAllUsers() {
this.searchUserClicked = false;
this.clearMsgs();
this.paginationSettings = new PaginationSettings();
this.fetchData();
}
resetSearchForm() {
this.showResults = false;
this.searchUserClicked = false;
this.formInit();
this.clearMsgs();
this.searchModel = new SearchModel();
this.paginationModel = new PaginationModel();
}
clearMsgs() {
this.errorMsg = '';
this.errorStatus = false;
this.successMsg = '';
this.successStatus = false;
this.archiveErrorMsg = '';
this.archiveErrorStatus = false;
this.archiveSuccessMsg = '';
this.archiveSuccessStatus = false;
}
resetCreateForm() {
this.clearMsgs();
this.formInit();
this.enableEdit = false;
this.enableExport = false;
this.isAdmin = false;
this.status = true;
this.requestModel = new CreateUserRequest();
}
buttonClicked(event) {
// if (event.buttonName === 'Modify') {
this.openEditModal(event);
// }
// else {
// this.openDeleteModal(event);
// // this.delete(event);
// }
}
// openDeleteModal(event) {
// this.deleteModalClosed = false;
// this.deleteEvent = { ...event };
// }
openEditModal(event) {
this.editRequestModel = { ...event.row };
this.userValueBeforeChange = { ...event.row };
// Custom validator to check if initial values in the edit modal changed or not
this.setCustomValidator(this.modifyForm, this.userValueBeforeChange);
this.modalClosed = false;
this.tabIdx = '-1';
this.modifyStatus = !this.editRequestModel.userRoles.some(
role => role === 'EWV_INACTIVE'
);
this.modifyIsAdmin = this.editRequestModel.userRoles.some(
role => role === 'EWV_ADMIN'
);
}
// delete() {
// const deleteSubscription = this.userAdminService
// .deleteUserInfo(this.deleteEvent.row.userId)
// .subscribe(data => {
// if (data.errorCode) {
// this.errorMsg = 'An error occurred during user deletion.';
// this.errorStatus = true;
// this.successStatus = false;
// this.successMsg = '';
// this.showResults = false;
// } else {
// if (this.searchUserClicked) {
// this.showResults = false;
// } else {
// this.successMsg = 'User was successfully deleted.';
// this.successStatus = true;
// this.errorMsg = '';
// this.errorStatus = false;
// this.showResults = true;
// this.listAllUsers();
// this.onDeleteModalClose();
// }
// }
// });
// this.subscription.add(deleteSubscription);
// }
// need to find initialValueChanged
setCustomValidator(form: FormGroup, initialObject: UserRequestModel) {
// form.setValidators([initialValueChanged(initialObject)]);
form.updateValueAndValidity();
}
// Creates user on submitting create user form
createUser() {
this.errorMsg = '';
this.errorStatus = false;
this.successMsg = '';
this.infoStatus = false;
this.infoMsg = '';
this.successStatus = false;
// this.requestModel.status = YN(this.status);
// this.requestModel.admin = YN(this.isAdmin);
this.setRequestRoles(true);
// this.requestModel.system = 'EWV';
this.requestModel.createdBy = this.authenticationService.getDecodedUserInfo().userName;
const createSubscription = this.userAdminService
.createUser(this.requestModel)
.subscribe(
data => {
this.formInit();
this.enableEdit = false;
this.enableExport = false;
this.isAdmin = false;
this.status = true;
this.requestModel = new CreateUserRequest();
this.successMsg = 'User was successfully created.';
this.successStatus = true;
this.errorMsg = '';
this.errorStatus = false;
},
error => {
if (error.status = 405) {
this.infoStatus = true;
this.infoMsg = error.error.message;
} else {
this.errorMsg = error.error.message;
this.errorStatus = true;
this.successMsg = '';
this.successStatus = false;
}
}
);
this.subscription.add(createSubscription);
}
modifyUser() {
this.modalClosed = true;
this.tabIdx = '0';
this.setRequestRoles(false);
this.editRequestModel.system = 'EWV';
this.editRequestModel.modifiedBy = this.authenticationService.getDecodedUserInfo().userName;
const modifySubscription = this.userAdminService
.modifyUser(this.editRequestModel)
.subscribe(
data => {
this.successMsg = 'User was successfully modified.';
this.successStatus = true;
this.errorMsg = '';
this.errorStatus = false;
if (!this.searchUserClicked) {
// Clears search form
this.formInit();
this.searchModel = new SearchModel();
this.paginationSettings = new PaginationSettings();
this.fetchData();
} else {
this.fetchData();
}
},
error => {
this.errorMsg = 'An error occurred during user modification.';
this.errorStatus = true;
this.successMsg = '';
this.successStatus = false;
}
);
this.subscription.add(modifySubscription);
}
setRequestRoles(isCreate: boolean) {
if (isCreate) {
this.requestModel.userRoles = ['EWV_BASE_USER'];
if (this.isAdmin) {
this.requestModel.userRoles.push('EWV_ADMIN');
}
if (!this.status) {
this.requestModel.userRoles.push('EWV_INACTIVE');
}
} else {
this.editRequestModel.userRoles = ['EWV_BASE_USER'];
if (this.modifyIsAdmin) {
this.editRequestModel.userRoles.push('EWV_ADMIN');
}
if (!this.modifyStatus) {
this.editRequestModel.userRoles.push('EWV_INACTIVE');
}
}
}
onModalClose(event) {
this.tabIdx = '0';
this.modalClosed = true;
}
// onDeleteModalClose() {
// this.deleteModalClosed = true;
// this.deleteEvent = null;
// }
// Resets Edit Modal
resetModal() {
this.editRequestModel = { ...this.userValueBeforeChange };
this.modalClosed = true;
this.tabIdx = '0';
}
getPdfFile(guid: number) {
this.ewvViewerService.getPdfFile(guid).subscribe(data => {
const fileName = data.headers.get('Content-Disposition');
const pdfBlob = new Blob([data.blob()], { type: 'application/pdf' });
const pdfURL = this._window.nativeWindow.URL.createObjectURL(pdfBlob);
if (
this._window.nativeWindow.navigator &&
this._window.nativeWindow.navigator.msSaveOrOpenBlob
) {
this._window.nativeWindow.navigator.msSaveOrOpenBlob(pdfBlob, fileName);
return;
}
this._window.nativeWindow.open(pdfURL);
});
}
notify(evt) {
if (evt.guid) {
this.getPdfFile(evt.guid);
}
}
// Unsubscribe all subscriptions
ngOnDestroy() {
this.subscription.unsubscribe();
}
}