Advertisement
tabvn

Untitled

Jul 14th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. User.send_message = function (id, body, callback) {
  2.  
  3.     var error = new Error("Access denied.");
  4.     error.errorCode = 401;
  5.     var paticipants = [];
  6.  
  7.     if (typeof body.participants === 'undefined' || body.participants === null || !Array.isArray(body.participants) || !body.participants.length) {
  8.       return callback(error);
  9.     } else {
  10.  
  11.       var userIds = body.participants;
  12.       userIds.push(id);
  13.       User.find({where: {id: {inq: userIds}}}, function (err, users) {
  14.  
  15.         if (users && users.length && err === null) {
  16.           // found users
  17.           users.forEach(function (user) {
  18.             paticipants.push(user.id);
  19.           });
  20.  
  21.  
  22.           // now let do query to mongodb.
  23.  
  24.           var GroupCollection = User.getDataSource().connector.collection(User.app.models.group.modelName);
  25.           GroupCollection.aggregate([
  26.             {$match: {status: true}},
  27.             {$sort: {updatedAt: 1}},
  28.             {
  29.               $lookup: {localField: "_id", from: "member", foreignField: "groupId", as: "members"}
  30.             },
  31.             {
  32.               $group: {
  33.                 _id: "$_id",
  34.                 members: {$addToSet: "$members.userId"}
  35.               }
  36.             },
  37.             {
  38.               $project: {
  39.                 _id: 1,
  40.                 id: 1,
  41.                 title: 1,
  42.                 members: {$arrayElemAt: ["$members", 0]}
  43.               }
  44.             },
  45.             {
  46.               $match: {
  47.                 $and: [
  48.                   {members: {$all: paticipants}},
  49.                   {members: {$size: paticipants.length}}
  50.                 ]
  51.               }
  52.             },
  53.  
  54.           ], function (err, result) {
  55.  
  56.             var filterQuery = {
  57.               include: [
  58.                 {
  59.                   relation: "messages",
  60.                   scope: {
  61.                     order: ["createdAt DESC"],
  62.                     limit: 10
  63.                   }
  64.                 },
  65.                 {
  66.                   relation: "members",
  67.                 }
  68.               ]
  69.             };
  70.  
  71.  
  72.             if (err === null && result && result.length && typeof result[0]._id !== 'undefined' && result[0]._id !== null) {
  73.               // found item.
  74.  
  75.  
  76.  
  77.               // if have message in body we need create it
  78.  
  79.               if (typeof body.message !== 'undefined' && body.message !== null && typeof body.message.body !== 'undefined' && body.message.body !== null && body.message.body !== "") {
  80.                 User.app.models.message.create({
  81.                   userId: id,
  82.                   body: body.message.body,
  83.                   messageType: body.message.messageType ? body.message.messageType : "text",
  84.                   groupId: result[0]._id
  85.                 }, function (err, newMessage) {
  86.  
  87.                   if (err !== null) {
  88.                     return callback(err);
  89.                   } else {
  90.  
  91.                     User.app.models.group.findById(result[0]._id, filterQuery, function (err, group) {
  92.                       return callback(null, group);
  93.                     });
  94.                   }
  95.  
  96.  
  97.                 });
  98.  
  99.               } else {
  100.  
  101.                 User.app.models.group.findById(result[0]._id, filterQuery, function (err, group) {
  102.                   return callback(null, group);
  103.                 });
  104.  
  105.               }
  106.  
  107.  
  108.             } else {
  109.               // we need create new group. if message from body is found
  110.               if (typeof body.message !== 'undefined' && body.message !== null && typeof body.message.body !== 'undefined' && body.message.body !== null && body.message.body !== "") {
  111.  
  112.  
  113.                 User.app.models.group.create({
  114.                   userId: id
  115.                 }, function (err, group) {
  116.  
  117.                   if (err) {
  118.                     return callback(err);
  119.                   }
  120.                   if (group) {
  121.  
  122.                     var groupMemers = [];
  123.                     users.forEach(function (p) {
  124.                       if (p.id != id) {
  125.                         groupMemers.push({groupId: group.id, userId: p.id, name: p.firstName ? p.firstName : ""});
  126.                       }
  127.                     });
  128.  
  129.                     group.members.create(groupMemers, function (err, members) {
  130.  
  131.                       group.messages.create({
  132.                         userId: id,
  133.                         body: body.message.body,
  134.                         messageType: body.message.messageType ? body.message.messageType : "text",
  135.                         groupId: group.id
  136.                       }, function (err, message) {
  137.  
  138.  
  139.                         User.app.models.group.findById(group.id, filterQuery, function (err, group) {
  140.                           return callback(null, group);
  141.                         });
  142.  
  143.  
  144.  
  145.  
  146.  
  147.                       });
  148.  
  149.  
  150.                     });
  151.  
  152.  
  153.                   } else {
  154.                     return callback(error);
  155.                   }
  156.  
  157.                 });
  158.  
  159.               } else {
  160.  
  161.                 return callback(null, result); // this is empty group. and no message post in body.
  162.               }
  163.  
  164.  
  165.             }
  166.  
  167.           });
  168.  
  169.  
  170.         } else {
  171.           return callback(error);
  172.         }
  173.  
  174.       });
  175.  
  176.  
  177.     }
  178.  
  179.  
  180.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement