Project

General

Profile

1
import { AuthService } from 'src/app/shared/services/auth.service';
2
import { USER_RIGHTS } from './../../../../shared/enums/USER_RIGHTS.enum';
3
import { CreateVerificationRuleFormValue } from './../../forms/verification-rule-form/create-verification-rule-form-value.interface';
4

    
5
import { VerificationRule } from './../../../../shared/models/verification-rule.interface';
6
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
7
import { ConfirmationService, LazyLoadEvent } from 'primeng/api';
8
import { VerificationRulesService } from 'src/app/shared/services/administration/verification-rules.service';
9
import { Page } from '../../../../shared/models/paging/page.interface';
10
import { TranslateService } from '@ngx-translate/core';
11
import { NotificationsHandlingService } from 'src/app/shared/services/notifications-handling/notifications-handling.service';
12
import { environment } from 'src/environments/environment';
13

    
14
@Component({
15
  selector: 'app-verification-rules-search-results',
16
  templateUrl: './verification-rules-search-results.component.html',
17
  styleUrls: ['./verification-rules-search-results.component.scss']
18
})
19
export class VerificationRulesSearchResultsComponent implements OnInit {
20

    
21
  @Output() paginationEvent = new EventEmitter<{ page: number, offset: number }>(true); // TODO: Use interface.
22
  @Output() valueChange = new EventEmitter<any>();
23
  @Input() loading: boolean;
24
  @Input() set searchResultsPage(results: Page<VerificationRule>) {
25

    
26
    // This setter may be called on page setup without arguments.
27
    if (!results) {
28
      return;
29
    }
30

    
31
    this._searchResultsPage = results;
32

    
33
    this.totalRecords = results.totalElements;
34
  }
35

    
36
  // tslint:disable-next-line:variable-name
37
  _searchResultsPage: Page<VerificationRule>;
38
  totalRecords: number;
39
  rows = 10;
40
  header: string;
41
  selectedValues: string[] = ['Enabled'];
42

    
43
  displayEditVerificationRuleDialog: boolean;
44
  verificationRuleToEdit: VerificationRule;
45

    
46
  constructor(
47
    private verificationRulesService: VerificationRulesService,
48
    private authService: AuthService,
49
    private translate: TranslateService,
50
    private confirmationService: ConfirmationService,
51
    private notificationHandling: NotificationsHandlingService
52
  ) { }
53

    
54
  ngOnInit(): void {
55
  }
56

    
57
  onLazyLoad(event: LazyLoadEvent): void {
58
    const pageToRequest = Math.floor(event.first / event.rows);
59
    const pageSize = event.rows;
60

    
61
    this.paginationEvent.emit({ page: pageToRequest, offset: pageSize });
62
  }
63

    
64
  edit(verificationRule: VerificationRule): void {
65
    this.verificationRuleToEdit = verificationRule;
66
    this.header = this.translate.instant('EDIT-RULE');
67
    this.displayEditVerificationRuleDialog = true;
68
  }
69

    
70
  cancelEditVerificationRule(): void {
71
    this.displayEditVerificationRuleDialog = false;
72
    this.verificationRuleToEdit = null;
73
  }
74

    
75
  delete(verificationRule: VerificationRule): void {
76
    this.confirmationService.confirm({
77
      accept: () => {
78
        this.verificationRulesService.delete(verificationRule.id).subscribe(
79
          (success) => {
80
            this.valueChange.emit(success);
81
            this.notificationHandling.showDeleteVerificationSuccess();
82
          },
83
          (error) => console.error(error)
84
        );
85
        // Actual logic to perform a confirmation
86
      }, reject: () => {
87

    
88
      }
89
    });
90
  }
91

    
92
  changeStatus(verificationRule: VerificationRule): void {
93

    
94
    this.loading = true;
95

    
96
    // Reverse its status.
97
    const newStatus = verificationRule.verificationRuleStatus.trim().toLowerCase() === 'enabled' ? 'Disabled' : 'Enabled';
98

    
99
    // First, transform the VerificationRule to a CreateVerificationRuleFormValue.
100
    const createRuleModel: CreateVerificationRuleFormValue = {
101
      id: verificationRule.id,
102
      confidenceLevelMinThreshold: verificationRule.confidenceLevelMinThreshold,
103
      clientId: verificationRule.client?.id,
104
      categoryId: verificationRule.template?.category?.id,
105
      subCategoryCode: verificationRule.template?.subCategoryCode,
106
      capturingVerification: verificationRule.capturingVerification,
107
      journalVerification: verificationRule.journalVerification,
108
      alteryxRoutineId: verificationRule.alteryxRoutineId,
109
      verificationRuleStatus: newStatus,
110
      template: verificationRule.template
111
    };
112

    
113
    // Update the rule and after we receive confirmation, also change its value in the table.
114
    this.verificationRulesService.updateVerificationRule(createRuleModel).subscribe(
115
      result => {
116
        this._searchResultsPage.data.find(rule => rule.id === verificationRule.id).verificationRuleStatus = newStatus
117
        this.valueChange.emit(result)
118
        this.notificationHandling.showUpdateVerificationRuleSuccess();
119
      },
120
      (error) => console.error(error),     // TODO: Handle this via a Messaging Service
121
      () => this.loading = false
122
    );
123
  }
124

    
125
  /*
126
   * Rights-check Methods
127
   */
128
  canEditRuleByUA(rule: VerificationRule): boolean {
129
    return this.authService.userHasRightForClient(USER_RIGHTS.B08, rule.client.id);
130
  }
131

    
132
  canDeleteRule(): boolean {
133
    return this.authService.userHasRightForClient(USER_RIGHTS.B05, environment.globalRightsClientID);
134
  }
135

    
136
  canEditRuleStatus(): boolean {
137
    return this.authService.userHasRightForClient(USER_RIGHTS.B06, environment.globalRightsClientID);
138
  }
139

    
140
  canEditRuleAll(): boolean {
141
    return this.authService.userHasRightForClient(USER_RIGHTS.B04, environment.globalRightsClientID);
142
  }
143

    
144
  passEvent(passEvent: any) {
145
    if (passEvent) {
146
      this.valueChange.emit(passEvent);
147
    }
148
  }
149

    
150

    
151
}
(4-4/4)