@@ -561,31 +561,30 @@ RestWrite.prototype.handleInstallation = function() {
561561 return ;
562562 }
563563
564- if ( ! this . query && ! this . data . deviceToken && ! this . data . installationId ) {
564+ if ( ! this . query && ! this . data . deviceToken && ! this . data . installationId && ! this . auth . installationId ) {
565565 throw new Parse . Error ( 135 ,
566566 'at least one ID field (deviceToken, installationId) ' +
567567 'must be specified in this operation' ) ;
568568 }
569569
570- if ( ! this . query && ! this . data . deviceType ) {
571- throw new Parse . Error ( 135 ,
572- 'deviceType must be specified in this operation' ) ;
573- }
574-
575570 // If the device token is 64 characters long, we assume it is for iOS
576571 // and lowercase it.
577572 if ( this . data . deviceToken && this . data . deviceToken . length == 64 ) {
578573 this . data . deviceToken = this . data . deviceToken . toLowerCase ( ) ;
579574 }
580575
581- // TODO: We may need installationId from headers, plumb through Auth?
582- // per installation_handler.go
583-
584576 // We lowercase the installationId if present
585577 if ( this . data . installationId ) {
586578 this . data . installationId = this . data . installationId . toLowerCase ( ) ;
587579 }
588580
581+ // If data.installationId is not set, we can lookup in the auth
582+ let installationId = this . data . installationId || this . auth . installationId ;
583+
584+ if ( installationId ) {
585+ installationId = installationId . toLowerCase ( ) ;
586+ }
587+
589588 var promise = Promise . resolve ( ) ;
590589
591590 var idMatch ; // Will be a match on either objectId or installationId
@@ -600,9 +599,9 @@ RestWrite.prototype.handleInstallation = function() {
600599 objectId : this . query . objectId
601600 } ) ;
602601 }
603- if ( this . data . installationId ) {
602+ if ( installationId ) {
604603 orQueries . push ( {
605- 'installationId' : this . data . installationId
604+ 'installationId' : installationId
606605 } ) ;
607606 }
608607 if ( this . data . deviceToken ) {
@@ -622,7 +621,7 @@ RestWrite.prototype.handleInstallation = function() {
622621 if ( this . query && this . query . objectId && result . objectId == this . query . objectId ) {
623622 objectIdMatch = result ;
624623 }
625- if ( result . installationId == this . data . installationId ) {
624+ if ( result . installationId == installationId ) {
626625 installationIdMatch = result ;
627626 }
628627 if ( result . deviceToken == this . data . deviceToken ) {
@@ -636,8 +635,8 @@ RestWrite.prototype.handleInstallation = function() {
636635 throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND ,
637636 'Object not found for update.' ) ;
638637 }
639- if ( this . data . installationId && objectIdMatch . installationId &&
640- this . data . installationId !== objectIdMatch . installationId ) {
638+ if ( installationId && objectIdMatch . installationId &&
639+ installationId !== objectIdMatch . installationId ) {
641640 throw new Parse . Error ( 136 ,
642641 'installationId may not be changed in this ' +
643642 'operation' ) ;
@@ -661,16 +660,21 @@ RestWrite.prototype.handleInstallation = function() {
661660 idMatch = objectIdMatch ;
662661 }
663662
664- if ( this . data . installationId && installationIdMatch ) {
663+ if ( installationId && installationIdMatch ) {
665664 idMatch = installationIdMatch ;
666665 }
666+ // need to specify deviceType only if it's new
667+ if ( ! this . query && ! this . data . deviceType && ! idMatch ) {
668+ throw new Parse . Error ( 135 ,
669+ 'deviceType must be specified in this operation' ) ;
670+ }
667671
668672 } ) . then ( ( ) => {
669673 if ( ! idMatch ) {
670674 if ( ! deviceTokenMatches . length ) {
671675 return ;
672676 } else if ( deviceTokenMatches . length == 1 &&
673- ( ! deviceTokenMatches [ 0 ] [ 'installationId' ] || ! this . data . installationId )
677+ ( ! deviceTokenMatches [ 0 ] [ 'installationId' ] || ! installationId )
674678 ) {
675679 // Single match on device token but none on installationId, and either
676680 // the passed object or the match is missing an installationId, so we
@@ -689,7 +693,7 @@ RestWrite.prototype.handleInstallation = function() {
689693 var delQuery = {
690694 'deviceToken' : this . data . deviceToken ,
691695 'installationId' : {
692- '$ne' : this . data . installationId
696+ '$ne' : installationId
693697 }
694698 } ;
695699 if ( this . data . appIdentifier ) {
0 commit comments