otp-yousign.component.ts 2.84 KB
Newer Older
1
import { Component, Input, OnInit, ViewChild } from '@angular/core';
2
3
4
5
import { HttpClient } from '@angular/common/http';
import { NotificationService } from '../../../../service/notification.service';
import { TranslateService } from '@ngx-translate/core';
import { NgForm } from '@angular/forms';
6
7
import { catchError, tap } from 'rxjs/operators';
import { of } from 'rxjs';
8
import { OtpService } from '../otp.service';
9
10
11
12
13
14
15
16

@Component({
    selector: 'app-otp-yousign',
    templateUrl: 'otp-yousign.component.html',
    styleUrls: ['otp-yousign.component.scss'],
})
export class OtpYousignComponent implements OnInit {
    @ViewChild('otpForm', { static: false }) otpForm: NgForm;
17

18
    @Input() connectorId: string;
19
    @Input() otpYousign: any;
20

21
    securityModes: any[] = [];
22

23
    roles: any[] = ['visa_yousign', 'sign_yousign'];
24
25

    otp: any = {
26
        type: 'yousign',
27
28
29
30
        firstname: '',
        lastname: '',
        email: '',
        phone: '',
31
        security: 'sms',
32
        role: 'sign_yousign',
33
34
        sourceId: '',
        modes: this.roles
35
36
37
38
39
40
    };

    constructor(
        public http: HttpClient,
        private translate: TranslateService,
        public notificationService: NotificationService,
41
42
43
44
45
46
47
48
49
        public otpService: OtpService
    ) {
        this.otpService.catchEvent().subscribe(async (res) => {
            if (res.id === 'connector') {
                this.otp.sourceId = res.connectorId;
                await this.getConfig();
            }
        });
    }
50

51
    async ngOnInit(): Promise<void> {
52
        this.otp.sourceId = this.otpYousign ? this.otpYousign.sourceId : this.connectorId;
53
        await this.getConfig();
54
55
56
    }

    getConfig() {
57
        return new Promise((resolve) => {
58
            this.http.get('../rest/connectors/' + this.connectorId).pipe(
59
60
61
                tap((data: any) => {
                    this.securityModes = [... new Set(data.otp.securityModes)];
                    this.otp.security = this.securityModes[0];
62
                    if (this.otpYousign) {
63
64
65
66
                        this.otp = {...this.otp, ...this.otpYousign};
                        this.otp.modes = this.roles;
                        this.otp.sourceId = this.connectorId;
                        this.formatPhone();
67
                    }
68
69
70
71
72
73
74
75
                    resolve(true);
                }),
                catchError(err => {
                    this.notificationService.handleErrors(err);
                    return of(false);
                })
            ).subscribe();
        });
76
77
78
79
80
81
    }

    getData() {
        return this.otp;
    }

82
83
84
85
    getSecurityMode() {
        return this.translate.instant('lang.' + this.otp.security);
    }

86
87
88
    isValid() {
        return this.otpForm.valid;
    }
89

90
    formatPhone() {
91
92
93
94
        if (this.otp.phone.length > 1 && this.otp.phone[0] === '0') {
            this.otp.phone = this.otp.phone.replace('0', '+33');
        }
    }
95
}