Skip to content

Commit

Permalink
Merge pull request #607 from LINCnil/feature-and-fix-for-user-destroy
Browse files Browse the repository at this point in the history
Feature and fix for user destroy
  • Loading branch information
kevin-atnos authored Nov 8, 2021
2 parents a94a644 + 3c01839 commit ce3e9ef
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 11 deletions.
52 changes: 51 additions & 1 deletion src/app/modules/entries/cards/pia-card/pia-card.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@

<div *ngIf="authService.state; else elseAuthorBlock">
<tag-input
#input
id="pia-edit-{{ pia.id }}-author-name"
[disable]="
authService.currentUserValue &&
Expand All @@ -116,6 +117,19 @@
'homepage.cards.placeholder_author' | translate
"
>
<ng-template let-item="item" let-index="index">
<div
[ngClass]="{
'custom-tag': true,
'user-not-found': !checkIfUserExist('author_name')
}"
>
{{ item.display }}
<delete-icon
(click)="input.removeItem(item, index)"
></delete-icon>
</div>
</ng-template>
<tag-input-dropdown
[keepOpen]="false"
identifyBy="id"
Expand Down Expand Up @@ -146,6 +160,7 @@
}}</label>
<div *ngIf="authService.state; else elseEvaluatorBlock">
<tag-input
#input
id="pia-edit-{{ pia.id }}-evaluator-name"
[disable]="
authService.currentUserValue &&
Expand All @@ -163,6 +178,19 @@
'homepage.cards.placeholder_evaluation' | translate
"
>
<ng-template let-item="item" let-index="index">
<div
[ngClass]="{
'custom-tag': true,
'user-not-found': !checkIfUserExist('evaluator_name')
}"
>
{{ item.display }}
<delete-icon
(click)="input.removeItem(item, index)"
></delete-icon>
</div>
</ng-template>
<tag-input-dropdown
[keepOpen]="false"
identifyBy="id"
Expand Down Expand Up @@ -193,6 +221,7 @@
}}</label>
<div *ngIf="authService.state; else elseValidatorBlock">
<tag-input
#input
id="pia-edit-{{ pia.id }}-validator-name"
[disable]="
authService.currentUserValue &&
Expand All @@ -210,6 +239,19 @@
'homepage.cards.placeholder_validation' | translate
"
>
<ng-template let-item="item" let-index="index">
<div
[ngClass]="{
'custom-tag': true,
'user-not-found': !checkIfUserExist('validator_name')
}"
>
{{ item.display }}
<delete-icon
(click)="input.removeItem(item, index)"
></delete-icon>
</div>
</ng-template>
<tag-input-dropdown
[keepOpen]="false"
identifyBy="id"
Expand Down Expand Up @@ -240,6 +282,7 @@
"homepage.cards.guest" | translate
}}</label>
<tag-input
#input
class="pia-users-tags"
id="pia-edit-{{ pia.id }}-guest-name"
[disable]="
Expand All @@ -258,6 +301,14 @@
'homepage.cards.placeholder_validation' | translate
"
>
<ng-template let-item="item" let-index="index">
<div [ngClass]="{ 'custom-tag': true }">
{{ item.display }}
<delete-icon
(click)="input.removeItem(item, index)"
></delete-icon>
</div>
</ng-template>
<tag-input-dropdown
[keepOpen]="false"
identifyBy="id"
Expand All @@ -275,7 +326,6 @@
type="text"
#piaCategory
formControlName="category"
required
placeholder="{{ 'homepage.cards.placeholder_category' | translate }}"
id="pia-edit-{{ pia.id }}-category"
(focusout)="piaCategoryFocusOut()"
Expand Down
27 changes: 22 additions & 5 deletions src/app/modules/entries/cards/pia-card/pia-card.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { TagModel, TagModelClass } from 'ngx-chips/core/accessor';
import { User } from 'src/app/models/user.model';
import { SimpleChanges } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { TagInputComponent } from 'ngx-chips';
declare const require: any;

