committed by
GitHub
24 changed files with 860 additions and 13 deletions
@ -0,0 +1,82 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.notification.settings; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonCreator; |
||||
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
||||
|
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
import lombok.Data; |
||||
|
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; |
||||
|
import org.thingsboard.server.common.data.notification.NotificationType; |
||||
|
import org.thingsboard.server.common.data.notification.targets.NotificationTargetType; |
||||
|
|
||||
|
import javax.validation.Valid; |
||||
|
import javax.validation.constraints.AssertTrue; |
||||
|
import javax.validation.constraints.NotNull; |
||||
|
import java.util.Collections; |
||||
|
import java.util.Map; |
||||
|
import java.util.Set; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Data |
||||
|
public class UserNotificationSettings { |
||||
|
|
||||
|
@NotNull |
||||
|
@Valid |
||||
|
private final Map<NotificationType, NotificationPref> prefs; |
||||
|
|
||||
|
public static final UserNotificationSettings DEFAULT = new UserNotificationSettings(Collections.emptyMap()); |
||||
|
|
||||
|
public static final Set<NotificationDeliveryMethod> deliveryMethods = NotificationTargetType.PLATFORM_USERS.getSupportedDeliveryMethods(); |
||||
|
|
||||
|
@JsonCreator |
||||
|
public UserNotificationSettings(@JsonProperty("prefs") Map<NotificationType, NotificationPref> prefs) { |
||||
|
this.prefs = prefs; |
||||
|
} |
||||
|
|
||||
|
public boolean isEnabled(NotificationType notificationType, NotificationDeliveryMethod deliveryMethod) { |
||||
|
NotificationPref pref = prefs.get(notificationType); |
||||
|
if (pref == null) { |
||||
|
return true; |
||||
|
} |
||||
|
if (!pref.isEnabled()) { |
||||
|
return false; |
||||
|
} |
||||
|
return pref.getEnabledDeliveryMethods().getOrDefault(deliveryMethod, true); |
||||
|
} |
||||
|
|
||||
|
@Data |
||||
|
public static class NotificationPref { |
||||
|
private boolean enabled; |
||||
|
@NotNull |
||||
|
private Map<NotificationDeliveryMethod, Boolean> enabledDeliveryMethods; |
||||
|
|
||||
|
public static NotificationPref createDefault() { |
||||
|
NotificationPref pref = new NotificationPref(); |
||||
|
pref.setEnabled(true); |
||||
|
pref.setEnabledDeliveryMethods(deliveryMethods.stream().collect(Collectors.toMap(v -> v, v -> true))); |
||||
|
return pref; |
||||
|
} |
||||
|
|
||||
|
@JsonIgnore |
||||
|
@AssertTrue(message = "Only email, Web and SMS delivery methods are allowed") |
||||
|
public boolean isValid() { |
||||
|
return enabledDeliveryMethods.entrySet().stream() |
||||
|
.allMatch(entry -> deliveryMethods.contains(entry.getKey()) && entry.getValue() != null); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2023 The Thingsboard Authors |
||||
|
|
||||
|
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
you may not use this file except in compliance with the License. |
||||
|
You may obtain a copy of the License at |
||||
|
|
||||
|
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
|
||||
|
Unless required by applicable law or agreed to in writing, software |
||||
|
distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
See the License for the specific language governing permissions and |
||||
|
limitations under the License. |
||||
|
|
||||
|
--> |
||||
|
<form [formGroup]="notificationSettingsFormGroup" fxLayout="column"> |
||||
|
<div fxLayout="row" style="height: 48px;"> |
||||
|
<div fxFlex="50" fxLayoutAlign="start center"> |
||||
|
<mat-checkbox color="primary" |
||||
|
[checked]="notificationSettingsFormGroup.get('enabled').value" |
||||
|
(click)="toggleEnabled()"> |
||||
|
<span class="notification-type" |
||||
|
[ngClass]="{'notification-type-disabled': !notificationSettingsFormGroup.get('enabled').value}"> |
||||
|
{{notificationTemplateTypeTranslateMap.get(notificationSettingsFormGroup.get('name').value)?.name | translate}} |
||||
|
</span> |
||||
|
</mat-checkbox> |
||||
|
</div> |
||||
|
<div fxFlex fxLayout="row" *ngFor="let deliveryMethods of deliveryMethods"> |
||||
|
<div fxFlex fxLayoutAlign="start center"> |
||||
|
<mat-checkbox color="primary" |
||||
|
[disabled]="!allowDeliveryMethods?.includes(deliveryMethods) || !notificationSettingsFormGroup.get('enabled').value" |
||||
|
[checked]="getChecked(deliveryMethods)" |
||||
|
(click)="toggleDeliviryMethod(deliveryMethods)" |
||||
|
></mat-checkbox> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
</form> |
||||
@ -0,0 +1,24 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
:host { |
||||
|
.notification-type { |
||||
|
font-size: 14px; |
||||
|
|
||||
|
&-disabled { |
||||
|
color: rgba(0, 0, 0, 0.38) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,126 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core'; |
||||
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; |
||||
|
import { UtilsService } from '@core/services/utils.service'; |
||||
|
import { isDefinedAndNotNull } from '@core/utils'; |
||||
|
import { Subscription } from 'rxjs'; |
||||
|
import { |
||||
|
NotificationDeliveryMethod, |
||||
|
NotificationTemplateTypeTranslateMap, |
||||
|
NotificationUserSetting |
||||
|
} from '@shared/models/notification.models'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-notification-setting-form', |
||||
|
templateUrl: './notification-setting-form.component.html', |
||||
|
styleUrls: ['./notification-setting-form.component.scss'], |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => NotificationSettingFormComponent), |
||||
|
multi: true |
||||
|
} |
||||
|
] |
||||
|
}) |
||||
|
export class NotificationSettingFormComponent implements ControlValueAccessor, OnInit, OnDestroy { |
||||
|
|
||||
|
@Input() |
||||
|
disabled: boolean; |
||||
|
|
||||
|
@Input() |
||||
|
deliveryMethods: NotificationDeliveryMethod[] = []; |
||||
|
|
||||
|
@Input() |
||||
|
allowDeliveryMethods: NotificationDeliveryMethod[] = []; |
||||
|
|
||||
|
notificationSettingsFormGroup: UntypedFormGroup; |
||||
|
|
||||
|
notificationTemplateTypeTranslateMap = NotificationTemplateTypeTranslateMap; |
||||
|
|
||||
|
private propagateChange = null; |
||||
|
|
||||
|
private valueChange$: Subscription = null; |
||||
|
|
||||
|
constructor(private utils: UtilsService, |
||||
|
private fb: UntypedFormBuilder) { |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any): void { |
||||
|
} |
||||
|
|
||||
|
ngOnInit() { |
||||
|
const deliveryMethod = {}; |
||||
|
this.deliveryMethods.forEach(value => { |
||||
|
deliveryMethod[value] = true; |
||||
|
}); |
||||
|
this.notificationSettingsFormGroup = this.fb.group( |
||||
|
{ |
||||
|
name: [''], |
||||
|
enabled: [true], |
||||
|
enabledDeliveryMethods: this.fb.group({ |
||||
|
...deliveryMethod |
||||
|
}) |
||||
|
}); |
||||
|
this.valueChange$ = this.notificationSettingsFormGroup.valueChanges.subscribe(() => { |
||||
|
this.updateModel(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
ngOnDestroy() { |
||||
|
if (this.valueChange$) { |
||||
|
this.valueChange$.unsubscribe(); |
||||
|
this.valueChange$ = null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean): void { |
||||
|
this.disabled = isDisabled; |
||||
|
if (this.disabled) { |
||||
|
this.notificationSettingsFormGroup.disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.notificationSettingsFormGroup.enable({emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
toggleEnabled() { |
||||
|
this.notificationSettingsFormGroup.get('enabled').patchValue(!this.notificationSettingsFormGroup.get('enabled').value); |
||||
|
} |
||||
|
|
||||
|
getChecked(deliveryMethod: NotificationDeliveryMethod): boolean { |
||||
|
return this.notificationSettingsFormGroup.get('enabledDeliveryMethods').get(deliveryMethod).value; |
||||
|
} |
||||
|
|
||||
|
toggleDeliviryMethod(deliveryMethod: NotificationDeliveryMethod) { |
||||
|
this.notificationSettingsFormGroup.get('enabledDeliveryMethods').get(deliveryMethod) |
||||
|
.patchValue(!this.notificationSettingsFormGroup.get('enabledDeliveryMethods').get(deliveryMethod).value); |
||||
|
} |
||||
|
|
||||
|
writeValue(value: NotificationUserSetting): void { |
||||
|
if (isDefinedAndNotNull(value)) { |
||||
|
this.notificationSettingsFormGroup.patchValue(value, {emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private updateModel() { |
||||
|
this.propagateChange(this.notificationSettingsFormGroup.value); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { Routes } from '@angular/router'; |
||||
|
import { ConfirmOnExitGuard } from '@core/guards/confirm-on-exit.guard'; |
||||
|
import { Authority } from '@shared/models/authority.enum'; |
||||
|
import { inject, NgModule } from '@angular/core'; |
||||
|
import { NotificationSettingsComponent } from '@home/pages/notification/settings/notification-settings.component'; |
||||
|
import { NotificationService } from '@core/http/notification.service'; |
||||
|
|
||||
|
export const notificationUserSettingsRoutes: Routes = [ |
||||
|
{ |
||||
|
path: 'notificationSettings', |
||||
|
component: NotificationSettingsComponent, |
||||
|
canDeactivate: [ConfirmOnExitGuard], |
||||
|
data: { |
||||
|
auth: [Authority.SYS_ADMIN, Authority.TENANT_ADMIN, Authority.CUSTOMER_USER], |
||||
|
title: 'account.notification-settings', |
||||
|
breadcrumb: { |
||||
|
label: 'account.notification-settings', |
||||
|
icon: 'settings' |
||||
|
} |
||||
|
}, |
||||
|
resolve: { |
||||
|
userSettings: () => inject(NotificationService).getNotificationUserSettings() |
||||
|
} |
||||
|
} |
||||
|
]; |
||||
@ -0,0 +1,84 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2023 The Thingsboard Authors |
||||
|
|
||||
|
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
you may not use this file except in compliance with the License. |
||||
|
You may obtain a copy of the License at |
||||
|
|
||||
|
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
|
||||
|
Unless required by applicable law or agreed to in writing, software |
||||
|
distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
See the License for the specific language governing permissions and |
||||
|
limitations under the License. |
||||
|
|
||||
|
--> |
||||
|
<div> |
||||
|
<mat-card appearance="outlined" class="settings-card tb-absolute-fill"> |
||||
|
<mat-card-header> |
||||
|
<div fxFlex fxLayout="row" fxLayout.xs="column" fxLayoutGap.xs="8px" |
||||
|
fxLayoutAlign="space-between start" fxLayoutAlign.xs="start start"> |
||||
|
<div fxFlex fxLayout="column"> |
||||
|
<span class="mat-headline-5" translate>notification.settings.notification-settings</span> |
||||
|
</div> |
||||
|
<div fxLayout="column"> |
||||
|
<button mat-button color="primary" |
||||
|
[disabled]="(isLoading$ | async)" |
||||
|
(click)="resetSettings()">{{ 'notification.settings.reset-all' | translate }}</button> |
||||
|
</div> |
||||
|
</div> |
||||
|
</mat-card-header> |
||||
|
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async"> |
||||
|
</mat-progress-bar> |
||||
|
<div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div> |
||||
|
<mat-card-content style="padding-top: 16px; overflow: hidden;"> |
||||
|
<form [formGroup]="notificationSettings" class="notification-form"> |
||||
|
<section class="notification-section"> |
||||
|
<div class="notification-section-block"> |
||||
|
<div fxLayout="row" fxLayoutAlign="start center" style="height: 44px;"> |
||||
|
<div fxFlex="50"> |
||||
|
<mat-checkbox color="warn" |
||||
|
(click)="$event.stopPropagation()" |
||||
|
[indeterminate]="getIndeterminate()" |
||||
|
(change)="changeInstanceTypeCheckBox($event.checked)" |
||||
|
[checked]="getChecked()"> |
||||
|
<mat-label translate>notification.settings.type</mat-label> |
||||
|
</mat-checkbox> |
||||
|
</div> |
||||
|
<div fxFlex *ngFor="let deliveryMethods of notificationDeliveryMethods"> |
||||
|
<div fxFlex fxLayoutAlign="start center"> |
||||
|
<mat-checkbox color="warn" |
||||
|
[disabled]="!allowNotificationDeliveryMethods?.includes(deliveryMethods) || !getSomeChecked()" |
||||
|
[checked]="getChecked(deliveryMethods)" |
||||
|
(change)="changeInstanceTypeCheckBox($event.checked, deliveryMethods)" |
||||
|
[indeterminate]="getIndeterminate(deliveryMethods)" |
||||
|
(click)="$event.stopPropagation()"> |
||||
|
<mat-label>{{ notificationDeliveryMethodTranslateMap.get(deliveryMethods) | translate }}</mat-label> |
||||
|
</mat-checkbox> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
<mat-divider></mat-divider> |
||||
|
<div *ngFor="let settingsControl of notificationSettingsFormArray.controls; let i = index; let $last = last;"> |
||||
|
<tb-notification-setting-form [formControl]="settingsControl" |
||||
|
[deliveryMethods]="notificationDeliveryMethods" |
||||
|
[allowDeliveryMethods]="allowNotificationDeliveryMethods"> |
||||
|
</tb-notification-setting-form> |
||||
|
<mat-divider *ngIf="!$last"></mat-divider> |
||||
|
</div> |
||||
|
</div> |
||||
|
</section> |
||||
|
</form> |
||||
|
</mat-card-content> |
||||
|
<div fxLayout="row" fxLayoutAlign="end start" style="padding: 16px;"> |
||||
|
<button mat-button mat-raised-button color="primary" |
||||
|
type="button" |
||||
|
(click)="save()" |
||||
|
[disabled]="(isLoading$ | async) || notificationSettings.invalid || !notificationSettings.dirty"> |
||||
|
{{ 'action.save' | translate }} |
||||
|
</button> |
||||
|
</div> |
||||
|
</mat-card> |
||||
|
</div> |
||||
@ -0,0 +1,41 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
@import "../../../../../../scss/constants"; |
||||
|
|
||||
|
:host { |
||||
|
.mat-mdc-card.settings-card { |
||||
|
margin: 8px; |
||||
|
@media #{$mat-gt-sm} { |
||||
|
width: 60%; |
||||
|
} |
||||
|
.mat-headline-5 { |
||||
|
margin: 0; |
||||
|
} |
||||
|
.notification-form { |
||||
|
height: 100%; |
||||
|
min-height: min-content; |
||||
|
max-height: min-content; |
||||
|
} |
||||
|
.notification-section { |
||||
|
height: 100%; |
||||
|
border: 1px solid rgba(0, 0, 0, 0.12); |
||||
|
overflow: scroll; |
||||
|
&-block { |
||||
|
min-width: 470px; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,188 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { Component, OnInit } from '@angular/core'; |
||||
|
import { PageComponent } from '@shared/components/page.component'; |
||||
|
import { Store } from '@ngrx/store'; |
||||
|
import { AppState } from '@core/core.state'; |
||||
|
import { AbstractControl, UntypedFormArray, UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; |
||||
|
import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard'; |
||||
|
import { TranslateService } from '@ngx-translate/core'; |
||||
|
import { ActivatedRoute } from '@angular/router'; |
||||
|
import { deepClone, isDefinedAndNotNull } from '@core/utils'; |
||||
|
import { |
||||
|
NotificationDeliveryMethod, |
||||
|
NotificationDeliveryMethodTranslateMap, |
||||
|
NotificationUserSettings |
||||
|
} from '@shared/models/notification.models'; |
||||
|
import { NotificationService } from '@core/http/notification.service'; |
||||
|
import { DialogService } from '@core/services/dialog.service'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-notification-settings', |
||||
|
templateUrl: './notification-settings.component.html', |
||||
|
styleUrls: ['./notification-settings.component.scss'] |
||||
|
}) |
||||
|
export class NotificationSettingsComponent extends PageComponent implements OnInit, HasConfirmForm { |
||||
|
|
||||
|
notificationSettings: UntypedFormGroup; |
||||
|
|
||||
|
notificationDeliveryMethods: NotificationDeliveryMethod[]; |
||||
|
notificationDeliveryMethodTranslateMap = NotificationDeliveryMethodTranslateMap; |
||||
|
|
||||
|
allowNotificationDeliveryMethods: Array<NotificationDeliveryMethod>; |
||||
|
|
||||
|
constructor(protected store: Store<AppState>, |
||||
|
private route: ActivatedRoute, |
||||
|
private translate: TranslateService, |
||||
|
private dialogService: DialogService, |
||||
|
private notificationService: NotificationService, |
||||
|
private fb: UntypedFormBuilder,) { |
||||
|
super(store); |
||||
|
} |
||||
|
|
||||
|
ngOnInit() { |
||||
|
this.notificationDeliveryMethods = this.getNotificationDeliveryMethods(); |
||||
|
|
||||
|
this.notificationService.getAvailableDeliveryMethods({ignoreLoading: true}).subscribe(allowMethods => { |
||||
|
this.allowNotificationDeliveryMethods = allowMethods; |
||||
|
}); |
||||
|
|
||||
|
this.buildNotificationSettingsForm(); |
||||
|
this.patchNotificationSettings(this.route.snapshot.data.userSettings); |
||||
|
} |
||||
|
|
||||
|
private getNotificationDeliveryMethods(): NotificationDeliveryMethod[] { |
||||
|
const deliveryMethods = new Set([ |
||||
|
NotificationDeliveryMethod.SLACK |
||||
|
]); |
||||
|
return Object.values(NotificationDeliveryMethod).filter(type => !deliveryMethods.has(type)); |
||||
|
} |
||||
|
|
||||
|
private buildNotificationSettingsForm() { |
||||
|
this.notificationSettings = this.fb.group({ |
||||
|
prefs: this.fb.array([]) |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private patchNotificationSettings(settings: NotificationUserSettings) { |
||||
|
const notificationSettingsControls: Array<AbstractControl> = []; |
||||
|
let preparedSettings; |
||||
|
if (settings.prefs) { |
||||
|
preparedSettings = this.prepareNotificationSettings(settings.prefs); |
||||
|
preparedSettings.forEach((setting) => { |
||||
|
setting.enabledDeliveryMethods = Object.assign( |
||||
|
this.notificationDeliveryMethods.reduce((a, v) => ({ ...a, [v]: true}), {}), |
||||
|
setting.enabledDeliveryMethods |
||||
|
); |
||||
|
notificationSettingsControls.push(this.fb.control(setting, [Validators.required])); |
||||
|
}); |
||||
|
} |
||||
|
this.notificationSettings.setControl('prefs', this.fb.array(notificationSettingsControls), {emitEvent: false}); |
||||
|
} |
||||
|
|
||||
|
private prepareNotificationSettings(prefs: any) { |
||||
|
return Object.entries(prefs).map((value: any) => { |
||||
|
value[1].name = value[0]; |
||||
|
return value[1]; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
resetSettings() { |
||||
|
this.dialogService.confirm( |
||||
|
this.translate.instant('notification.settings.reset-all-title'), |
||||
|
this.translate.instant('notification.settings.reset-all-text'), |
||||
|
this.translate.instant('action.no'), |
||||
|
this.translate.instant('action.yes'), |
||||
|
true |
||||
|
).subscribe( |
||||
|
result => { |
||||
|
if (result) { |
||||
|
const settings = this.prepareNotificationSettings(this.route.snapshot.data.userSettings.prefs); |
||||
|
const notificationSettingsControls: Array<AbstractControl> = []; |
||||
|
this.notificationSettings.reset({}); |
||||
|
if (settings) { |
||||
|
settings.forEach((setting) => { |
||||
|
setting.enabled = true; |
||||
|
setting.enabledDeliveryMethods = this.notificationDeliveryMethods.reduce((a, v) => ({ ...a, [v]: true}), {}); |
||||
|
notificationSettingsControls.push(this.fb.control(setting, [Validators.required])); |
||||
|
}); |
||||
|
} |
||||
|
this.notificationSettings.setControl('prefs', this.fb.array(notificationSettingsControls), {emitEvent: false}); |
||||
|
this.save(); |
||||
|
} |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
getChecked = (method: NotificationDeliveryMethod = null): boolean => { |
||||
|
const type = this.notificationSettings.get('prefs').value; |
||||
|
if (isDefinedAndNotNull(method)) { |
||||
|
return isDefinedAndNotNull(type) && type.every(resource => resource.enabledDeliveryMethods[method]); |
||||
|
} |
||||
|
return isDefinedAndNotNull(type) && type.every(resource => resource.enabled); |
||||
|
}; |
||||
|
|
||||
|
getSomeChecked = () => { |
||||
|
const type = this.notificationSettings.get('prefs').value; |
||||
|
return isDefinedAndNotNull(type) && type.some(resource => resource.enabled); |
||||
|
}; |
||||
|
|
||||
|
getIndeterminate = (deliveryMethod: NotificationDeliveryMethod = null): boolean => { |
||||
|
const type = this.notificationSettings.get('prefs').value; |
||||
|
if (isDefinedAndNotNull(type)) { |
||||
|
const checkedResource = isDefinedAndNotNull(deliveryMethod) ? |
||||
|
type.filter(resource => resource.enabledDeliveryMethods[deliveryMethod]) : |
||||
|
type.filter(resource => resource.enabled); |
||||
|
return checkedResource.length !== 0 && checkedResource.length !== type.length; |
||||
|
} |
||||
|
return false; |
||||
|
}; |
||||
|
|
||||
|
changeInstanceTypeCheckBox = (value: boolean, deliveryMethod: NotificationDeliveryMethod = null): void => { |
||||
|
const type = deepClone(this.notificationSettings.get('prefs').value); |
||||
|
if (isDefinedAndNotNull(deliveryMethod)) { |
||||
|
type.forEach(notificationType => notificationType.enabledDeliveryMethods[deliveryMethod] = value); |
||||
|
} else { |
||||
|
type.forEach(notificationType => notificationType.enabled = value); |
||||
|
} |
||||
|
this.notificationSettings.get('prefs').patchValue(type); |
||||
|
this.notificationSettings.markAsDirty(); |
||||
|
}; |
||||
|
|
||||
|
get notificationSettingsFormArray(): UntypedFormArray { |
||||
|
return this.notificationSettings.get('prefs') as UntypedFormArray; |
||||
|
} |
||||
|
|
||||
|
save(): void { |
||||
|
const settings = {prefs: {}}; |
||||
|
this.notificationSettings.getRawValue().prefs.forEach(value => { |
||||
|
const key = value.name; |
||||
|
delete value.name; |
||||
|
settings.prefs[key] = value; |
||||
|
}); |
||||
|
this.notificationService.saveNotificationUserSettings(settings).subscribe( |
||||
|
(userSettings) => { |
||||
|
this.notificationSettings.get('prefs').reset({}); |
||||
|
this.patchNotificationSettings(userSettings); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
confirmForm(): UntypedFormGroup { |
||||
|
return this.notificationSettings; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue