@@ -594,6 +594,9 @@ RestWrite.prototype.handleInstallation = function() {
594
594
595
595
var promise = Promise . resolve ( ) ;
596
596
597
+ var idMatch ; // Will be a match on either objectId or installationId
598
+ var deviceTokenMatches = [ ] ;
599
+
597
600
if ( this . query && this . query . objectId ) {
598
601
promise = promise . then ( ( ) => {
599
602
return this . config . database . find ( '_Installation' , {
@@ -603,22 +606,22 @@ RestWrite.prototype.handleInstallation = function() {
603
606
throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND ,
604
607
'Object not found for update.' ) ;
605
608
}
606
- var existing = results [ 0 ] ;
607
- if ( this . data . installationId && existing . installationId &&
608
- this . data . installationId !== existing . installationId ) {
609
+ idMatch = results [ 0 ] ;
610
+ if ( this . data . installationId && idMatch . installationId &&
611
+ this . data . installationId !== idMatch . installationId ) {
609
612
throw new Parse . Error ( 136 ,
610
613
'installationId may not be changed in this ' +
611
614
'operation' ) ;
612
615
}
613
- if ( this . data . deviceToken && existing . deviceToken &&
614
- this . data . deviceToken !== existing . deviceToken &&
615
- ! this . data . installationId && ! existing . installationId ) {
616
+ if ( this . data . deviceToken && idMatch . deviceToken &&
617
+ this . data . deviceToken !== idMatch . deviceToken &&
618
+ ! this . data . installationId && ! idMatch . installationId ) {
616
619
throw new Parse . Error ( 136 ,
617
620
'deviceToken may not be changed in this ' +
618
621
'operation' ) ;
619
622
}
620
623
if ( this . data . deviceType && this . data . deviceType &&
621
- this . data . deviceType !== existing . deviceType ) {
624
+ this . data . deviceType !== idMatch . deviceType ) {
622
625
throw new Parse . Error ( 136 ,
623
626
'deviceType may not be changed in this ' +
624
627
'operation' ) ;
@@ -629,8 +632,6 @@ RestWrite.prototype.handleInstallation = function() {
629
632
}
630
633
631
634
// Check if we already have installations for the installationId/deviceToken
632
- var installationMatch ;
633
- var deviceTokenMatches = [ ] ;
634
635
promise = promise . then ( ( ) => {
635
636
if ( this . data . installationId ) {
636
637
return this . config . database . find ( '_Installation' , {
@@ -641,7 +642,7 @@ RestWrite.prototype.handleInstallation = function() {
641
642
} ) . then ( ( results ) => {
642
643
if ( results && results . length ) {
643
644
// We only take the first match by installationId
644
- installationMatch = results [ 0 ] ;
645
+ idMatch = results [ 0 ] ;
645
646
}
646
647
if ( this . data . deviceToken ) {
647
648
return this . config . database . find (
@@ -653,7 +654,7 @@ RestWrite.prototype.handleInstallation = function() {
653
654
if ( results ) {
654
655
deviceTokenMatches = results ;
655
656
}
656
- if ( ! installationMatch ) {
657
+ if ( ! idMatch ) {
657
658
if ( ! deviceTokenMatches . length ) {
658
659
return ;
659
660
} else if ( deviceTokenMatches . length == 1 &&
@@ -691,14 +692,14 @@ RestWrite.prototype.handleInstallation = function() {
691
692
// Exactly one device token match and it doesn't have an installation
692
693
// ID. This is the one case where we want to merge with the existing
693
694
// object.
694
- var delQuery = { objectId : installationMatch . objectId } ;
695
+ var delQuery = { objectId : idMatch . objectId } ;
695
696
return this . config . database . destroy ( '_Installation' , delQuery )
696
697
. then ( ( ) => {
697
698
return deviceTokenMatches [ 0 ] [ 'objectId' ] ;
698
699
} ) ;
699
700
} else {
700
701
if ( this . data . deviceToken &&
701
- installationMatch . deviceToken != this . data . deviceToken ) {
702
+ idMatch . deviceToken != this . data . deviceToken ) {
702
703
// We're setting the device token on an existing installation, so
703
704
// we should try cleaning out old installations that match this
704
705
// device token.
@@ -714,7 +715,7 @@ RestWrite.prototype.handleInstallation = function() {
714
715
this . config . database . destroy ( '_Installation' , delQuery ) ;
715
716
}
716
717
// In non-merge scenarios, just return the installation match id
717
- return installationMatch . objectId ;
718
+ return idMatch . objectId ;
718
719
}
719
720
}
720
721
} ) . then ( ( objId ) => {
0 commit comments