@Component({
Expand Down Expand Up @@ -51,8 +52,9 @@ export class PiaCardComponent implements OnInit, OnChanges {
piaEvaluatorName: ElementRef;
@ViewChild('piaValidatorName')
piaValidatorName: ElementRef;
@ViewChild('piaGuestName')
piaGuestName: ElementRef;

@ViewChild('authorTagInput')
authorTagInput: TagInputComponent;

constructor(
public piaService: PiaService,
Expand Down Expand Up @@ -88,14 +90,19 @@ export class PiaCardComponent implements OnInit, OnChanges {
complete: () => {
if (this.authService.state) {
this.piaForm.controls.author_name.setValue(
this.pia.author_name ? [this.pia.author_name] : []
this.pia.author_name ? [{ display: this.pia.author_name }] : []
);
this.piaForm.controls.evaluator_name.setValue(
this.pia.evaluator_name ? [this.pia.evaluator_name] : []
this.pia.evaluator_name
? [{ display: this.pia.evaluator_name }]
: []
);
this.piaForm.controls.validator_name.setValue(
this.pia.validator_name ? [this.pia.validator_name] : []
this.pia.validator_name
? [{ display: this.pia.validator_name }]
: []
);

this.piaForm.controls.guests.setValue(
this.pia.guests.map((guest: User) => {
return {
Expand Down Expand Up @@ -400,4 +407,14 @@ export class PiaCardComponent implements OnInit, OnChanges {
this.pia['guests'] = this.piaForm.controls[field].value.map(x => x.id);
this.piaService.update(this.pia);
}

checkIfUserExist(field): boolean {
return (
this.users.findIndex(
u =>
u.firstname + ' ' + u.lastname ===
this.piaForm.controls[field].value[0].display
) !== -1
);
}
}
43 changes: 43 additions & 0 deletions src/app/modules/entries/list/pia-line/pia-line.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<td class="pia-listsBlock-item">
<div *ngIf="authService.state; else author_name">
<tag-input
#input
[disable]="
authService.currentUserValue &&
!authService.currentUserValue.access_type.includes('functional')
Expand All @@ -103,6 +104,17 @@
'homepage.cards.placeholder_validation' | translate
"
>
<ng-template let-item="item" let-index="index">
<div
[ngClass]="{
'custom-tag': true,
'user-not-found': !checkIfUserExist('author_name')
}"
>
{{ item.display }}
<delete-icon (click)="input.removeItem(item, index)"></delete-icon>
</div>
</ng-template>
<tag-input-dropdown
[keepOpen]="false"
identifyBy="id"
Expand All @@ -120,6 +132,7 @@
<td class="pia-listsBlock-item">
<div *ngIf="authService.state; else evaluator_name">
<tag-input
#input
[disable]="
authService.currentUserValue &&
!authService.currentUserValue.access_type.includes('functional')
Expand All @@ -136,6 +149,17 @@
'homepage.cards.placeholder_validation' | translate
"
>
<ng-template let-item="item" let-index="index">
<div
[ngClass]="{
'custom-tag': true,
'user-not-found': !checkIfUserExist('evaluator_name')
}"
>
{{ item.display }}
<delete-icon (click)="input.removeItem(item, index)"></delete-icon>
</div>
</ng-template>
<tag-input-dropdown
[keepOpen]="false"
identifyBy="id"
Expand All @@ -153,6 +177,7 @@
<td class="pia-listsBlock-item">
<div *ngIf="authService.state; else validator_name">
<tag-input
#input
[disable]="
authService.currentUserValue &&
!authService.currentUserValue.access_type.includes('functional')
Expand All @@ -169,6 +194,17 @@
'homepage.cards.placeholder_validation' | translate
"
>
<ng-template let-item="item" let-index="index">
<div
[ngClass]="{
'custom-tag': true,
'user-not-found': !checkIfUserExist('validator_name')
}"
>
{{ item.display }}
<delete-icon (click)="input.removeItem(item, index)"></delete-icon>
</div>
</ng-template>
<tag-input-dropdown
[keepOpen]="false"
identifyBy="id"
Expand All @@ -185,6 +221,7 @@
</td>
<td *ngIf="authService.state">
<tag-input
#input
[disable]="
authService.currentUserValue &&
!authService.currentUserValue.access_type.includes('functional')
Expand All @@ -201,6 +238,12 @@
'homepage.cards.placeholder_validation' | translate
"
>
<ng-template let-item="item" let-index="index">
<div [ngClass]="{ 'custom-tag': true }">
{{ item.display }}
<delete-icon (click)="input.removeItem(item, index)"></delete-icon>
</div>
</ng-template>
<tag-input-dropdown
[keepOpen]="false"
identifyBy="id"
Expand Down
19 changes: 19 additions & 0 deletions src/app/modules/entries/list/pia-line/pia-line.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,23 @@ export class PiaLineComponent implements OnInit, OnChanges {
this.pia[field] = guests.map(x => (typeof x === 'object' ? x.id : x));
this.piaService.update(this.pia);
}

private checkUserInField(field) {
return (
this.users.findIndex(u => u.firstname + ' ' + u.lastname === field) !== -1
);
}

checkIfUserExist(field): boolean {
switch (field) {
case 'author_name':
return this.checkUserInField(this.authorField[0].display);
case 'evaluator_name':
return this.checkUserInField(this.evaluatorField[0].display);
case 'validator_name':
return this.checkUserInField(this.validatorField[0].display);
default:
return false;
}
}
}
10 changes: 5 additions & 5 deletions src/app/services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ export class UsersService extends ApplicationDb {
// Get data
const usersList: User[] = await this.getUsers();
const userPias: Pia[] = await this.getUserPias(userId);

const user: User = this.authService.currentUserValue;
const authUser: User = this.authService.currentUserValue;

// Check if the current user doesn't try to delete himself from the database
if (userId !== user.id) {
if (userId !== authUser.id) {
if (usersList) {
// Check if user don't delete all technical Administrator
if (
usersList.filter(u => u.access_type.includes('technical')).length >
1
usersList.filter(
u => u.access_type.includes('technical') && u.id !== userId
).length >= 1
) {
// Check pias presence
if (userPias && userPias.length <= 0) {
Expand Down
10 changes: 10 additions & 0 deletions src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,13 @@ tag-input-form {
max-width: 650px !important;
}
}

.ng2-tag-input.foundation-theme tag {
padding: 0px;
.custom-tag {
padding: 0.08rem 0.45rem;
&.user-not-found {
background-color: #a7a7a7;
}
}
}

0 comments on commit ce3e9ef

Please sign in to comment.