2020-07-20 15:57:57 +02:00
|
|
|
|
|
|
|
import { COMMA, ENTER } from '@angular/cdk/keycodes';
|
2023-12-29 16:04:16 +01:00
|
|
|
import { Component, Inject, OnInit } from '@angular/core';
|
2023-10-05 15:39:17 +02:00
|
|
|
import { UntypedFormGroup } from '@angular/forms';
|
2023-10-06 10:10:53 +02:00
|
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
2020-07-20 15:57:57 +02:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
2023-12-29 16:04:16 +01:00
|
|
|
import { DmpUserRole } from '@app/core/common/enum/dmp-user-role';
|
2023-12-29 17:36:02 +01:00
|
|
|
import { IsActive } from '@app/core/common/enum/is-active.enum';
|
2024-01-05 14:33:27 +01:00
|
|
|
import { DmpUserInvitePersist, DmpUserInviteTypePersist } from '@app/core/model/dmp/dmp';
|
|
|
|
import { DmpAssociatedUser, User } from '@app/core/model/user/user';
|
2023-12-29 17:36:02 +01:00
|
|
|
import { UserLookup } from '@app/core/query/user.lookup';
|
|
|
|
import { DmpService } from '@app/core/services/dmp/dmp.service';
|
2023-12-29 16:04:16 +01:00
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
|
2023-12-29 17:36:02 +01:00
|
|
|
import { UserService } from '@app/core/services/user/user.service';
|
2023-12-29 16:04:16 +01:00
|
|
|
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
|
2020-07-20 15:57:57 +02:00
|
|
|
import { MultipleAutoCompleteConfiguration } from '@app/library/auto-complete/multiple/multiple-auto-complete-configuration';
|
|
|
|
import { BaseComponent } from '@common/base/base.component';
|
2023-12-29 17:36:02 +01:00
|
|
|
import { FilterService } from '@common/modules/text-filter/filter-service';
|
|
|
|
import { Guid } from '@common/types/guid';
|
2023-12-29 16:04:16 +01:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2020-07-20 15:57:57 +02:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
import { map, takeUntil } from 'rxjs/operators';
|
2023-12-29 17:36:02 +01:00
|
|
|
import { nameof } from 'ts-simple-nameof';
|
|
|
|
import { DmpInvitationDialogEditorModel } from './dmp-invitation-dialog.editor.model';
|
2020-07-20 15:57:57 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-invitation-dialog-component',
|
|
|
|
templateUrl: 'dmp-invitation-dialog.component.html',
|
|
|
|
styleUrls: ['./dmp-invitation-dialog.component.scss'],
|
|
|
|
})
|
|
|
|
export class DmpInvitationDialogComponent extends BaseComponent implements OnInit {
|
|
|
|
|
2023-12-29 17:36:02 +01:00
|
|
|
dmpId: Guid;
|
|
|
|
editorModel: DmpInvitationDialogEditorModel;
|
|
|
|
formGroup: UntypedFormGroup;
|
|
|
|
dmpUserRoleEnum = DmpUserRole;
|
2020-07-20 15:57:57 +02:00
|
|
|
readonly separatorKeysCodes: number[] = [ENTER, COMMA];
|
|
|
|
|
|
|
|
usersAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = {
|
|
|
|
filterFn: this.filterUsers.bind(this),
|
|
|
|
initialItems: (excludedItems: any[]) => this.filterUsers('').pipe(map(result => result.filter(resultItem => (excludedItems || []).map(x => x.id).indexOf(resultItem.id) === -1))),
|
2023-12-29 16:04:16 +01:00
|
|
|
displayFn: (item) => typeof (item) === 'string' ? item : item.name,
|
|
|
|
titleFn: (item) => typeof (item) === 'string' ? item : item.name,
|
2020-09-29 13:18:46 +02:00
|
|
|
subtitleFn: (item) => item.email,
|
2020-07-20 15:57:57 +02:00
|
|
|
valueAssign: (item) => {
|
2024-01-05 14:33:27 +01:00
|
|
|
const result = typeof (item) === 'string' ? item : item.id;
|
2020-07-20 15:57:57 +02:00
|
|
|
return result;
|
2021-07-21 10:09:13 +02:00
|
|
|
},
|
2021-07-21 11:16:22 +02:00
|
|
|
autoSelectFirstOptionOnBlur: true,
|
2023-12-29 16:04:16 +01:00
|
|
|
appendClassToItem: [{
|
|
|
|
class: 'invalid-email', applyFunc: (item) => {
|
|
|
|
const val = typeof (item) === 'string' ? item : item.email;
|
|
|
|
const regexp = new RegExp(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
|
|
|
|
return !regexp.test(val);
|
|
|
|
}
|
2021-07-21 11:16:22 +02:00
|
|
|
}]
|
2020-07-20 15:57:57 +02:00
|
|
|
};
|
|
|
|
|
2023-12-29 17:36:02 +01:00
|
|
|
constructor(
|
|
|
|
public enumUtils: EnumUtils,
|
|
|
|
public route: ActivatedRoute,
|
|
|
|
public router: Router,
|
|
|
|
private language: TranslateService,
|
|
|
|
public dialogRef: MatDialogRef<DmpInvitationDialogComponent>,
|
|
|
|
private uiNotificationService: UiNotificationService,
|
|
|
|
private dmpService: DmpService,
|
|
|
|
private userService: UserService,
|
|
|
|
private filterService: FilterService,
|
|
|
|
@Inject(MAT_DIALOG_DATA) public data: any
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
this.dmpId = data.dmpId;
|
2020-07-20 15:57:57 +02:00
|
|
|
}
|
|
|
|
|
2023-12-29 17:36:02 +01:00
|
|
|
ngOnInit() {
|
|
|
|
this.editorModel = new DmpInvitationDialogEditorModel();
|
|
|
|
this.formGroup = this.editorModel.buildForm();
|
2020-07-20 15:57:57 +02:00
|
|
|
}
|
|
|
|
|
2023-12-29 17:36:02 +01:00
|
|
|
send() {
|
|
|
|
if (!this.formGroup.valid) { return; }
|
2024-01-05 14:33:27 +01:00
|
|
|
let inviteTypeValues: DmpUserInviteTypePersist[] = [];
|
|
|
|
this.formGroup.get('users').value.forEach(x => {
|
|
|
|
let inviteType: DmpUserInviteTypePersist;
|
|
|
|
if (!(/^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$/).test(x)) {
|
|
|
|
inviteType = {
|
|
|
|
userId: null,
|
|
|
|
email: x
|
|
|
|
};
|
|
|
|
}else{
|
|
|
|
inviteType = {
|
|
|
|
userId: Guid.parse(x),
|
|
|
|
email: null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
inviteTypeValues.push(inviteType)
|
|
|
|
})
|
|
|
|
const value: DmpUserInvitePersist = {
|
|
|
|
role: this.formGroup.get('role').value,
|
|
|
|
users: inviteTypeValues
|
|
|
|
}
|
2023-12-29 17:36:02 +01:00
|
|
|
// invitationObject.users.push(...(<any[]>this.formGroup.get('users').value).filter(user => typeof (user) === 'string').map(email => ({ email: email, name: email })));
|
|
|
|
// invitationObject.users.push(...(<any[]>this.formGroup.get('users').value).filter(user => typeof (user) !== 'string'));
|
|
|
|
// this.emails.forEach(email => {
|
|
|
|
// invitationObject.users.push({ email: email, name: email });
|
|
|
|
// });
|
|
|
|
|
|
|
|
this.dmpService.inviteUsers(this.dmpId, value)
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
complete => {
|
|
|
|
this.dialogRef.close();
|
|
|
|
this.onCallbackSuccess();
|
|
|
|
},
|
|
|
|
error => this.onCallbackError(error)
|
|
|
|
);
|
2020-07-20 15:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
closeDialog(): void {
|
|
|
|
this.dialogRef.close();
|
|
|
|
}
|
|
|
|
|
2024-01-05 14:33:27 +01:00
|
|
|
filterUsers(like: string): Observable<DmpAssociatedUser[]> {
|
2023-12-29 17:36:02 +01:00
|
|
|
const lookup: UserLookup = new UserLookup();
|
|
|
|
lookup.page = { size: 100, offset: 0 };
|
|
|
|
// if (excludedIds && excludedIds.length > 0) { lookup.excludedIds = excludedIds; }
|
|
|
|
// if (ids && ids.length > 0) { lookup.ids = ids; }
|
|
|
|
lookup.isActive = [IsActive.Active];
|
|
|
|
lookup.project = {
|
|
|
|
fields: [
|
|
|
|
nameof<User>(x => x.id),
|
|
|
|
nameof<User>(x => x.name)
|
|
|
|
]
|
|
|
|
};
|
|
|
|
lookup.order = { items: [nameof<User>(x => x.name)] };
|
|
|
|
if (like) { lookup.like = this.filterService.transformLike(like); }
|
2024-01-05 14:33:27 +01:00
|
|
|
return this.userService.queryDmpAssociated(lookup).pipe(takeUntil(this._destroyed), map(x => x.items));
|
2020-07-20 15:57:57 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 13:18:46 +02:00
|
|
|
hasValue(): boolean {
|
|
|
|
return this.formGroup.get('users') && this.formGroup.get('users').value && this.formGroup.get('users').value.length > 0;
|
|
|
|
}
|
|
|
|
|
2020-10-02 18:00:35 +02:00
|
|
|
onCallbackSuccess(): void {
|
2023-12-29 16:04:16 +01:00
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.INVITATION-DIALOG.SUCCESS'), SnackBarNotificationLevel.Success);
|
2020-10-02 18:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackError(errorResponse: any) {
|
2023-12-29 16:04:16 +01:00
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.INVITATION-DIALOG.ERROR'), SnackBarNotificationLevel.Error);
|
2020-10-02 18:00:35 +02:00
|
|
|
}
|
2020-07-20 15:57:57 +02:00
|
|
|
}
|