Project

General

Profile

1
import {Component, OnInit} from '@angular/core';
2
import {FormBuilder, FormGroup} from '@angular/forms';
3
import {InvoiceDownloadSchedulerService} from '../../../../shared/services/invoice-download-scheduler.service';
4
import {findAll} from '@angular/compiler-cli/ngcc/src/utils';
5
import { IPowerClient } from 'src/app/shared/models/ipower-client.interface';
6
import { Observable } from 'rxjs';
7
import { USER_RIGHTS } from 'src/app/shared/enums/USER_RIGHTS.enum';
8
import { environment } from 'src/environments/environment';
9
import { AuthService } from 'src/app/shared/services/auth.service';
10
import { IpowerClientsService } from 'src/app/shared/services/administration/ipower-clients.service';
11
import { ErrorHandlingService } from 'src/app/shared/services/error-handling/error-handling.service';
12

    
13
@Component({
14
  selector: 'app-file-download-search',
15
  templateUrl: './file-download-search.component.html',
16
  styleUrls: ['./file-download-search.component.scss']
17
})
18
export class FileDownloadSearchComponent implements OnInit {
19
  searchFilter: FormGroup;
20
  suggestionsForWorkspace = [];
21
  fileStatusList: string[];
22
  workSpaces: string[] = [];
23
  iPowerClientCodeSuggestions: string[];
24
  iPowerClientSuggestions: IPowerClient[];
25
  endpointToSubscribeTo : IPowerClient[];
26
  iPowerClientNameSuggestions: any;
27
  private selectedIPowerClient: IPowerClient;
28

    
29
  constructor(private fb: FormBuilder, private downloadService: InvoiceDownloadSchedulerService, private authService: AuthService, private iPowerClientsService: IpowerClientsService,
30
    private errorHandlingService: ErrorHandlingService) {
31
  }
32

    
33
  ngOnInit(): void {
34
    this.downloadService.getStatuses().subscribe(x => {
35
      this.fileStatusList = x;
36
    });
37
    this.downloadService.getWorkspaces().subscribe(x => {
38
      this.workSpaces = x;
39
    });
40
    this.searchFilter = this.fb.group({
41
      iPowerClientName: [{value: null, disabled: false}],
42
      iPowerClientCode: [{value: null, disabled: false}],
43
      status: [{value: null, disabled: false}]
44
    });
45
  }
46

    
47
  // searchWorkspace($event: any): void {
48
  //   this.suggestionsForWorkspace = this.workSpaces.filter(x => {
49
  //     return x.search(this.searchFilter.get('clientWorkspace').value);
50
  //   });
51
  // }
52

    
53
  autosuggestIPowerClientCode(event) {
54

    
55
    if (event.query.length < 3) {
56
      this.iPowerClientCodeSuggestions = [];
57
      return;
58
    }
59

    
60
    // If the user has the right to Preview of Scheduling Procedure (A02), we use the endpoint that returns all iPowerClients,
61
    // otherwise, the one that checks for the user's User_Access too. Whether the user can see ANY rule has already been handled by the 'search' button.
62
    let endpointToSubscribeTo: Observable<IPowerClient[]> = this.authService.userHasRightForClient(USER_RIGHTS.A02, environment.globalRightsClientID)
63
      ? this.iPowerClientsService.getClientsByCodeOnly(event.query)
64
      : this.authService.userRights.find(rdc => USER_RIGHTS.A03.isGrantedToUser(rdc.rights)) != null ? this.iPowerClientsService.getClientsByCodeDistinct(event.query) : null;
65

    
66
    endpointToSubscribeTo.subscribe(
67
      (values: IPowerClient[]) => {
68
        let temp: string[] = [];
69
        this.iPowerClientSuggestions = values;
70
        values.map(val => temp.push(val.clientCode));
71
        this.iPowerClientCodeSuggestions = temp
72
      },
73
      err => this.errorHandlingService.showHttpResponseError(err)
74
    );
75
  }
76

    
77
  /*
78
  * Auto-suggest & Auto-complete IPower Client
79
  */
80
 autosuggestIPowerClientName(event): void {
81

    
82
  if (!event.query || event.query.length < 3) {
83
    this.iPowerClientNameSuggestions = [];
84
    return;
85
  }
86

    
87
  // If the user has the right to Preview of Scheduling Procedure (A02), we use the endpoint that returns all iPowerClients,
88
    // otherwise, the one that checks for the user's User_Access too. Whether the user can see ANY rule has already been handled by the 'search' button.
89
    let endpointToSubscribeTo: Observable<IPowerClient[]> = this.authService.userHasRightForClient(USER_RIGHTS.A02, environment.globalRightsClientID)
90
      ? this.iPowerClientsService.getClientsByNameOnly(event.query)
91
      : this.authService.userRights.find(rdc => USER_RIGHTS.A03.isGrantedToUser(rdc.rights)) != null ? this.iPowerClientsService.getClientsByNameDistinct(event.query) : null;
92

    
93
      endpointToSubscribeTo.subscribe(
94
        (values: IPowerClient[]) => {
95
      this.iPowerClientSuggestions = values;
96

    
97
      const temp: string[] = [];
98
      values.map(val => temp.push(val.name));
99
      this.iPowerClientNameSuggestions = temp;
100
    },
101
    err => console.error(err)   // TODO: Handle this via a Messaging Service
102
  );
103
}
104

    
105
  public resetForm(): void {
106
    this.searchFilter.reset();
107
  }
108

    
109
  canPreviewAll() : boolean {
110
    return this.authService.userHasRightForClient(USER_RIGHTS.A02, environment.globalRightsClientID);
111
  }
112

    
113
  canPreviewByUA() : boolean {
114
    return this.authService.userRights.find(rdc => USER_RIGHTS.A03.isGrantedToUser(rdc.rights)) != null;
115
  }
116

    
117
  iPowerClientNameSelected(name: string): void {
118
    this.selectedIPowerClient = this.iPowerClientSuggestions.find(client => client.name === name);
119
    this.searchFilter.get('iPowerClientCode').patchValue(this.selectedIPowerClient ? this.selectedIPowerClient.clientCode : '');
120
    this.searchFilter.updateValueAndValidity();
121
  }
122

    
123
  iPowerClientCodeSelected(code: string): void {
124
    this.selectedIPowerClient = this.iPowerClientSuggestions.find(client => client.clientCode === code);
125
    this.searchFilter.get('iPowerClientName').patchValue(this.selectedIPowerClient ? this.selectedIPowerClient.name : '');
126
    this.searchFilter.updateValueAndValidity();
127
  }
128
}
(4-4/4)