@@ -73,8 +73,11 @@ export class DatabaseConnection {
7373 for ( let key in query ) {
7474 if ( key === "_id" ) {
7575 var id : string = query . _id ;
76- delete query . _id ;
77- query . $or = [ { _id : id } , { _id : safeObjectID ( id ) } ] ;
76+ var safeid = safeObjectID ( id ) ;
77+ if ( safeid !== null && safeid !== undefined ) {
78+ delete query . _id ;
79+ query . $or = [ { _id : id } , { _id : safeObjectID ( id ) } ] ;
80+ }
7881 }
7982 }
8083
@@ -98,6 +101,7 @@ export class DatabaseConnection {
98101 return value ; // leave any other value as-is
99102 } ) ;
100103 }
104+ var user : TokenUser = Crypt . verityToken ( jwt ) ;
101105 var _query : Object = { } ;
102106 if ( collectionname === "files" ) { collectionname = "fs.files" ; }
103107 if ( collectionname === "fs.files" ) {
@@ -124,6 +128,7 @@ export class DatabaseConnection {
124128 }
125129 for ( var i : number = 0 ; i < arr . length ; i ++ ) { arr [ i ] = this . decryptentity ( arr [ i ] ) ; }
126130 this . traversejsondecode ( arr ) ;
131+ this . _logger . debug ( "[" + user . username + "] query gave " + arr . length + " results " + JSON . stringify ( query ) ) ;
127132 return arr ;
128133 }
129134 /**
@@ -346,6 +351,10 @@ export class DatabaseConnection {
346351 }
347352 }
348353 }
354+ if ( q . item . _acl === null || q . item . _acl === undefined ) {
355+ q . item . _acl = original . _acl ;
356+ q . item . _version = original . _version ;
357+ }
349358 q . item = this . ensureResource ( q . item ) ;
350359 this . traversejsonencode ( q . item ) ;
351360 q . item = this . encryptentity < T > ( q . item ) ;
@@ -507,6 +516,7 @@ export class DatabaseConnection {
507516 query = { _id : q . item . _id } ;
508517 }
509518 var exists = await this . query ( query , { name : 1 } , 2 , 0 , null , q . collectionname , q . jwt ) ;
519+ console . log ( JSON . stringify ( query , null , 2 ) ) ;
510520 if ( exists . length == 1 ) {
511521 q . item . _id = exists [ 0 ] . _id ;
512522 }
@@ -515,13 +525,17 @@ export class DatabaseConnection {
515525 }
516526 var user : TokenUser = Crypt . verityToken ( q . jwt ) ;
517527 if ( ! this . hasAuthorization ( user , q . item , "update" ) ) { throw new Error ( "Access denied" ) ; }
518- if ( q . item . _id !== null && q . item . _id !== undefined && q . item . _id !== "" ) {
528+ // if (q.item._id !== null && q.item._id !== undefined && q.item._id !== "") {
529+ if ( exists . length == 1 ) {
530+ this . _logger . debug ( "[" + user . username + "] InsertOrUpdateOne, Updating found one in database" ) ;
519531 var uq = new UpdateOneMessage < T > ( ) ;
520- uq . query = query ; uq . item = q . item ; uq . collectionname = q . collectionname ; uq . w = q . w ; uq . j ; uq . jwt = q . jwt ;
532+ // uq.query = query;
533+ uq . item = q . item ; uq . collectionname = q . collectionname ; uq . w = q . w ; uq . j ; uq . jwt = q . jwt ;
521534 uq = await this . UpdateOne ( uq ) ;
522535 q . opresult = uq . opresult ;
523536 q . result = uq . result ;
524537 } else {
538+ this . _logger . debug ( "[" + user . username + "] InsertOrUpdateOne, Inserting as new in database" ) ;
525539 q . result = await this . InsertOne ( q . item , q . collectionname , q . w , q . j , q . jwt ) ;
526540 }
527541 return q ;
@@ -803,10 +817,13 @@ export class DatabaseConnection {
803817 _skip_array . forEach ( x => skip_array . push ( x . trim ( ) ) ) ;
804818 if ( skip_array . indexOf ( q . collectionname ) > - 1 ) { return 0 ; }
805819 var res = await this . query < T > ( q . query , null , 1 , 0 , null , q . collectionname , q . jwt ) ;
820+ var name : string = "unknown" ;
821+ var _id : string = "" ;
806822 if ( res . length > 0 ) {
807823 var _version = 1 ;
808824 var original = res [ 0 ] ;
809-
825+ name = original . name ;
826+ _id = original . _id ;
810827 delete original . _modifiedby ;
811828 delete original . _modifiedbyid ;
812829 delete original . _modified ;
@@ -821,8 +838,8 @@ export class DatabaseConnection {
821838 _created : new Date ( new Date ( ) . toISOString ( ) ) ,
822839 _createdby : user . name ,
823840 _createdbyid : user . _id ,
824- name : original . name ,
825- id : original . _id ,
841+ name : name ,
842+ id : _id ,
826843 update : q . item ,
827844 _version : _version ,
828845 reason : ""
0 commit comments