Commit d5cf27cdbf0359f3ade857b1a9b6ec0118fe3e7b

Authored by Igor Kulikov
1 parent 449cb594

Sms provider configuration component improvements

... ... @@ -92,6 +92,10 @@ export function isEmptyStr(value: any): boolean {
92 92 return value === '';
93 93 }
94 94
  95 +export function isNotEmptyStr(value: any): boolean {
  96 + return value !== null && typeof value === 'string' && value.trim().length > 0;
  97 +}
  98 +
95 99 export function isFunction(value: any): boolean {
96 100 return typeof value === 'function';
97 101 }
... ...
... ... @@ -20,7 +20,11 @@ import { Store } from '@ngrx/store';
20 20 import { AppState } from '@app/core/core.state';
21 21 import { coerceBooleanProperty } from '@angular/cdk/coercion';
22 22 import { isDefinedAndNotNull } from '@core/utils';
23   -import { AwsSnsSmsProviderConfiguration } from '@shared/models/settings.models';
  23 +import {
  24 + AwsSnsSmsProviderConfiguration,
  25 + SmsProviderConfiguration,
  26 + SmsProviderType
  27 +} from '@shared/models/settings.models';
24 28
25 29 @Component({
26 30 selector: 'tb-aws-sns-provider-configuration',
... ... @@ -93,6 +97,7 @@ export class AwsSnsProviderConfigurationComponent implements ControlValueAccesso
93 97 let configuration: AwsSnsSmsProviderConfiguration = null;
94 98 if (this.awsSnsProviderConfigurationFormGroup.valid) {
95 99 configuration = this.awsSnsProviderConfigurationFormGroup.value;
  100 + (configuration as SmsProviderConfiguration).type = SmsProviderType.AWS_SNS;
96 101 }
97 102 this.propagateChange(configuration);
98 103 }
... ...
... ... @@ -27,7 +27,7 @@ import {
27 27 import { deepClone } from '@core/utils';
28 28 import {
29 29 createSmsProviderConfiguration,
30   - SmsProviderConfiguration,
  30 + SmsProviderConfiguration, smsProviderConfigurationValidator,
31 31 SmsProviderType,
32 32 smsProviderTypeTranslationMap
33 33 } from '@shared/models/settings.models';
... ... @@ -78,7 +78,7 @@ export class SmsProviderConfigurationComponent implements ControlValueAccessor,
78 78 ngOnInit() {
79 79 this.smsProviderConfigurationFormGroup = this.fb.group({
80 80 type: [null, Validators.required],
81   - configuration: [null, Validators.required]
  81 + configuration: [null, smsProviderConfigurationValidator(true)]
82 82 });
83 83 this.smsProviderConfigurationFormGroup.valueChanges.subscribe(() => {
84 84 this.updateModel();
... ...
... ... @@ -20,7 +20,11 @@ import { Store } from '@ngrx/store';
20 20 import { AppState } from '@app/core/core.state';
21 21 import { coerceBooleanProperty } from '@angular/cdk/coercion';
22 22 import { isDefinedAndNotNull } from '@core/utils';
23   -import { phoneNumberPattern, TwilioSmsProviderConfiguration } from '@shared/models/settings.models';
  23 +import {
  24 + phoneNumberPattern,
  25 + SmsProviderConfiguration, SmsProviderType,
  26 + TwilioSmsProviderConfiguration
  27 +} from '@shared/models/settings.models';
24 28
25 29 @Component({
26 30 selector: 'tb-twilio-sms-provider-configuration',
... ... @@ -95,6 +99,7 @@ export class TwilioSmsProviderConfigurationComponent implements ControlValueAcce
95 99 let configuration: TwilioSmsProviderConfiguration = null;
96 100 if (this.twilioSmsProviderConfigurationFormGroup.valid) {
97 101 configuration = this.twilioSmsProviderConfigurationFormGroup.value;
  102 + (configuration as SmsProviderConfiguration).type = SmsProviderType.TWILIO;
98 103 }
99 104 this.propagateChange(configuration);
100 105 }
... ...
... ... @@ -15,6 +15,8 @@
15 15 ///
16 16
17 17 import { DeviceTransportType } from '@shared/models/device.models';
  18 +import { ValidatorFn } from '@angular/forms';
  19 +import { isNotEmptyStr } from '@core/utils';
18 20
19 21 export const smtpPortPattern: RegExp = /^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
20 22
... ... @@ -95,6 +97,36 @@ export interface SmsProviderConfiguration extends SmsProviderConfigurations {
95 97 type: SmsProviderType;
96 98 }
97 99
  100 +export function smsProviderConfigurationValidator(required: boolean): ValidatorFn {
  101 + return control => {
  102 + const configuration: SmsProviderConfiguration = control.value;
  103 + let errors = null;
  104 + if (required) {
  105 + let valid = false;
  106 + if (configuration && configuration.type) {
  107 + switch (configuration.type) {
  108 + case SmsProviderType.AWS_SNS:
  109 + const awsSnsConfiguration: AwsSnsSmsProviderConfiguration = configuration;
  110 + valid = isNotEmptyStr(awsSnsConfiguration.accessKeyId) && isNotEmptyStr(awsSnsConfiguration.secretAccessKey)
  111 + && isNotEmptyStr(awsSnsConfiguration.region);
  112 + break;
  113 + case SmsProviderType.TWILIO:
  114 + const twilioConfiguration: TwilioSmsProviderConfiguration = configuration;
  115 + valid = isNotEmptyStr(twilioConfiguration.numberFrom) && isNotEmptyStr(twilioConfiguration.accountSid)
  116 + && isNotEmptyStr(twilioConfiguration.accountToken);
  117 + break;
  118 + }
  119 + }
  120 + if (!valid) {
  121 + errors = {
  122 + invalid: true
  123 + };
  124 + }
  125 + }
  126 + return errors;
  127 + };
  128 +}
  129 +
98 130 export interface TestSmsRequest {
99 131 providerConfiguration: SmsProviderConfiguration;
100 132 numberTo: string;
... ...