lrf402788946 4 years ago
parent
commit
55ea275989
1 changed files with 7 additions and 6 deletions
  1. 7 6
      app/service/users/person_room.js

+ 7 - 6
app/service/users/person_room.js

@@ -20,10 +20,8 @@ class Person_roomService extends CrudService {
       condition.$or = [{ p1_id: p_id }, { p2_id: p_id }];
       condition.$or = [{ p1_id: p_id }, { p2_id: p_id }];
     }
     }
     let data = await this.model.find(condition).skip(parseInt(skip)).limit(parseInt(limit));
     let data = await this.model.find(condition).skip(parseInt(skip)).limit(parseInt(limit));
-    // const notRead = await this.pc.find({ room_id: data.map(i => i._id), is_read: false, receiver_id: p_id });
-    console.log(data.map(i => ObjectId(i.id || i._id)));
     const aggQuery = [
     const aggQuery = [
-      { $match: { is_read: false, receiver_id: ObjectId(p_id) } },
+      { $match: { room_id: { $in: data.map(i => ObjectId(i._id)) }, is_read: false, receiver_id: ObjectId(p_id) } },
       {
       {
         $group: {
         $group: {
           _id: '$room_id',
           _id: '$room_id',
@@ -31,10 +29,13 @@ class Person_roomService extends CrudService {
         },
         },
       },
       },
     ];
     ];
-    console.log(aggQuery);
-    const r = await this.pc.aggregate(aggQuery);
-    console.log(r);
+    const notReads = await this.pc.aggregate(aggQuery);
     if (data.length > 0) data = JSON.parse(JSON.stringify(data));
     if (data.length > 0) data = JSON.parse(JSON.stringify(data));
+    data = data.map(i => {
+      const nr = notReads.find(f => ObjectId(f._id).equals(i._id));
+      if (nr) i.not_read = nr.sum;
+      return i;
+    });
     const total = await this.model.count(condition);
     const total = await this.model.count(condition);
     return { data, total };
     return { data, total };
   }
   }