From bea4f5e64af128a9806d5ef743d544ac34d25c7e Mon Sep 17 00:00:00 2001 From: Iain Anderson Date: Fri, 25 Sep 2020 16:43:00 +0100 Subject: [PATCH] fix(metadata): Notify both device services when a Device is moved from one to the other (#2716) Signed-off-by: Iain Anderson --- internal/core/metadata/operators/device/events.go | 11 ++++++----- internal/core/metadata/operators/device/notify.go | 13 +++++++++++++ internal/core/metadata/operators/device/update.go | 4 ++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/internal/core/metadata/operators/device/events.go b/internal/core/metadata/operators/device/events.go index 18f1223722..6fcc6b418e 100644 --- a/internal/core/metadata/operators/device/events.go +++ b/internal/core/metadata/operators/device/events.go @@ -1,9 +1,10 @@ package device type DeviceEvent struct { - DeviceId string - DeviceName string - Error error - HttpMethod string - ServiceId string + DeviceId string + DeviceName string + Error error + HttpMethod string + ServiceId string + SecondaryServiceId string } diff --git a/internal/core/metadata/operators/device/notify.go b/internal/core/metadata/operators/device/notify.go index 99db41ed59..16fbf3e48b 100644 --- a/internal/core/metadata/operators/device/notify.go +++ b/internal/core/metadata/operators/device/notify.go @@ -85,6 +85,19 @@ func (op deviceNotifier) Execute() { if err := op.callback(service, deviceId, httpMethod, models.DEVICE); err != nil { op.logger.Error(err.Error()) } + + if msg.SecondaryServiceId != "" { + // Callback for secondary device service + service, err := op.database.GetDeviceServiceById(msg.SecondaryServiceId) + if err != nil { + op.logger.Error(err.Error()) + return + } + + if err := op.callback(service, deviceId, httpMethod, models.DEVICE); err != nil { + op.logger.Error(err.Error()) + } + } } } diff --git a/internal/core/metadata/operators/device/update.go b/internal/core/metadata/operators/device/update.go index 4ccecea1c4..02c04b123c 100644 --- a/internal/core/metadata/operators/device/update.go +++ b/internal/core/metadata/operators/device/update.go @@ -143,6 +143,10 @@ func (op updateDevice) Execute() (err error) { evt.HttpMethod = http.MethodPut evt.ServiceId = op.device.Service.Id + if op.device.Service.Id != oldDevice.Service.Id { + evt.SecondaryServiceId = oldDevice.Service.Id + } + op.events <- evt return nil