send-external-signatory-book-action.component.ts 8.72 KB
Newer Older
1
import { Component, OnInit, Inject, ViewChild, ChangeDetectorRef } from '@angular/core';
2
import { TranslateService } from '@ngx-translate/core';
3
import { NotificationService } from '@service/notification/notification.service';
4
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
5
6
import { HttpClient } from '@angular/common/http';
import { NoteEditorComponent } from '../../notes/note-editor.component';
7
8
import { XParaphComponent } from './x-paraph/x-paraph.component';
import { MaarchParaphComponent } from './maarch-paraph/maarch-paraph.component';
9
import { FastParaphComponent } from './fast-paraph/fast-paraph.component';
10
import { IParaphComponent } from './i-paraph/i-paraph.component';
11
import { IxbusParaphComponent } from './ixbus-paraph/ixbus-paraph.component';
12
13
import { tap, finalize, catchError } from 'rxjs/operators';
import { of } from 'rxjs';
14
15

@Component({
16
    templateUrl: 'send-external-signatory-book-action.component.html',
17
18
    styleUrls: ['send-external-signatory-book-action.component.scss'],
})
19
export class SendExternalSignatoryBookActionComponent implements OnInit {
20

21
22
23
24
25
26
27
    @ViewChild('noteEditor', { static: true }) noteEditor: NoteEditorComponent;

    @ViewChild('xParaph', { static: false }) xParaph: XParaphComponent;
    @ViewChild('maarchParapheur', { static: false }) maarchParapheur: MaarchParaphComponent;
    @ViewChild('fastParapheur', { static: false }) fastParapheur: FastParaphComponent;
    @ViewChild('iParapheur', { static: false }) iParapheur: IParaphComponent;
    @ViewChild('ixbus', { static: false }) ixbus: IxbusParaphComponent;
28

29
    loading: boolean = false;
30

31
    additionalsInfos: any = {
32
        destinationId: '',
33
        users: [],
34
        attachments: [],
35
        noAttachment: []
36
    };
37
    resourcesToSign: any[] = [];
38
    resourcesMailing: any[] = [];
39
    signatoryBookEnabled: string = '';
40

41
    externalSignatoryBookDatas: any = {
42
        steps: [],
43
        objectSent: 'attachment'
44
    };
45
46
47
48
49
50
51

    integrationsInfo: any = {
        inSignatureBook: {
            icon: 'fas fa-file-signature'
        }
    };

52
    errors: any;
53

54
55
    mainDocumentSigned: boolean = false;

56
    constructor(
57
        public translate: TranslateService,
58
59
60
61
62
        public http: HttpClient,
        private notify: NotificationService,
        public dialogRef: MatDialogRef<SendExternalSignatoryBookActionComponent>,
        @Inject(MAT_DIALOG_DATA) public data: any,
        private changeDetectorRef: ChangeDetectorRef) { }
63
64

    ngOnInit(): void {
65
        this.loading = true;
66
        if (this.data.resource.integrations['inSignatureBook']) {
67
68
69
70
71
72
73
74
75
76
77
78
            this.http.get(`../rest/resources/${this.data.resource.resId}/versionsInformations`).pipe(
                tap((data: any) => {
                    this.mainDocumentSigned = data.SIGN.length !== 0;
                    if (!this.mainDocumentSigned) {
                        this.toggleDocToSign(true, this.data.resource, true);
                    }
                }),
                catchError((err: any) => {
                    this.notify.handleSoftErrors(err);
                    return of(false);
                })
            ).subscribe();
79
        }
80
        this.checkExternalSignatureBook();
81
82
    }

83
84
85
86
87
88
89
90
    async onSubmit() {
        if (this.hasEmptyOtpSignaturePosition()) {
            this.notify.error(this.translate.instant('lang.mustSign'));
        } else {
            this.loading = true;
            if (this.data.resIds.length > 0) {
                this.executeAction();
            }
91
92
        }
    }
93

94
95
    checkExternalSignatureBook() {
        this.loading = true;
96

97
        return new Promise((resolve) => {
98
            this.http.post(`../rest/resourcesList/users/${this.data.userId}/groups/${this.data.groupId}/baskets/${this.data.basketId}/checkExternalSignatoryBook`, { resources: this.data.resIds }).pipe(
99
100
101
102
                tap((data: any) => {
                    this.additionalsInfos = data.additionalsInfos;
                    if (this.additionalsInfos.attachments.length > 0) {
                        this.signatoryBookEnabled = data.signatureBookEnabled;
103
                        this.resourcesMailing = data.additionalsInfos.attachments.filter((element: any) => element.mailing);
104
                        data.availableResources.filter((element: any) => !element.mainDocument).forEach((element: any) => {
105
106
                            this.toggleDocToSign(true, element, false);
                        });
107
                    }
108
109
110
111
112
113
                    this.errors = data.errors;
                    resolve(true);
                }),
                finalize(() => this.loading = false),
                catchError((err: any) => {
                    this.notify.handleSoftErrors(err);
114
                    this.dialogRef.close();
115
116
117
118
119
120
                    return of(false);
                })
            ).subscribe();
        });
    }

121
    executeAction() {
122
123
        const realResSelected: string[] = this[this.signatoryBookEnabled].getRessources();
        const datas: any = this[this.signatoryBookEnabled].getDatas();
124
125

        this.http.put(this.data.processActionRoute, { resources: realResSelected, note: this.noteEditor.getNote(), data: datas }).pipe(
126
            tap((data: any) => {
127
                if (!data) {
128
                    this.dialogRef.close(realResSelected);
129
130
131
132
                }
                if (data && data.errors != null) {
                    this.notify.error(data.errors);
                }
133
134
135
            }),
            finalize(() => this.loading = false),
            catchError((err: any) => {
136
                this.notify.handleSoftErrors(err);
137
138
139
                return of(false);
            })
        ).subscribe();
140
    }
141

142
    isValidAction() {
143
        if (this[this.signatoryBookEnabled] !== undefined) {
144
            return this[this.signatoryBookEnabled].isValidParaph();
145
        } else {
146
            return false;
147
148
        }
    }
149
150

    toggleIntegration(integrationId: string) {
151
        this.resourcesToSign = [];
152
        this.http.put('../rest/resourcesList/integrations', { resources: this.data.resIds, integrations: { [integrationId]: !this.data.resource.integrations[integrationId] } }).pipe(
153
154
            tap(async () => {
                this.data.resource.integrations[integrationId] = !this.data.resource.integrations[integrationId];
155
156
157
158

                if (!this.mainDocumentSigned) {
                    this.toggleDocToSign(this.data.resource.integrations[integrationId], this.data.resource, true);
                }
159
160
161
162
163
164
165
166
167
168
                await this.checkExternalSignatureBook();
                this.changeDetectorRef.detectChanges();
            }),
            catchError((err: any) => {
                this.notify.handleSoftErrors(err);
                return of(false);
            })
        ).subscribe();
    }

169
170
171
172
173
174
175
176
177
178
179
180
181
182
    toggleDocToSign(state: boolean, document: any, mainDocument: boolean = true) {
        if (state) {
            this.resourcesToSign.push(
                {
                    resId: document.resId,
                    chrono: document.chrono,
                    title: document.subject,
                    mainDocument: mainDocument,
                });
        } else {
            const index = this.resourcesToSign.map((item: any) => `${item.resId}_${item.mainDocument}`).indexOf(`${document.resId}_${mainDocument}`);
            this.resourcesToSign.splice(index, 1);
        }
    }
183
184

    hasEmptyOtpSignaturePosition() {
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        if (this.signatoryBookEnabled == 'maarchParapheur') {
            const externalUsers: any[] = this.maarchParapheur.appExternalVisaWorkflow.visaWorkflow.items.filter((user: any) => user.item_id === null && user.role === 'sign');
            if (externalUsers.length > 0) {
                let resToSign: any[] = this.maarchParapheur.resourcesToSign.filter((res: any) => res.hasOwnProperty('signaturePositions'));
                let mustSign: boolean = false;
                if (this.maarchParapheur.resourcesToSign.length > 1 && this.maarchParapheur.resourcesToSign.length - resToSign.length  >= 1) {
                    return true;
                } else if (resToSign.length === 0) {
                    return true;
                } else {
                    resToSign = resToSign.map((res: any) => res.signaturePositions);
                    externalUsers.forEach((element: any) => {
                        for (let i = 0; i < resToSign.length; i++) {
                            const userIndex: number = this.maarchParapheur.appExternalVisaWorkflow.visaWorkflow.items.indexOf(element);
                            if (resToSign[i].filter((item: any) => item.sequence === userIndex).length ===  0) {
                                mustSign = true;
                                break;
                            }
203
                        }
204
205
206
                    });
                    return mustSign;
                }
207
            }
208
209
        } else {
            return false;
210
211
        }
    }
212
}