-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unique index to userdevices to prevent duplicate device registrat… (
#196) * Add unique index to userdevices to prevent duplicate device registrations This also requires a manual effort when upgrading to remove duplicated entries, until the unique index can be enforced by the database. Fixes #195 Signed-off-by: Florian Schmidt <florian.schmidt.welzow@t-online.de> * Also check for duplicate androidregistrationid and iosdevicetoken Signed-off-by: Florian Schmidt <florian.schmidt.welzow@t-online.de>
- Loading branch information
1 parent
cea8d8f
commit 81a414e
Showing
3 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const mongoose = require('mongoose'), | ||
logger = require('../logger.js'), | ||
config = require('../config.json'), | ||
UserDevice = require('../models/userdevice'), | ||
system = require('../system'), | ||
MongoConnect = require('../system/mongoconnect'); | ||
|
||
system.setConfiguration(config); | ||
|
||
const mongoConnect = new MongoConnect(system); | ||
mongoConnect.connect(mongoose); | ||
|
||
logger.info('Looking for all registered devices...'); | ||
function deleteDuplicateUserDevices(err, devices) { | ||
if (err) { | ||
logger.error('Could not load all devices to fix duplicates.', err); | ||
} | ||
|
||
logger.info('Found ' + devices.length + ' devices...'); | ||
const alreadyRegisteredDeviceId = []; | ||
const alreadyRegisteredAndroidRegistration = []; | ||
const alreadyRegisteredIOSToken = []; | ||
devices.forEach(function(device) { | ||
if ( | ||
!alreadyRegisteredDeviceId.includes(device.deviceId) && | ||
!alreadyRegisteredAndroidRegistration.includes(device.androidRegistration) && | ||
!alreadyRegisteredIOSToken.includes(device.iosDeviceToken) | ||
) { | ||
alreadyRegisteredDeviceId.push(device.deviceId); | ||
alreadyRegisteredAndroidRegistration.push(device.androidRegistration); | ||
alreadyRegisteredIOSToken.push(device.iosDeviceToken); | ||
return; | ||
} | ||
|
||
logger.info('Remove duplicated device with deviceId ' + device.deviceId + ' and ID ' + device.id); | ||
device.remove(); | ||
}); | ||
|
||
if (alreadyRegisteredDeviceId.length === devices.length) { | ||
logger.info('No duplicated devices found.'); | ||
} else { | ||
logger.info('All duplicated devices should be removed now.'); | ||
} | ||
|
||
process.exit(0); | ||
} | ||
|
||
UserDevice.find({}, deleteDuplicateUserDevices); |