1
|
import { VerificationRulesService } from './../../../../shared/services/administration/verification-rules.service';
|
2
|
import { VerificationRule } from './../../../../shared/models/verification-rule.interface';
|
3
|
import { Component, Input, OnInit } from '@angular/core';
|
4
|
import { AbstractControl, FormBuilder } from '@angular/forms';
|
5
|
import { Category } from 'src/app/shared/models/category.interface';
|
6
|
import { DocumentClassification } from 'src/app/shared/models/document-classification.interface';
|
7
|
import { DocumentSubclassification } from 'src/app/shared/models/document-subclassification.interface';
|
8
|
import { IPowerClient } from 'src/app/shared/models/ipower-client.interface';
|
9
|
import { CategoriesService } from 'src/app/shared/services/administration/categories.service';
|
10
|
import { DocumentClassificationsService } from 'src/app/shared/services/administration/document-classifications.service';
|
11
|
import { DocumentSubclassificationsService } from 'src/app/shared/services/administration/document-subclassifications.service';
|
12
|
import { IpowerClientsService } from 'src/app/shared/services/administration/ipower-clients.service';
|
13
|
import { VerificationRuleSearchFormValue } from './verification-rule-search-form-value.interface';
|
14
|
import { ErrorHandlingService } from 'src/app/shared/services/error-handling/error-handling.service';
|
15
|
import { USER_RIGHTS } from 'src/app/shared/enums/USER_RIGHTS.enum';
|
16
|
import { AuthService } from 'src/app/shared/services/auth.service';
|
17
|
import { Observable } from 'rxjs';
|
18
|
import { environment } from 'src/environments/environment';
|
19
|
|
20
|
@Component({
|
21
|
selector: 'app-verification-rule-search-form',
|
22
|
templateUrl: './verification-rule-search-form.component.html',
|
23
|
styleUrls: ['./verification-rule-search-form.component.scss']
|
24
|
})
|
25
|
export class VerificationRuleSearchFormComponent implements OnInit {
|
26
|
|
27
|
@Input() disabled: boolean = false;
|
28
|
|
29
|
documentClassificationsList: DocumentClassification[];
|
30
|
documentSubclassificationsList: DocumentSubclassification[];
|
31
|
availableDocumentSubclassifications: DocumentSubclassification[];
|
32
|
previouslySelectedClassificationId: number;
|
33
|
|
34
|
categoryNameSuggestions: string[];
|
35
|
categoryCodeSuggestions: string[];
|
36
|
categorySuggestions: Category[];
|
37
|
selectedCategory: Category = null;
|
38
|
|
39
|
ipowerClientNameSuggestions: string[];
|
40
|
ipowerClientCodeSuggestions: string[];
|
41
|
ipowerClientSuggestions: IPowerClient[];
|
42
|
selectedIPowerClient: IPowerClient = null;
|
43
|
|
44
|
displayValidationMessagesEvenIfPristine: boolean;
|
45
|
|
46
|
verificationRuleForm = this.fb.group({
|
47
|
ipowerName: [null],
|
48
|
ipowerCode: [null],
|
49
|
categoryName: [null],
|
50
|
categoryCode: [null],
|
51
|
documentClassification: [null],
|
52
|
subCategoryCode: [null] // This actually represents the complete DocumentSubclassification object.
|
53
|
});
|
54
|
|
55
|
constructor(
|
56
|
private fb: FormBuilder,
|
57
|
private documentClassificationsService: DocumentClassificationsService,
|
58
|
private documentSubclassificationsService: DocumentSubclassificationsService,
|
59
|
private verificationRulesService: VerificationRulesService,
|
60
|
private categoriesService: CategoriesService,
|
61
|
private ipowerClientsService: IpowerClientsService,
|
62
|
private errorHandlingService: ErrorHandlingService,
|
63
|
private authService: AuthService
|
64
|
) { }
|
65
|
|
66
|
ngOnInit(): void {
|
67
|
|
68
|
if (this.disabled) {
|
69
|
Object.keys(this.verificationRuleForm.controls).forEach(ctrl => this.verificationRuleForm.get(ctrl).disable());
|
70
|
return; // Don't even bother initialising or requesting anything.
|
71
|
}
|
72
|
|
73
|
this.initData();
|
74
|
}
|
75
|
|
76
|
initData() {
|
77
|
this.documentClassificationsService.getAll().subscribe(
|
78
|
value => this.documentClassificationsList = value,
|
79
|
err => this.errorHandlingService.showHttpResponseError(err)
|
80
|
);
|
81
|
|
82
|
// This is NOT the list of Subclassifications used for the dropdown.
|
83
|
this.documentSubclassificationsService.getAll().subscribe(
|
84
|
value => {
|
85
|
this.documentSubclassificationsList = value;
|
86
|
this.availableDocumentSubclassifications = value;
|
87
|
},
|
88
|
err => this.errorHandlingService.showHttpResponseError(err)
|
89
|
);
|
90
|
}
|
91
|
|
92
|
clear() {
|
93
|
this.verificationRuleForm.reset();
|
94
|
|
95
|
this.selectedCategory = null;
|
96
|
this.selectedIPowerClient = null;
|
97
|
}
|
98
|
|
99
|
documentClassificationSelected(selection: DocumentClassification) {
|
100
|
|
101
|
// If a different Classification has been selected, reset the Subclassification's value.
|
102
|
if (this.previouslySelectedClassificationId && selection.classificationId != this.previouslySelectedClassificationId) {
|
103
|
this.verificationRuleForm.get('subCategoryCode').reset();
|
104
|
}
|
105
|
this.previouslySelectedClassificationId = this.verificationRuleForm.get('documentClassification').value.classificationId;
|
106
|
|
107
|
this.availableDocumentSubclassifications = this.documentSubclassificationsList.filter(element => element.documentClassification.classificationId == selection.classificationId);
|
108
|
}
|
109
|
|
110
|
documentSubclassificationSelected(selection: DocumentSubclassification) {
|
111
|
this.verificationRuleForm.get('documentClassification').setValue(selection.documentClassification);
|
112
|
}
|
113
|
|
114
|
/*
|
115
|
* Auto-suggest/complete Categories
|
116
|
*/
|
117
|
autosuggestCategoryName(event) {
|
118
|
|
119
|
if (!event.query || event.query.length < 3) {
|
120
|
this.categoryNameSuggestions = [];
|
121
|
return;
|
122
|
}
|
123
|
|
124
|
let classId = this.verificationRuleForm.get('documentClassification').value ? this.verificationRuleForm.get('documentClassification').value.classificationId : null;
|
125
|
this.categoriesService.autosuggestCategoryName(event.query, classId).subscribe(
|
126
|
(values: Category[]) => {
|
127
|
let temp: string[] = [];
|
128
|
this.categorySuggestions = values;
|
129
|
values.map(val => temp.push(val.categoryName));
|
130
|
this.categoryNameSuggestions = temp
|
131
|
},
|
132
|
err => this.errorHandlingService.showHttpResponseError(err)
|
133
|
);
|
134
|
}
|
135
|
|
136
|
autosuggestCategoryCode(event) {
|
137
|
|
138
|
if (event.query.length < 3) {
|
139
|
this.categoryCodeSuggestions = [];
|
140
|
return;
|
141
|
}
|
142
|
|
143
|
let classId = this.verificationRuleForm.get('documentClassification').value ? this.verificationRuleForm.get('documentClassification').value.classificationId : null;
|
144
|
this.categoriesService.autosuggestCategoryCode(event.query, classId).subscribe(
|
145
|
(values: Category[]) => {
|
146
|
let temp: string[] = [];
|
147
|
this.categorySuggestions = values;
|
148
|
values.map(val => temp.push(val.categoryCode));
|
149
|
this.categoryCodeSuggestions = temp
|
150
|
},
|
151
|
err => this.errorHandlingService.showHttpResponseError(err)
|
152
|
);
|
153
|
}
|
154
|
|
155
|
categoryNameSelected(name: string) {
|
156
|
this.selectedCategory = this.categorySuggestions.find(cat => cat.categoryName == name);
|
157
|
this.verificationRuleForm.get('categoryCode').patchValue(this.selectedCategory.categoryCode);
|
158
|
this.verificationRuleForm.updateValueAndValidity();
|
159
|
}
|
160
|
|
161
|
categoryCodeSelected(code: string) {
|
162
|
this.selectedCategory = this.categorySuggestions.find(cat => cat.categoryCode == code);
|
163
|
this.verificationRuleForm.get('categoryName').patchValue(this.selectedCategory.categoryName);
|
164
|
this.verificationRuleForm.updateValueAndValidity();
|
165
|
}
|
166
|
|
167
|
// /*
|
168
|
// * Auto-suggest/complete iPower Clients
|
169
|
// */
|
170
|
// autosuggestIPowerClientName(event) {
|
171
|
|
172
|
// if (!event.query || event.query.length < 3) {
|
173
|
// this.ipowerClientNameSuggestions = [];
|
174
|
// return;
|
175
|
// }
|
176
|
|
177
|
// this.ipowerClientsService.getClientsByNameDistinct(event.query).subscribe(
|
178
|
// (values: IPowerClient[]) => {
|
179
|
// let temp: string[] = [];
|
180
|
// this.ipowerClientSuggestions = values;
|
181
|
// values.map(val => temp.push(val.name));
|
182
|
// this.ipowerClientNameSuggestions = temp
|
183
|
// },
|
184
|
// err => this.errorHandlingService.showHttpResponseError(err)
|
185
|
// );
|
186
|
// }
|
187
|
|
188
|
// autosuggestIPowerClientCode(event) {
|
189
|
|
190
|
// if (!event.query || event.query.length < 3) {
|
191
|
// this.ipowerClientCodeSuggestions = [];
|
192
|
// return;
|
193
|
// }
|
194
|
|
195
|
// this.ipowerClientsService.getClientsByCodeDistinct(event.query).subscribe(
|
196
|
// (values: IPowerClient[]) => {
|
197
|
// let temp: string[] = [];
|
198
|
// this.ipowerClientSuggestions = values;
|
199
|
// values.map(val => temp.push(val.clientCode));
|
200
|
// this.ipowerClientCodeSuggestions = temp
|
201
|
// },
|
202
|
// err => this.errorHandlingService.showHttpResponseError(err)
|
203
|
// );
|
204
|
// }
|
205
|
|
206
|
autosuggestIPowerClientCode(event) {
|
207
|
|
208
|
if (event.query.length < 3) {
|
209
|
this.ipowerClientCodeSuggestions = [];
|
210
|
return;
|
211
|
}
|
212
|
|
213
|
// If the user has the right to Preview of Scheduling Procedure (A02), we use the endpoint that returns all iPowerClients,
|
214
|
// 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.
|
215
|
let endpointToSubscribeTo: Observable<IPowerClient[]> = this.authService.userHasRightForClient(USER_RIGHTS.B01, environment.globalRightsClientID)
|
216
|
? this.ipowerClientsService.getClientsByCodeOnly(event.query)
|
217
|
: this.authService.userRights.find(rdc => USER_RIGHTS.B02.isGrantedToUser(rdc.rights)) != null ? this.ipowerClientsService.getClientsByCodeDistinct(event.query) : null;
|
218
|
|
219
|
endpointToSubscribeTo.subscribe(
|
220
|
(values: IPowerClient[]) => {
|
221
|
let temp: string[] = [];
|
222
|
this.ipowerClientSuggestions = values;
|
223
|
values.map(val => temp.push(val.clientCode));
|
224
|
this.ipowerClientCodeSuggestions = temp
|
225
|
},
|
226
|
err => this.errorHandlingService.showHttpResponseError(err)
|
227
|
);
|
228
|
}
|
229
|
|
230
|
/*
|
231
|
* Auto-suggest & Auto-complete IPower Client
|
232
|
*/
|
233
|
autosuggestIPowerClientName(event): void {
|
234
|
|
235
|
if (!event.query || event.query.length < 3) {
|
236
|
this.ipowerClientNameSuggestions = [];
|
237
|
return;
|
238
|
}
|
239
|
|
240
|
// If the user has the right to Preview of Scheduling Procedure (A02), we use the endpoint that returns all iPowerClients,
|
241
|
// 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.
|
242
|
let endpointToSubscribeTo: Observable<IPowerClient[]> = this.authService.userHasRightForClient(USER_RIGHTS.B01, environment.globalRightsClientID)
|
243
|
? this.ipowerClientsService.getClientsByNameOnly(event.query)
|
244
|
: this.authService.userRights.find(rdc => USER_RIGHTS.B02.isGrantedToUser(rdc.rights)) != null ? this.ipowerClientsService.getClientsByNameDistinct(event.query) : null;
|
245
|
|
246
|
endpointToSubscribeTo.subscribe(
|
247
|
(values: IPowerClient[]) => {
|
248
|
this.ipowerClientSuggestions = values;
|
249
|
|
250
|
const temp: string[] = [];
|
251
|
values.map(val => temp.push(val.name));
|
252
|
this.ipowerClientNameSuggestions = temp;
|
253
|
},
|
254
|
err => this.errorHandlingService.showHttpResponseError(err)
|
255
|
);
|
256
|
}
|
257
|
|
258
|
ipowerClientNameSelected(name: string) {
|
259
|
this.selectedIPowerClient = this.ipowerClientSuggestions.find(client => client.name == name);
|
260
|
this.verificationRuleForm.get('ipowerCode').patchValue(this.selectedIPowerClient.clientCode);
|
261
|
this.verificationRuleForm.updateValueAndValidity();
|
262
|
}
|
263
|
|
264
|
ipowerClientCodeSelected(code: string) {
|
265
|
this.selectedIPowerClient = this.ipowerClientSuggestions.find(client => client.clientCode == code);
|
266
|
this.verificationRuleForm.get('ipowerName').patchValue(this.selectedIPowerClient.name);
|
267
|
this.verificationRuleForm.updateValueAndValidity();
|
268
|
}
|
269
|
|
270
|
/*
|
271
|
* Utility Methods
|
272
|
*/
|
273
|
// Auto-suggest inputs - We have to ensure our reactive form holds values that represent the selectedCategory, otherwise truncate it.
|
274
|
syncSelectedCategory() {
|
275
|
|
276
|
// Ιf our form has no value, truncate the selectedCategory either way.
|
277
|
if (!this.verificationRuleForm.get('categoryName').value && !this.verificationRuleForm.get('categoryCode').value) {
|
278
|
this.selectedCategory = null;
|
279
|
return;
|
280
|
}
|
281
|
|
282
|
// If both or either of our form's values match the selectedCategory's and the other one doesn't have a value, all is good.
|
283
|
// Just sync the values in case one is missing. Otherwise truncate the selectedCategory.
|
284
|
if (
|
285
|
this.verificationRuleForm.get('categoryName').value == this.selectedCategory.categoryName
|
286
|
|| this.verificationRuleForm.get('categoryCode').value == this.selectedCategory.categoryCode
|
287
|
) {
|
288
|
this.selectedCategory.categoryName = this.verificationRuleForm.get('categoryName').value;
|
289
|
this.selectedCategory.categoryCode = this.verificationRuleForm.get('categoryCode').value;
|
290
|
}
|
291
|
|
292
|
// If both our values were different from the selectedCategory's, truncate it. This is an extremely abnormal scenario.
|
293
|
else {
|
294
|
console.error('WARNING - syncSelectedCategory()', 'Both of our form\'s values were different from the selectedCategory\'s.');
|
295
|
this.selectedCategory = null;
|
296
|
this.verificationRuleForm.get('categoryName').setValue('');
|
297
|
this.verificationRuleForm.get('categoryCode').setValue('');
|
298
|
this.verificationRuleForm.updateValueAndValidity();
|
299
|
}
|
300
|
}
|
301
|
|
302
|
// Auto-suggest inputs - We have to ensure our reactive form holds values that represent the selectedIPowerClient, otherwise truncate it.
|
303
|
syncSelectedIPowerClient() {
|
304
|
|
305
|
// Ιf our form has no value, truncate the selectedIPowerClient either way.
|
306
|
if (!this.verificationRuleForm.get('ipowerName').value && !this.verificationRuleForm.get('ipowerCode').value) {
|
307
|
this.selectedIPowerClient = null;
|
308
|
return;
|
309
|
}
|
310
|
|
311
|
// If both or either of our form's values match the selectedIPowerClient's and the other one doesn't have a value, all is good.
|
312
|
// Just sync the values in case one is missing. Otherwise truncate the selectedIPowerClient.
|
313
|
if (
|
314
|
this.verificationRuleForm.get('ipowerName').value == this.selectedIPowerClient.name
|
315
|
|| this.verificationRuleForm.get('ipowerCode').value == this.selectedIPowerClient.clientCode
|
316
|
) {
|
317
|
this.selectedIPowerClient.name = this.verificationRuleForm.get('ipowerName').value;
|
318
|
this.selectedIPowerClient.clientCode = this.verificationRuleForm.get('ipowerCode').value;
|
319
|
}
|
320
|
|
321
|
// If both our values were different from the selectedIPowerClient's, truncate it. This is an extremely abnormal scenario.
|
322
|
else {
|
323
|
console.error('WARNING - syncSelectedIPowerClient()', 'Both of our form\'s values were different from the selectedIPowerClient\'s.');
|
324
|
this.selectedIPowerClient = null;
|
325
|
this.verificationRuleForm.get('ipowerName').setValue('');
|
326
|
this.verificationRuleForm.get('ipowerCode').setValue('');
|
327
|
this.verificationRuleForm.updateValueAndValidity();
|
328
|
}
|
329
|
}
|
330
|
|
331
|
/*
|
332
|
* API methods
|
333
|
*/
|
334
|
public resetForm(): void {
|
335
|
this.verificationRuleForm.reset();
|
336
|
}
|
337
|
|
338
|
// TODO: Set type
|
339
|
public formValue(): VerificationRuleSearchFormValue {
|
340
|
|
341
|
this.syncSelectedCategory();
|
342
|
this.syncSelectedIPowerClient();
|
343
|
|
344
|
let formValue: VerificationRuleSearchFormValue = {
|
345
|
clientId: this.selectedIPowerClient ? this.selectedIPowerClient.id : '',
|
346
|
categoryId: this.selectedCategory ? this.selectedCategory.id : null,
|
347
|
docClassificationId: this.verificationRuleForm.get('documentClassification').value ? this.verificationRuleForm.get('documentClassification').value.classificationId : '',
|
348
|
// The backend only requires the subcategory's name, and not the whole object. Don't ask me.
|
349
|
docSubcategory: this.verificationRuleForm.get('subCategoryCode').value ? this.verificationRuleForm.get('subCategoryCode').value['subclassificationName'] : ''
|
350
|
}
|
351
|
|
352
|
return formValue;
|
353
|
}
|
354
|
|
355
|
public isValid(): boolean {
|
356
|
return this.verificationRuleForm.valid;
|
357
|
}
|
358
|
}
|