All files / src/app/features/user/email-validation email-validation.component.ts

77.77% Statements 28/36
53.84% Branches 7/13
70% Functions 7/10
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551x 1x 1x   1x 1x 1x 1x               2x     5x 5x 5x 5x 5x 5x     3x 2x     1x         4x 4x 1x     3x       2x 2x     1x 1x          
import { Component, OnInit } from '@angular/core';
import { UserService } from '@core/services/user.service';
import { ActivatedRoute, Router } from '@angular/router';
import { ValidateEmailRequest } from '@core/model/validate-email-request.interface';
import { catchError, take, throwError } from 'rxjs';
import { NotificationService } from '@core/services/notification.service';
import { SessionService } from '@core/services/session.service';
import { ErrorProcessorService } from '@core/services/error-processor.service';
 
@Component({
  selector: 'app-email-validation',
  imports: [],
  templateUrl: './email-validation.component.html',
  styleUrl: './email-validation.component.scss'
})
export class EmailValidationComponent implements OnInit {
 
  constructor(
    private userService: UserService, 
    private activatedRoute: ActivatedRoute, 
    private router: Router, 
    private notificationService: NotificationService, 
    private sessionService: SessionService,
    private errorProcessorService: ErrorProcessorService) {}
 
  private redirect(): void {
    if(this.sessionService.isLogged()) {
      this.router.navigate(["/jobs"])
    }
    else {
      this.router.navigate(["/login"])
    }
  }
 
  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe((params) => {
      if(params['code'] == undefined || params['code'] == null || params['code'] == '') {
        this.router.navigate([""]);
      }
      else {
        this.userService.validateEmail({code: params['code']} as ValidateEmailRequest).pipe(
          take(1),
          catchError(
            () => {
              this.redirect();
              return this.errorProcessorService.processError(new Error($localize `:email validation fail@@error.email.validation_failed:Email validation failed`));
            }
        )).subscribe(() => {
          this.notificationService.confirmation($localize `:@@info.email.validated:Your email has been validated.`);
          this.redirect();
        })
      }
    });
  }
}