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 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 }
... ...
... ... @@ -14,7 +14,8 @@
14 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 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 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 129 export interface TestSmsRequest {
99 130 providerConfiguration: SmsProviderConfiguration;
100 131 numberTo: string;
... ...