Skip to content

Commit

Permalink
separated widget update endpoints for name and other widget specific …
Browse files Browse the repository at this point in the history
…properties; fix #SNRGY-2814
  • Loading branch information
hahahannes committed Nov 28, 2023
1 parent 7bf4723 commit 0d6b7e5
Show file tree
Hide file tree
Showing 123 changed files with 846 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export class LadonService {
environment.processIoUrl,
environment.dashboardServiceUrl + '/dashboards',
environment.dashboardServiceUrl + '/widgets',
environment.dashboardServiceUrl + '/widgets/name',
environment.dashboardServiceUrl + '/widgets/properties',
environment.usersServiceUrl,
environment.notificationsUrl,
environment.waitingRoomUrl
Expand Down
11 changes: 9 additions & 2 deletions src/app/modules/dashboard/dashboard.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@

<div *ngIf="zoomedWidgetIndex !== null">
<div class="zoomed-widget">
<senergy-widget [userHasDeleteAuthorization]="userHasDeleteWidgetAuthorization" [userHasUpdateAuthorization]="userHasUpdateWidgetAuthorization"
<senergy-widget [userHasDeleteAuthorization]="userHasDeleteWidgetAuthorization"
[userHasUpdatePropertiesAuthorization]="userHasUpdateWidgetPropertiesAuthorization"
[userHasUpdateNameAuthorization]="userHasUpdateWidgetNameAuthorization"
[dashboardId]="dashboard.id"
[widget]="dashboards[activeTabIndex].widgets[zoomedWidgetIndex]"
[zoom]="true"></senergy-widget>
Expand All @@ -91,7 +93,12 @@
<gridster-item [item]="widget" *ngFor="let widget of dashboards[activeTabIndex].widgets"
[ngClass]="inDragMode ? 'highlight color-sidenav' : ''"
class="mat-elevation-z1">
<senergy-widget [userHasDeleteAuthorization]="userHasDeleteWidgetAuthorization" [userHasUpdateAuthorization]="userHasUpdateWidgetAuthorization" [dashboardId]="dashboard.id" [widget]="widget" [zoom]="false"></senergy-widget>
<senergy-widget [userHasDeleteAuthorization]="userHasDeleteWidgetAuthorization"
[userHasUpdatePropertiesAuthorization]="userHasUpdateWidgetPropertiesAuthorization"
[userHasUpdateNameAuthorization]="userHasUpdateWidgetNameAuthorization"
[dashboardId]="dashboard.id"
[widget]="widget"
[zoom]="false"></senergy-widget>
</gridster-item>
</gridster>
</div>
Expand Down
7 changes: 5 additions & 2 deletions src/app/modules/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ export class DashboardComponent implements OnInit, OnDestroy {
userHasUpdateDashboardAuthorization: boolean = false
userHasDeleteDashboardAuthorization: boolean = false
userHasCreateDashboardAuthorization: boolean = false
userHasUpdateWidgetAuthorization: boolean = false
userHasUpdateWidgetPropertiesAuthorization: boolean = false
userHasDeleteWidgetAuthorization: boolean = false
userHasCreateWidgetAuthorization: boolean = false
userHasMoveWidgetAuthorization: boolean = false
userHasUpdateWidgetNameAuthorization: boolean = false

constructor(
private responsiveService: ResponsiveService,
Expand Down Expand Up @@ -106,7 +107,9 @@ export class DashboardComponent implements OnInit, OnDestroy {
this.userHasDeleteDashboardAuthorization = this.dashboardService.userHasDeleteDashboardAuthorization()
this.userHasCreateDashboardAuthorization = this.dashboardService.userHasCreateDashboardAuthorization()

this.userHasUpdateWidgetAuthorization = this.dashboardService.userHasUpdateWidgetAuthorization()
this.userHasUpdateWidgetPropertiesAuthorization = this.dashboardService.userHasUpdateWidgetPropertiesAuthorization()
this.userHasUpdateWidgetNameAuthorization = this.dashboardService.userHasUpdateWidgetNameAuthorization()

this.userHasDeleteWidgetAuthorization = this.dashboardService.userHasDeleteWidgetAuthorization()
this.userHasCreateWidgetAuthorization = this.dashboardService.userHasCreateWidgetAuthorization()
this.userHasMoveWidgetAuthorization = this.dashboardService.userHasMoveWidgetAuthorization()
Expand Down
31 changes: 26 additions & 5 deletions src/app/modules/dashboard/shared/dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export class DashboardService {

dashboardAuthorizations: PermissionTestResponse
widgetAuthorizations: PermissionTestResponse
widgetNameAuthorizations: PermissionTestResponse
widgetPositionAuthorizations: PermissionTestResponse
widgetPropertiesAuthorizations: PermissionTestResponse

constructor(
private dialog: MatDialog,
Expand All @@ -58,6 +61,9 @@ export class DashboardService {
) {
this.dashboardAuthorizations = this.ladonService.getUserAuthorizationsForURI(environment.dashboardServiceUrl + '/dashboards')
this.widgetAuthorizations = this.ladonService.getUserAuthorizationsForURI(environment.dashboardServiceUrl + '/widgets')
this.widgetNameAuthorizations = this.ladonService.getUserAuthorizationsForURI(environment.dashboardServiceUrl + '/widgets/name')
this.widgetPositionAuthorizations = this.ladonService.getUserAuthorizationsForURI(environment.dashboardServiceUrl + '/widgets/position')
this.widgetPropertiesAuthorizations = this.ladonService.getUserAuthorizationsForURI(environment.dashboardServiceUrl + '/widgets/properties')
}


Expand Down Expand Up @@ -107,10 +113,21 @@ export class DashboardService {
.pipe(catchError(this.errorHandlerService.handleError(DashboardService.name, 'deleteWidget', { message: 'error delete' })));
}

updateWidget(dashboardId: string, widget: WidgetModel): Observable<DashboardResponseMessageModel> {
updateWidgetProperty(dashboardId: string, widgetId: string, pathToProperty: string[], newValue: any): Observable<DashboardResponseMessageModel> {
var url = environment.dashboardServiceUrl + '/widgets/properties'
if(pathToProperty.length > 0) {
var pathToPropertyString = pathToProperty.join(".")
url += pathToPropertyString
}
return this.http
.put<DashboardResponseMessageModel>(environment.dashboardServiceUrl + '/widgets/' + dashboardId + '/' + widget.id, widget)
.pipe(catchError(this.errorHandlerService.handleError(DashboardService.name, 'updateWidget', { message: 'error update' })));
.patch<DashboardResponseMessageModel>(url + '/' + dashboardId + '/' + widgetId, newValue)
.pipe(catchError(this.errorHandlerService.handleError(DashboardService.name, 'updateWidgetProperty', { message: 'error update' })));
}

updateWidgetName(dashboardId: string, widgetId: string, name: any): Observable<DashboardResponseMessageModel> {
return this.http
.patch<DashboardResponseMessageModel>(environment.dashboardServiceUrl + '/widgets/name/' + dashboardId + '/' + widgetId, name)
.pipe(catchError(this.errorHandlerService.handleError(DashboardService.name, 'updateWidgetName', { message: 'error update' })));
}

updateWidgetPosition(dashboardId: string, widgetPositions: WidgetUpdatePosition[]): Observable<DashboardResponseMessageModel> {
Expand Down Expand Up @@ -221,8 +238,8 @@ export class DashboardService {
return this.widgetAuthorizations["DELETE"]
}

userHasUpdateWidgetAuthorization(): boolean {
return this.widgetAuthorizations["PUT"]
userHasUpdateWidgetPropertiesAuthorization(): boolean {
return this.widgetPropertiesAuthorizations["PATCH"]
}

userHasCreateWidgetAuthorization(): boolean {
Expand All @@ -232,4 +249,8 @@ export class DashboardService {
userHasMoveWidgetAuthorization(): boolean {
return this.widgetAuthorizations["PATCH"]
}

userHasUpdateWidgetNameAuthorization(): boolean {
return this.widgetNameAuthorizations["PATCH"]
}
}
2 changes: 1 addition & 1 deletion src/app/widgets/ac-control/ac-control.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
</div>
</mat-card-content>
<div>
<senergy-widget-footer [userHasDeleteAuthorization]="userHasDeleteAuthorization" [userHasUpdateAuthorization]="userHasUpdateAuthorization" [dashboardId]="dashboardId" [widget]="widget"
<senergy-widget-footer [widgetHasUpdateableProperties]="true" [userHasUpdateNameAuthorization]="userHasUpdateNameAuthorization" [userHasDeleteAuthorization]="userHasDeleteAuthorization" [userHasUpdatePropertiesAuthorization]="userHasUpdatePropertiesAuthorization" [dashboardId]="dashboardId" [widget]="widget"
(editEvent)="edit()" [refreshing]="refreshing"></senergy-widget-footer>
</div>
</mat-card>
Expand Down
5 changes: 4 additions & 1 deletion src/app/widgets/ac-control/ac-control.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class AcControlComponent implements OnInit, OnDestroy {
@Input() widget: WidgetModel = {} as WidgetModel;
@Input() zoom = false;
@Input() userHasDeleteAuthorization = false;
@Input() userHasUpdateAuthorization = false;
@Input() userHasUpdatePropertiesAuthorization = false;
@Input() userHasUpdateNameAuthorization = false;

ready = false;
refreshing = false;
Expand All @@ -62,6 +63,8 @@ export class AcControlComponent implements OnInit, OnDestroy {
dialogConfig.data = {
widget: this.widget,
dashboardId: this.dashboardId,
userHasUpdateNameAuth: this.userHasUpdateNameAuthorization,
userHasUpdatePropertiesAuthorization: this.userHasUpdatePropertiesAuthorization
};
dialogConfig.minWidth = '350px';
const editDialogRef = this.dialog.open(AcControlEditDialogComponent, dialogConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2 mat-dialog-title>Edit AC Control</h2>
<mat-dialog-content >
<senergy-widget-spinner [show]="!ready"></senergy-widget-spinner>
<form *ngIf="ready" [formGroup]="form" fxLayout="column">
<mat-form-field color="accent">
<mat-form-field color="accent" *ngIf="userHasUpdateNameAuthorization">
<mat-label>Widget Name</mat-label>
<input type="text" matInput placeholder="Widget Name" formControlName="name" required>
</mat-form-field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {DeviceInstancesService} from '../../../modules/devices/device-instances/
import {map} from 'rxjs/operators';
import {UntypedFormBuilder, Validators} from '@angular/forms';
import {AcControlElementModel} from '../shared/ac-control.model';
import {Observable} from 'rxjs';
import {Observable, of, concatMap, forkJoin} from 'rxjs';
import {DeviceGroupsService} from '../../../modules/devices/device-groups/shared/device-groups.service';

const INTEGER = 'https://schema.org/Integer';
Expand Down Expand Up @@ -52,17 +52,26 @@ export class AcControlEditDialogComponent implements OnInit {
environment.getBatteryLevelFunctionId,
];
selectables: DeviceSelectablesFullModel[] = [];
userHasUpdateNameAuthorization: boolean = false
userHasUpdatePropertiesAuthorization: boolean = false

constructor(private dialogRef: MatDialogRef<AcControlEditDialogComponent>,
private dashboardService: DashboardService,
private deviceTypeService: DeviceTypeService,
private deviceInstancesService: DeviceInstancesService,
private deviceGroupsService: DeviceGroupsService,
private fb: UntypedFormBuilder,
@Inject(MAT_DIALOG_DATA) data: { widget: WidgetModel; dashboardId: string },
@Inject(MAT_DIALOG_DATA) data: {
widget: WidgetModel;
dashboardId: string;
userHasUpdateNameAuthorization: boolean;
userHasUpdatePropertiesAuthorization: boolean;
},
) {
this.widget = data.widget;
this.dashboardId = data.dashboardId;
this.userHasUpdateNameAuthorization = data.userHasUpdateNameAuthorization;
this.userHasUpdatePropertiesAuthorization = data.userHasUpdatePropertiesAuthorization;
}

ngOnInit(): void {
Expand Down Expand Up @@ -96,14 +105,17 @@ export class AcControlEditDialogComponent implements OnInit {
this.dialogRef.close();
}

save(): void {
this.ready = false;
updateName(): Observable<DashboardResponseMessageModel> {
var newName = this.form.get('name')?.value;
return this.dashboardService.updateWidgetName(this.dashboardId, this.widget.id, newName)
}

updateACProperties(): Observable<DashboardResponseMessageModel> {
this.widget.properties.acControl = {
minTarget: this.form.get('minTarget')?.value,
maxTarget: this.form.get('maxTarget')?.value,
tempStep: this.tempStep,
};
this.widget.name = this.form.get('name')?.value;

const selectable = this.selectables.find(s => s.device?.id === this.form.get('selectable')?.value || s.device_group?.id === this.form.get('selectable')?.value);

Expand Down Expand Up @@ -134,8 +146,9 @@ export class AcControlEditDialogComponent implements OnInit {
return mappings;
}));
}

if (observableMappings !== undefined) {
observableMappings.pipe(map(mappings => {
return observableMappings.pipe(map(mappings => {
if (this.widget.properties.acControl === undefined) {
return;
}
Expand Down Expand Up @@ -217,18 +230,31 @@ export class AcControlEditDialogComponent implements OnInit {
const m = mappings.get(environment.getBatteryLevelFunctionId)?.[0];
this.widget.properties.acControl.getBatteryLevel = this.getElement(m, environment.getBatteryLevelFunctionId);
}
}), map(_ => {
this.dashboardService.updateWidget(this.dashboardId, this.widget).subscribe((resp: DashboardResponseMessageModel) => {
if (resp.message === 'OK') {
this.dialogRef.close(this.widget);
} else {
this.ready = true;
}
});
})).subscribe();
} else {
this.ready = true;
}), concatMap(_ => {
return this.dashboardService.updateWidgetProperty(this.dashboardId, this.widget.id, [], this.widget.properties)
}))
}

return of({"message": ""})
}

save(): void {
this.ready = false;
var obs = []
if(this.userHasUpdatePropertiesAuthorization) {
obs.push(this.updateACProperties())
}
if(this.userHasUpdateNameAuthorization) {
obs.push(this.updateName())
}
forkJoin(obs).subscribe(results => {
const errorOccured = results.find((response) => response.message != "OK")
if(errorOccured) {
this.ready = true
} else {
this.dialogRef.close(this.widget);
}
})
}

private traverseDataStructure(serviceId: string, field: DeviceTypeContentVariableModel | undefined, functionIds: string[], results: Map<string, { serviceId: string; aspectId: string }[]>) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/widgets/air-quality/air-quality.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
</div>
</mat-card-content>
<div>
<senergy-widget-footer [userHasDeleteAuthorization]="userHasDeleteAuthorization" [userHasUpdateAuthorization]="userHasUpdateAuthorization" [dashboardId]="dashboardId" [widget]="widget"
<senergy-widget-footer [widgetHasUpdateableProperties]="true" [userHasUpdateNameAuthorization]="userHasUpdateNameAuthorization" [userHasDeleteAuthorization]="userHasDeleteAuthorization" [userHasUpdatePropertiesAuthorization]="userHasUpdatePropertiesAuthorization" [dashboardId]="dashboardId" [widget]="widget"
(editEvent)="edit()" [refreshing]="refreshing"></senergy-widget-footer>
</div>
</mat-card>
Expand Down
5 changes: 3 additions & 2 deletions src/app/widgets/air-quality/air-quality.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export class AirQualityComponent implements OnInit, OnDestroy {
@Input() widget: WidgetModel = {} as WidgetModel;
@Input() zoom = false;
@Input() userHasDeleteAuthorization = false;
@Input() userHasUpdateAuthorization = false;
@Input() userHasUpdatePropertiesAuthorization = false;
@Input() userHasUpdateNameAuthorization = false;

constructor(
private iconRegistry: MatIconRegistry,
Expand Down Expand Up @@ -72,7 +73,7 @@ export class AirQualityComponent implements OnInit, OnDestroy {
}

edit() {
this.airRecommendationService.openEditDialog(this.dashboardId, this.widget.id);
this.airRecommendationService.openEditDialog(this.dashboardId, this.widget.id, this.userHasUpdateNameAuthorization, this.userHasUpdatePropertiesAuthorization);
}

private update() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2 mat-dialog-title>Edit Air Recommendation</h2>
<mat-dialog-content>
<senergy-widget-spinner [show]="!ready"></senergy-widget-spinner>
<mat-vertical-stepper *ngIf="ready" #stepper>
<mat-step label="Give the widget a name">
<mat-step label="Give the widget a name" *ngIf="userHasUpdateNameAuthorization">
<mat-form-field color="accent">
<input type="text" matInput placeholder="Name" [(ngModel)]="name" required>
</mat-form-field>
Expand Down
Loading

0 comments on commit 0d6b7e5

Please sign in to comment.