give-avis-parallel-action.component.ts 5.08 KB
Newer Older
1
import { Component, OnInit, Inject, ViewChild } from '@angular/core';
2
import { TranslateService } from '@ngx-translate/core';
3
import { NotificationService } from '@service/notification/notification.service';
4
5
6
7
8
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { HttpClient } from '@angular/common/http';
import { NoteEditorComponent } from '../../notes/note-editor.component';
import { tap, finalize, catchError } from 'rxjs/operators';
import { of } from 'rxjs';
9
import { FunctionsService } from '@service/functions.service';
10
import { HeaderService } from '@service/header.service';
11
12

@Component({
13
    templateUrl: 'give-avis-parallel-action.component.html',
14
15
16
17
    styleUrls: ['give-avis-parallel-action.component.scss'],
})
export class GiveAvisParallelActionComponent implements OnInit {

18
    @ViewChild('noteEditor', { static: true }) noteEditor: NoteEditorComponent;
19

20
21
22
23
24
25
26
    loading: boolean = false;

    resourcesWarnings: any[] = [];
    resourcesErrors: any[] = [];

    noResourceToProcess: boolean = null;

27
28
    opinionLimitDate: string | Date = null;

29
30
31
    ownerOpinion: string = '';
    opinionContent: string = '';

32
33
34
35
36
    delegation: any = {
        isDelegated: false,
        userDelegated: null
    };

37
    constructor(
38
        public translate: TranslateService,
39
40
41
42
        public http: HttpClient,
        private notify: NotificationService,
        public dialogRef: MatDialogRef<GiveAvisParallelActionComponent>,
        @Inject(MAT_DIALOG_DATA) public data: any,
43
44
45
        public functions: FunctionsService,
        public headerService: HeaderService
    ) { }
46

47
    ngOnInit() {
48
49
50
51
52
53
54
55
        this.checkAvisParallel();
    }

    checkAvisParallel() {
        this.loading = true;
        this.resourcesErrors = [];
        this.resourcesWarnings = [];

56
        this.http.post('../rest/resourcesList/users/' + this.data.userId + '/groups/' + this.data.groupId + '/baskets/' + this.data.basketId + '/actions/' + this.data.action.id + '/checkGiveParallelOpinion', { resources: this.data.resIds }).pipe(
57
58
59
60
61
62
63
64
65
66
67
68
69
            tap((data: any) => {
                if (!this.functions.empty(data.resourcesInformations.warning)) {
                    this.resourcesWarnings = data.resourcesInformations.warning;
                }

                if (!this.functions.empty(data.resourcesInformations.error)) {
                    this.resourcesErrors = data.resourcesInformations.error;
                    this.noResourceToProcess = this.resourcesErrors.length === this.data.resIds.length;
                }

                if (!this.noResourceToProcess) {
                    this.ownerOpinion = data.resourcesInformations.success[0].avisUserAsk;
                    this.opinionContent = data.resourcesInformations.success[0].note;
70
71
                    this.opinionLimitDate = new Date(data.resourcesInformations.success[0].opinionLimitDate);
                    this.opinionLimitDate = this.functions.formatDateObjectToDateString(this.opinionLimitDate);
72
                }
73
74
75
76
77
                const userId: number = parseInt(this.data.userId, 10);
                this.delegation.isDelegated = userId !== this.headerService.user.id ? true : false;
                if (this.delegation.isDelegated && !this.noResourceToProcess) {
                    this.delegation.userDelegated = data.resourcesInformations.success[0].delegatingUser;
                }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
            }),
            finalize(() => this.loading = false),
            catchError((err: any) => {
                this.notify.handleSoftErrors(err);
                this.dialogRef.close();
                return of(false);
            })
        ).subscribe();
    }

    onSubmit() {
        const realResSelected: number[] = this.data.resIds.filter((resId: any) => this.resourcesErrors.map(resErr => resErr.res_id).indexOf(resId) === -1);
        this.executeAction(realResSelected);
    }

    executeAction(realResSelected: number[]) {
94
        const delegateMsg: string = this.translate.instant('lang.insteadOf').replace(/^.{1}/g, this.translate.instant('lang.insteadOf')[0].toLowerCase());
Hamza HRAMCHI's avatar
Hamza HRAMCHI committed
95
96
        const opinionUserState: string = this.delegation.isDelegated ? `${this.translate.instant('lang.opinionUserState').toUpperCase()} ${delegateMsg} ${this.delegation.userDelegated}` : this.translate.instant('lang.opinionUserState');
        const noteContent: string = `[${opinionUserState}] ${this.noteEditor.getNoteContent()}`;
97
98
        this.noteEditor.setNoteContent(noteContent);
        this.http.put(this.data.processActionRoute, { resources: realResSelected, note: this.noteEditor.getNote()}).pipe(
99
100
            tap((data: any) => {
                if (!data) {
101
                    this.dialogRef.close(realResSelected);
102
103
104
105
106
107
108
                }
                if (data && data.errors != null) {
                    this.notify.error(data.errors);
                }
            }),
            finalize(() => this.loading = false),
            catchError((err: any) => {
109
                this.notify.handleSoftErrors(err);
110
111
112
113
114
115
                return of(false);
            })
        ).subscribe();
    }

    isValidAction() {
116
        return !this.noResourceToProcess && !this.functions.empty(this.noteEditor.getNoteContent());
117
118
    }
}