diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 01345a6bd..6d74e84c8 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,2 +1,3 @@ +- Fix: ensure device apikey in already provisioned device (#1430) - Upgrade mongodb dev dep from 4.7.0 to 4.17.0 - Upgrade mongoose dep from 5.13.14 to 5.13.20 diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index b68bd0273..d8fdaac1f 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -611,6 +611,17 @@ function checkRegistry(fn) { function findOrCreate(deviceId, group, callback) { getDeviceSilently(deviceId, group.service, group.subservice, function (error, device) { if (!error && device) { + if ( + (!('apikey' in device) || device.apikey === undefined) && + 'apikey' in group && + group.apikey !== undefined + ) { + logger.info(context, 'Update provisioned device %j with measure/group apikey %j', device, group.apikey); + device.apikey = group.apikey; // group apikey is the same of current measure apikey + updateRegisterDevice(device, function (error, device) { + callback(error, device, group); + }); + } callback(null, device, group); } else if (error.name === 'DEVICE_NOT_FOUND') { const newDevice = {