@@ -320,6 +320,43 @@ func (mg *Mongo) GetDocumentByID(auth model.Auth, dbName, col, id string) (map[s
320
320
return result , nil
321
321
}
322
322
323
+ func (mg * Mongo ) GetDocumentsByIDs (auth model.Auth , dbName , col string , ids []string ) (docs []map [string ]interface {}, err error ) {
324
+ db := mg .Client .Database (dbName )
325
+
326
+ var oids []primitive.ObjectID
327
+
328
+ for _ , id := range ids {
329
+ oid , err := primitive .ObjectIDFromHex (id )
330
+ if err != nil {
331
+ return []map [string ]interface {}{}, err
332
+ }
333
+ oids = append (oids , oid )
334
+ }
335
+
336
+ acctID , userID , err := parseObjectID (auth )
337
+ if err != nil {
338
+ return []map [string ]interface {}{}, err
339
+ }
340
+
341
+ filter := bson.M {FieldID : bson.D {{"$in" , oids }}}
342
+
343
+ secureRead (acctID , userID , auth .Role , col , filter )
344
+
345
+ cur , err := db .Collection (model .CleanCollectionName (col )).Find (mg .Ctx , filter )
346
+ if err != nil {
347
+ return docs , err
348
+ }
349
+ for cur .Next (mg .Ctx ) {
350
+ var v map [string ]interface {}
351
+ if err := cur .Decode (& v ); err != nil {
352
+ return []map [string ]interface {}{}, err
353
+ }
354
+ cleanMap (v )
355
+ docs = append (docs , v )
356
+ }
357
+ return
358
+ }
359
+
323
360
func (mg * Mongo ) UpdateDocument (auth model.Auth , dbName , col , id string , doc map [string ]interface {}) (map [string ]interface {}, error ) {
324
361
db := mg .Client .Database (dbName )
325
362
@@ -377,6 +414,26 @@ func (mg *Mongo) UpdateDocuments(auth model.Auth, dbName, col string, filters ma
377
414
secureWrite (acctID , userID , auth .Role , col , filters )
378
415
removeNotEditableFields (updateFields )
379
416
417
+ var ids []string
418
+ findOpts := options .Find ().SetProjection (bson.D {{"id" , 1 }})
419
+ cur , err := db .Collection (model .CleanCollectionName (col )).Find (mg .Ctx , filters , findOpts )
420
+ if err != nil {
421
+ return 0 , err
422
+ }
423
+ for cur .Next (mg .Ctx ) {
424
+ var v map [string ]interface {}
425
+ if err := cur .Decode (& v ); err != nil {
426
+ mg .log .Error ().Err (err ).Msg ("" )
427
+ }
428
+ id , ok := v [FieldID ].(primitive.ObjectID )
429
+ if ok {
430
+ ids = append (ids , id .Hex ())
431
+ }
432
+ }
433
+ if len (ids ) == 0 {
434
+ return 0 , nil
435
+ }
436
+
380
437
newProps := bson.M {}
381
438
for k , v := range updateFields {
382
439
newProps [k ] = v
@@ -388,6 +445,16 @@ func (mg *Mongo) UpdateDocuments(auth model.Auth, dbName, col string, filters ma
388
445
if err != nil {
389
446
return 0 , err
390
447
}
448
+
449
+ go func () {
450
+ docs , err := mg .GetDocumentsByIDs (auth , dbName , col , ids )
451
+ if err != nil {
452
+ mg .log .Error ().Err (err ).Msgf ("the documents with ids=%#s are not received for publishDocument event" , ids )
453
+ }
454
+ for _ , doc := range docs {
455
+ mg .PublishDocument ("db-" + col , model .MsgTypeDBUpdated , doc )
456
+ }
457
+ }()
391
458
return res .ModifiedCount , err
392
459
}
393
460
0 commit comments