Treat objectId and installationId matches the same when handling Installation writes

This commit is contained in:
stephentuso
2016-02-26 00:55:36 -05:00
parent 3b1165b686
commit 7ca8836768

View File

@@ -571,6 +571,9 @@ RestWrite.prototype.handleInstallation = function() {
var promise = Promise.resolve(); var promise = Promise.resolve();
var idMatch; // Will be a match on either objectId or installationId
var deviceTokenMatches = [];
if (this.query && this.query.objectId) { if (this.query && this.query.objectId) {
promise = promise.then(() => { promise = promise.then(() => {
return this.config.database.find('_Installation', { return this.config.database.find('_Installation', {
@@ -580,22 +583,22 @@ RestWrite.prototype.handleInstallation = function() {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
'Object not found for update.'); 'Object not found for update.');
} }
var existing = results[0]; idMatch = results[0];
if (this.data.installationId && existing.installationId && if (this.data.installationId && idMatch.installationId &&
this.data.installationId !== existing.installationId) { this.data.installationId !== idMatch.installationId) {
throw new Parse.Error(136, throw new Parse.Error(136,
'installationId may not be changed in this ' + 'installationId may not be changed in this ' +
'operation'); 'operation');
} }
if (this.data.deviceToken && existing.deviceToken && if (this.data.deviceToken && idMatch.deviceToken &&
this.data.deviceToken !== existing.deviceToken && this.data.deviceToken !== idMatch.deviceToken &&
!this.data.installationId && !existing.installationId) { !this.data.installationId && !idMatch.installationId) {
throw new Parse.Error(136, throw new Parse.Error(136,
'deviceToken may not be changed in this ' + 'deviceToken may not be changed in this ' +
'operation'); 'operation');
} }
if (this.data.deviceType && this.data.deviceType && if (this.data.deviceType && this.data.deviceType &&
this.data.deviceType !== existing.deviceType) { this.data.deviceType !== idMatch.deviceType) {
throw new Parse.Error(136, throw new Parse.Error(136,
'deviceType may not be changed in this ' + 'deviceType may not be changed in this ' +
'operation'); 'operation');
@@ -606,10 +609,8 @@ RestWrite.prototype.handleInstallation = function() {
} }
// Check if we already have installations for the installationId/deviceToken // Check if we already have installations for the installationId/deviceToken
var installationMatch;
var deviceTokenMatches = [];
promise = promise.then(() => { promise = promise.then(() => {
if (this.data.installationId) { if (!idMatch && this.data.installationId) {
return this.config.database.find('_Installation', { return this.config.database.find('_Installation', {
'installationId': this.data.installationId 'installationId': this.data.installationId
}); });
@@ -618,7 +619,7 @@ RestWrite.prototype.handleInstallation = function() {
}).then((results) => { }).then((results) => {
if (results && results.length) { if (results && results.length) {
// We only take the first match by installationId // We only take the first match by installationId
installationMatch = results[0]; idMatch = results[0];
} }
if (this.data.deviceToken) { if (this.data.deviceToken) {
return this.config.database.find( return this.config.database.find(
@@ -630,7 +631,7 @@ RestWrite.prototype.handleInstallation = function() {
if (results) { if (results) {
deviceTokenMatches = results; deviceTokenMatches = results;
} }
if (!installationMatch) { if (!idMatch) {
if (!deviceTokenMatches.length) { if (!deviceTokenMatches.length) {
return; return;
} else if (deviceTokenMatches.length == 1 && } else if (deviceTokenMatches.length == 1 &&
@@ -668,14 +669,14 @@ RestWrite.prototype.handleInstallation = function() {
// Exactly one device token match and it doesn't have an installation // Exactly one device token match and it doesn't have an installation
// ID. This is the one case where we want to merge with the existing // ID. This is the one case where we want to merge with the existing
// object. // object.
var delQuery = {objectId: installationMatch.objectId}; var delQuery = {objectId: idMatch.objectId};
return this.config.database.destroy('_Installation', delQuery) return this.config.database.destroy('_Installation', delQuery)
.then(() => { .then(() => {
return deviceTokenMatches[0]['objectId']; return deviceTokenMatches[0]['objectId'];
}); });
} else { } else {
if (this.data.deviceToken && if (this.data.deviceToken &&
installationMatch.deviceToken != this.data.deviceToken) { idMatch.deviceToken != this.data.deviceToken) {
// We're setting the device token on an existing installation, so // We're setting the device token on an existing installation, so
// we should try cleaning out old installations that match this // we should try cleaning out old installations that match this
// device token. // device token.
@@ -691,7 +692,7 @@ RestWrite.prototype.handleInstallation = function() {
this.config.database.destroy('_Installation', delQuery); this.config.database.destroy('_Installation', delQuery);
} }
// In non-merge scenarios, just return the installation match id // In non-merge scenarios, just return the installation match id
return installationMatch.objectId; return idMatch.objectId;
} }
} }
}).then((objId) => { }).then((objId) => {