Commit abfb6893ba7dd67abba3e42f2c3f4f47b3b4a6d2

Authored by Vladyslav_Prykhodko
1 parent 10e6b9a4

UI: Added unique alarm type validation in alarm rule

... ... @@ -41,6 +41,9 @@
41 41 <mat-error *ngIf="alarmFormGroup.get('alarmType').hasError('required')">
42 42 {{ 'device-profile.alarm-type-required' | translate }}
43 43 </mat-error>
  44 + <mat-error *ngIf="alarmFormGroup.get('alarmType').hasError('unique')">
  45 + {{ 'device-profile.alarm-type-unique' | translate }}
  46 + </mat-error>
44 47 <mat-hint *ngIf="!disabled"
45 48 innerHTML="{{ 'device-profile.alarm-type-pattern-hint' | translate }}"></mat-hint>
46 49 </mat-form-field>
... ...
... ... @@ -133,6 +133,19 @@ export class DeviceProfileAlarmComponent implements ControlValueAccessor, OnInit
133 133 }
134 134
135 135 public validate(c: FormControl) {
  136 + if (c.parent) {
  137 + const alarmType = c.value.alarmType;
  138 + const profileAlarmsType = [];
  139 + c.parent.getRawValue().forEach((alarm: DeviceProfileAlarm) => {
  140 + profileAlarmsType.push(alarm.alarmType);
  141 + }
  142 + );
  143 + if (profileAlarmsType.filter(profileAlarmType => profileAlarmType === alarmType).length > 1) {
  144 + this.alarmFormGroup.get('alarmType').setErrors({
  145 + unique: true
  146 + });
  147 + }
  148 + }
136 149 return (this.alarmFormGroup.valid) ? null : {
137 150 alarm: {
138 151 valid: false,
... ...
... ... @@ -38,7 +38,7 @@ import {
38 38 } from '@shared/models/device.models';
39 39 import { EntityType } from '@shared/models/entity-type.models';
40 40 import { RuleChainId } from '@shared/models/id/rule-chain-id';
41   -import {ServiceType} from "@shared/models/queue.models";
  41 +import { ServiceType } from '@shared/models/queue.models';
42 42
43 43 @Component({
44 44 selector: 'tb-device-profile',
... ... @@ -176,10 +176,10 @@ export class DeviceProfileComponent extends EntityComponent<DeviceProfile> {
176 176 transportConfiguration: entity.profileData?.transportConfiguration,
177 177 alarms: entity.profileData?.alarms,
178 178 provisionConfiguration: deviceProvisionConfiguration
179   - }});
180   - this.entityForm.patchValue({defaultRuleChainId: entity.defaultRuleChainId ? entity.defaultRuleChainId.id : null});
181   - this.entityForm.patchValue({defaultQueueName: entity.defaultQueueName});
182   - this.entityForm.patchValue({description: entity.description});
  179 + }}, {emitEvent: false});
  180 + this.entityForm.patchValue({defaultRuleChainId: entity.defaultRuleChainId ? entity.defaultRuleChainId.id : null}, {emitEvent: false});
  181 + this.entityForm.patchValue({defaultQueueName: entity.defaultQueueName}, {emitEvent: false});
  182 + this.entityForm.patchValue({description: entity.description}, {emitEvent: false});
183 183 }
184 184
185 185 prepareFormValue(formValue: any): any {
... ...
... ... @@ -909,6 +909,7 @@
909 909 "edit-alarm-rule": "Edit alarm rule",
910 910 "alarm-type": "Alarm type",
911 911 "alarm-type-required": "Alarm type is required.",
  912 + "alarm-type-unique": "Alarm type needs to be unique.",
912 913 "alarm-type-pattern-hint": "Alarm type pattern, use <code>${metaKeyName}</code> to substitute variables from metadata",
913 914 "create-alarm-pattern": "Create <b>{{alarmType}}</b> alarm",
914 915 "create-alarm-rules": "Create alarm rules",
... ...