import { Component, OnInit, ViewChild, ChangeDetectorRef, OnDestroy } from '@angular/core'; import { CommonService } from 'src/app/hmg-common/services/common/common.service'; import { AuthenticationService } from 'src/app/hmg-common/services/authentication/authentication.service'; import { Router } from '@angular/router'; import { AlertController } from '@ionic/angular'; import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service'; import { CheckUserAuthenticationRequest } from 'src/app/hmg-common/services/authentication/models/check-user-auth.request'; import { CheckUserAuthenticationResponse } from 'src/app/hmg-common/services/authentication/models/check-user-auth.response'; import { CheckActivationCodeRequest } from 'src/app/hmg-common/services/authentication/models/check-activation-code.request'; import { SmsReaderService } from 'src/app/hmg-common/services/sms/sms-reader.service'; import { ForgotFileIDResponse } from '../../hmg-common/services/authentication/models/forgot-File-ID.response'; import { InternationalMobileComponent } from 'src/app/hmg-common/ui/mobile-number/international-mobile/international-mobile.component'; import { CountryCode } from 'src/app/hmg-common/ui/mobile-number/international-mobile/models/country-code.model'; @Component({ selector: 'app-forgot', templateUrl: './forgot.component.html', styleUrls: ['./forgot.component.scss'] }) export class ForgotComponent implements OnInit,OnDestroy { public countryCode: CountryCode; @ViewChild(InternationalMobileComponent) internationlMobile: InternationalMobileComponent; constructor( public cs: CommonService, public authService: AuthenticationService, public router: Router, public alertController: AlertController, public ts: TranslatorService, public smsService: SmsReaderService, public changeDetector: ChangeDetectorRef ) { } ngOnInit() { } ngOnDestroy(): void { this.smsService.stopSMSMonitoring(); } public onForgot() { this.sendSMSForForgotPassword(); } public countryCodeChanged(countryCode: CountryCode) { this.countryCode = countryCode; } public isValidForm() { return (this.countryCode && this.countryCode.isValid); } private checkUserResult: CheckUserAuthenticationResponse; private sendSMSForForgotPassword() { const request = new CheckUserAuthenticationRequest(); //request.PatientMobileNumber = this.countryCode.number; //request.ZipCode = CountryCode.localCode(this.countryCode.code); this.authService.sendSMSForForgotFileNumber( request, () => { this.sendSMSForForgotPassword(); }, this.ts.trPK('general', 'ok')).subscribe((result: CheckUserAuthenticationResponse) => { if (this.cs.validResponse(result)) { this.checkUserResult = result; if (result.isSMSSent) { this.startReceivingSMS(); this.presentSMSPasswordDialog(); } } }); } private startReceivingSMS() { this.smsService.startSMSMonitoring((code) => { this.cs.dismissSMSDialog().subscribe(cleared => { this.checkActivationCode(code); }); }); } public presentSMSPasswordDialog() { this.cs.presentSMSPasswordDialog( (code: string) => { this.checkActivationCode(code); }); } private checkActivationCode(readedCode?) { const request = new CheckActivationCodeRequest(); request.LogInTokenID = this.checkUserResult.LogInTokenID; //request.PatientOutSA = this.checkUserResult.PatientOutSA ? 1 : 0; request.PatientMobileNumber = this.countryCode.number; request.ZipCode = CountryCode.localCode(this.countryCode.code); request.activationCode = readedCode; this.authService.forgotFileIdActivation(request, () => { this.presentSMSPasswordDialog(); }, this.ts.trPK('general', 'retry')).subscribe((result: ForgotFileIDResponse) => { if (this.cs.validResponse(result)) { this.smsService.stopSMSMonitoring(); this.cs.presentAlert(result.ReturnMessage); } }); } }