Commit cd0e86439856396b886d146a35d5eb1f85a62773

Authored by Igor Kulikov
2 parents eb8975d5 6f780b8f

Merge branch 'master' into feature/sms-email-limits

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