From 2a6003e4aa6fdd12ea44acca2929cc0f0a24d6a6 Mon Sep 17 00:00:00 2001 From: Valentin Date: Wed, 27 Oct 2021 17:59:22 +0300 Subject: [PATCH] updated the way of how users are automatically assigned as reviewers or approvers based on the ticket rules --- ...sign-reviewer-approver-dialog.component.ts | 7 ++- .../file-actions/file-actions.component.ts | 10 +-- .../shared/services/file-action.service.ts | 62 +++++++++++++------ 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/apps/red-ui/src/app/modules/dossier/dialogs/assign-reviewer-approver-dialog/assign-reviewer-approver-dialog.component.ts b/apps/red-ui/src/app/modules/dossier/dialogs/assign-reviewer-approver-dialog/assign-reviewer-approver-dialog.component.ts index 8f12cc5b9..277be4ad2 100644 --- a/apps/red-ui/src/app/modules/dossier/dialogs/assign-reviewer-approver-dialog/assign-reviewer-approver-dialog.component.ts +++ b/apps/red-ui/src/app/modules/dossier/dialogs/assign-reviewer-approver-dialog/assign-reviewer-approver-dialog.component.ts @@ -15,6 +15,7 @@ class DialogData { dossier?: Dossier; files?: File[]; ignoreChanged?: boolean; + withCurrentUserAsDefault?: boolean; } @Component({ @@ -111,10 +112,14 @@ export class AssignReviewerApproverDialogComponent { uniqueReviewers.add(file.currentReviewer); } } - let singleUser: string = uniqueReviewers.size === 1 ? uniqueReviewers.values().next().value : this.userService.currentUser.id; + let singleUser: string = uniqueReviewers.size === 1 ? uniqueReviewers.values().next().value : this.userService.currentUser.id; singleUser = this.singleUsersSelectOptions.indexOf(singleUser) >= 0 ? singleUser : this.singleUsersSelectOptions[0]; + if (this.data.withCurrentUserAsDefault && this.singleUsersSelectOptions.includes(this.userService.currentUser.id)) { + singleUser = this.userService.currentUser.id; + } + this.usersForm = this._formBuilder.group({ // Allow a null reviewer if a previous reviewer exists (= it's not the first assignment) & current user is allowed to unassign singleUser: [singleUser, this._canUnassignFiles && !singleUser ? Validators.required : null], diff --git a/apps/red-ui/src/app/modules/dossier/shared/components/file-actions/file-actions.component.ts b/apps/red-ui/src/app/modules/dossier/shared/components/file-actions/file-actions.component.ts index 7bdcd13cf..1375aa11f 100644 --- a/apps/red-ui/src/app/modules/dossier/shared/components/file-actions/file-actions.component.ts +++ b/apps/red-ui/src/app/modules/dossier/shared/components/file-actions/file-actions.component.ts @@ -151,7 +151,7 @@ export class FileActionsComponent extends AutoUnsubscribe implements OnInit, OnD assign($event: MouseEvent) { const mode = this.file.isUnderApproval ? 'approver' : 'reviewer'; const files = [this.file]; - this._dialogService.openDialog('assignFile', $event, { mode, files }, () => { + this._dialogService.openDialog('assignFile', $event, { mode, files, withCurrentUserAsDefault: true }, () => { this.actionPerformed.emit('assign-reviewer'); }); } @@ -173,10 +173,10 @@ export class FileActionsComponent extends AutoUnsubscribe implements OnInit, OnD }); } - setFileUnderApproval($event: MouseEvent) { + async setFileUnderApproval($event: MouseEvent) { $event.stopPropagation(); if (this.dossiersService.activeDossier.approverIds.length > 1) { - this._fileActionService.assignFile('approver', $event, this.file, () => this.reloadDossiers('assign-reviewer'), true); + await this._fileActionService.assignFile('approver', $event, this.file, () => this.reloadDossiers('assign-reviewer'), true); } else { this.addSubscription = this._fileActionService.setFilesUnderApproval([this.file]).subscribe(() => { this.reloadDossiers('set-under-approval'); @@ -210,8 +210,8 @@ export class FileActionsComponent extends AutoUnsubscribe implements OnInit, OnD }); } - setFileUnderReview($event: MouseEvent, ignoreDialogChanges = false) { - this._fileActionService.assignFile( + async setFileUnderReview($event: MouseEvent, ignoreDialogChanges = false) { + await this._fileActionService.assignFile( 'reviewer', $event, this.file, diff --git a/apps/red-ui/src/app/modules/dossier/shared/services/file-action.service.ts b/apps/red-ui/src/app/modules/dossier/shared/services/file-action.service.ts index dc40eaac5..882121e1c 100644 --- a/apps/red-ui/src/app/modules/dossier/shared/services/file-action.service.ts +++ b/apps/red-ui/src/app/modules/dossier/shared/services/file-action.service.ts @@ -86,38 +86,58 @@ export class FileActionService { ); } - assignFile( + async assignFile( mode: 'reviewer' | 'approver', $event: MouseEvent, file = this._appStateService.activeFile, callback?: Function, ignoreChanged = false, ) { - const userIds = this._getUserIds(mode); - if (userIds.length === 1 || userIds.includes(this._userService.currentUser.id)) { - $event?.stopPropagation(); // event$ is null when called from workflow view - const userId = userIds.length === 1 ? userIds[0] : this._userService.currentUser.id; - this._assignFile(userId, mode, [file]).then(async () => { - if (callback) { - await callback(); - } - }); + $event?.stopPropagation(); + + const currentUserId = this._userService.currentUser.id; + const eligibleUsersIds = this._getUserIds(mode); + + if (file.isUnassigned) { + await this._assignFile(currentUserId, mode, file, callback); + } else if (file.currentReviewer === currentUserId) { + if (eligibleUsersIds.includes(currentUserId)) { + await this._assignFile(currentUserId, mode, file, callback); + } else if (eligibleUsersIds.length === 1) { + await this._assignFile(eligibleUsersIds[0], mode, file, callback); + } else { + const files = [file]; + this._dialogService.openDialog('assignFile', null, { mode, files, ignoreChanged }, async () => { + if (callback) { + await callback(); + } + }); + } } else { - const data = { mode, files: [file], ignoreChanged }; - this._dialogService.openDialog('assignFile', $event, data, async () => { - if (callback) { - await callback(); - } - }); + if (eligibleUsersIds.length === 1) { + await this._assignFile(eligibleUsersIds[0], mode, file, callback); + } else { + const files = [file]; + this._dialogService.openDialog( + 'assignFile', + null, + { mode, files, ignoreChanged, withCurrentUserAsDefault: true }, + async () => { + if (callback) { + await callback(); + } + }, + ); + } } } - private async _assignFile(userId: string, mode: 'reviewer' | 'approver', files: File[]) { + private async _assignFile(userId: string, mode: 'reviewer' | 'approver', file: File, callback?: Function) { try { if (mode === 'reviewer') { await this._filesService .setReviewerFor( - files.map(f => f.fileId), + [file].map(f => f.fileId), this._dossiersService.activeDossierId, userId, ) @@ -125,12 +145,16 @@ export class FileActionService { } else { await this._filesService .setUnderApprovalFor( - files.map(f => f.fileId), + [file].map(f => f.fileId), this._dossiersService.activeDossierId, userId, ) .toPromise(); } + + if (callback) { + await callback(); + } } catch (error) { this._toaster.error(_('error.http.generic'), { params: error }); }