continue-avis-circuit-action.component.ts 4.35 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
11
12
import { AvisWorkflowComponent } from '../../avis/avis-workflow.component';

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

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

21
22
23
24
25
    loading: boolean = false;

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

26
27
28
    ownerOpinion: string = '';
    opinionContent: string = '';

29
30
    opinionLimitDate: string | Date = null;

31
32
33
    noResourceToProcess: boolean = null;

    constructor(
34
        public translate: TranslateService,
35
36
37
        public http: HttpClient,
        private notify: NotificationService,
        public dialogRef: MatDialogRef<ContinueAvisCircuitActionComponent>,
38
39
40
        @Inject(MAT_DIALOG_DATA) public data: any,
        public functions: FunctionsService) { }

41
42
    ngOnInit() {
        this.checkAvisCircuit();
43
44
45
    }

    checkAvisCircuit() {
46
        this.loading = true;
47
48
        this.resourcesErrors = [];
        this.resourcesWarnings = [];
49
        this.http.post('../rest/resourcesList/users/' + this.data.userId + '/groups/' + this.data.groupId + '/baskets/' + this.data.basketId + '/actions/' + this.data.action.id + '/checkContinueOpinionCircuit', { resources: this.data.resIds }).pipe(
50
            tap((data: any) => {
51
52
53
54
                if (!this.functions.empty(data.resourcesInformations.warning)) {
                    this.resourcesWarnings = data.resourcesInformations.warning;
                }

55
                if (!this.functions.empty(data.resourcesInformations.error)) {
56
57
58
                    this.resourcesErrors = data.resourcesInformations.error;
                    this.noResourceToProcess = this.resourcesErrors.length === this.data.resIds.length;
                }
59
60
61
62

                if (!this.noResourceToProcess) {
                    this.ownerOpinion = data.resourcesInformations.success[0].avisUserAsk;
                    this.opinionContent = data.resourcesInformations.success[0].note;
63
64
                    this.opinionLimitDate = new Date(data.resourcesInformations.success[0].opinionLimitDate);
                    this.opinionLimitDate = this.functions.formatDateObjectToDateString(this.opinionLimitDate);
65
66
67
68
                }
            }),
            finalize(() => this.loading = false),
            catchError((err: any) => {
69
                this.notify.handleSoftErrors(err);
70
71
72
73
                this.dialogRef.close();
                return of(false);
            })
        ).subscribe();
74
75
    }

76
    onSubmit() {
77
78
79
80
81
        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[]) {
Hamza HRAMCHI's avatar
Hamza HRAMCHI committed
82
        const noteContent: string = `[${this.translate.instant('lang.opinionUserState')}] ${this.noteEditor.getNoteContent()}`;
83
84
        this.noteEditor.setNoteContent(noteContent);
        this.http.put(this.data.processActionRoute, {resources : realResSelected, note: this.noteEditor.getNote()}).pipe(
85
86
            tap((data: any) => {
                if (!data) {
87
                    this.dialogRef.close(realResSelected);
88
89
90
91
92
93
94
                }
                if (data && data.errors != null) {
                    this.notify.error(data.errors);
                }
            }),
            finalize(() => this.loading = false),
            catchError((err: any) => {
95
                this.notify.handleSoftErrors(err);
96
97
98
99
100
101
                return of(false);
            })
        ).subscribe();
    }

    isValidAction() {
102
        return !this.noResourceToProcess && !this.functions.empty(this.noteEditor.getNoteContent());
103
104
    }
}