From 748cca985aed45a9f66b62897558cea923cb68fd Mon Sep 17 00:00:00 2001 From: Greta Date: Mon, 25 Nov 2024 13:33:20 +0000 Subject: [PATCH] SNRGY-3619 edit device instance: enable updates to local Id --- .../device-instances.component.ts | 3 +++ .../shared/device-instances-dialog.service.ts | 27 ++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/app/modules/devices/device-instances/device-instances.component.ts b/src/app/modules/devices/device-instances/device-instances.component.ts index 2dc90474..77be3731 100644 --- a/src/app/modules/devices/device-instances/device-instances.component.ts +++ b/src/app/modules/devices/device-instances/device-instances.component.ts @@ -106,6 +106,7 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit { sortDirection: SortDirection = 'asc'; userHasDeleteAuthorization = false; + userHasUpdateAuthorization = false; userHasReadDeviceUsageAuthorization = false; userHasUpdateDisplayNameAuthorization = false; userHasUpdateAttributesAuthorization = false; @@ -148,6 +149,7 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit { this.userHasUpdateDisplayNameAuthorization = this.deviceInstancesService.userHasUpdateDisplayNameAuthorization(); this.userHasUpdateAttributesAuthorization = this.deviceInstancesService.userHasUpdateAttributesAuthorization(); + this.userHasUpdateAuthorization = this.deviceInstancesService.userHasUpdateAuthorization(); if (this.userHasUpdateDisplayNameAuthorization || this.userHasUpdateAttributesAuthorization) { this.displayedColumns.push('edit'); @@ -308,6 +310,7 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit { editDevice(device: DeviceInstanceModel): void { this.deviceInstancesDialogService.openDeviceEditDialog( device, + this.userHasUpdateAuthorization, this.userHasUpdateDisplayNameAuthorization, this.userHasUpdateAttributesAuthorization, (spinnerState: boolean) => { diff --git a/src/app/modules/devices/device-instances/shared/device-instances-dialog.service.ts b/src/app/modules/devices/device-instances/shared/device-instances-dialog.service.ts index 0fbb6b6a..5689ea8d 100644 --- a/src/app/modules/devices/device-instances/shared/device-instances-dialog.service.ts +++ b/src/app/modules/devices/device-instances/shared/device-instances-dialog.service.ts @@ -128,7 +128,7 @@ export class DeviceInstancesDialogService { }); } - openDeviceEditDialog(device: DeviceInstanceModel, userHasUpdateDisplayNameAuthorization: boolean, userHasUpdateAttributesAuthorization: boolean, setSpinnerState?: (state: boolean) => void) { + openDeviceEditDialog(device: DeviceInstanceModel, userHasUpdateAuthorization: boolean, userHasUpdateDisplayNameAuthorization: boolean, userHasUpdateAttributesAuthorization: boolean, setSpinnerState?: (state: boolean) => void) { const dialogConfig = new MatDialogConfig(); dialogConfig.width = '50vh'; dialogConfig.disableClose = false; @@ -145,21 +145,28 @@ export class DeviceInstancesDialogService { concatMap((newDevice: DeviceInstanceModel) => { let obs: Observable = of(null); // Display Name is an attribute, so if someone got attribute authorization, display name is also allowed - if (newDevice.attributes !== undefined && userHasUpdateAttributesAuthorization) { - if(setSpinnerState) { + if (newDevice !== undefined && userHasUpdateAuthorization) { + if (setSpinnerState) { setSpinnerState(true); } - obs = this.deviceInstancesService.updateDeviceInstanceAttributes(device.id, newDevice.attributes); - } + obs = (this.deviceInstancesService.updateDeviceInstance(newDevice)); + } else { + if (newDevice !== undefined && newDevice.attributes !== undefined && userHasUpdateAttributesAuthorization) { + if (setSpinnerState) { + setSpinnerState(true); + } + obs = this.deviceInstancesService.updateDeviceInstanceAttributes(device.id, newDevice.attributes); + } - if(newDevice.display_name != null && userHasUpdateDisplayNameAuthorization && !userHasUpdateAttributesAuthorization) { - if(setSpinnerState) { - setSpinnerState(true); + if (newDevice !== undefined && newDevice.display_name != null && userHasUpdateDisplayNameAuthorization && !userHasUpdateAttributesAuthorization) { + if (setSpinnerState) { + setSpinnerState(true); + } + obs = this.deviceInstancesService.updateDeviceInstanceDisplayName(device.id, newDevice.display_name); } - obs = this.deviceInstancesService.updateDeviceInstanceDisplayName(device.id, newDevice.display_name); } return obs.pipe( - map((resp: (DeviceInstanceModel | null)) => { + map((resp) => { let errorOccured = false; if(resp === null) { errorOccured = true;