tabvn

Untitled

Sep 21st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. /**
  2. * Search
  3. * @param {string} filter Filter
  4. * @param {Function(Error, object)} callback
  5. */
  6.  
  7. Production.search = function (filter, callback) {
  8.  
  9. let collection = Production.getDataSource().connector.collection('production');
  10.  
  11. let search = _.get(filter, 'search', '');
  12.  
  13.  
  14. let searchRegex = new RegExp(search, 'i');
  15.  
  16. let limit = _.get(filter, 'limit', 500);
  17. let skip = _.get(filter, 'skip', 0);
  18.  
  19. // search able for relations
  20.  
  21. //Client
  22. //Prod Type
  23. //Show,
  24. //Title
  25.  
  26.  
  27. let query = [];
  28.  
  29.  
  30. let relations = _.get(Production, 'definition.settings.relations');
  31. let include = _.get(Production, 'definition.settings.scope.include', []);
  32.  
  33.  
  34. let mainQuery = [
  35. // query relation:client
  36. {
  37. $lookup: {
  38. localField: "clientId",
  39. from: 'client',
  40. foreignField: '_id',
  41. as: 'client'
  42. }
  43. },
  44. {
  45. $unwind: {
  46. path: "$client",
  47. preserveNullAndEmptyArrays: true
  48.  
  49. }
  50. },
  51. // query relation:client_contact
  52. {
  53. $lookup: {
  54. localField: "client._id",
  55. from: 'client_contact',
  56. foreignField: 'clientId',
  57. as: 'client_contact'
  58. }
  59. },
  60. {
  61. $unwind: {
  62. path: "$client_contact",
  63. preserveNullAndEmptyArrays: true
  64.  
  65. }
  66. },
  67.  
  68. // query relation: contact
  69. {
  70. $lookup: {
  71. from: "contact",
  72. localField: "client_contact.contactId",
  73. foreignField: "_id",
  74. as: "contacts",
  75. }
  76. },
  77.  
  78. {
  79. $unwind: {
  80. path: "$contacts",
  81. preserveNullAndEmptyArrays: true
  82.  
  83. }
  84. },
  85.  
  86. // query relation production type
  87. {
  88. $lookup: {
  89. from: "production_type",
  90. localField: '_id',
  91. foreignField: 'productionId',
  92. as: 'production_type'
  93. }
  94. },
  95. {
  96. $unwind: {
  97. path: "$production_type",
  98. preserveNullAndEmptyArrays: true
  99.  
  100. }
  101. },
  102. // let query relation type
  103. {
  104. $lookup: {
  105. from: "type",
  106. localField: "production_type.typeId",
  107. foreignField: "_id",
  108. as: "types",
  109. }
  110. },
  111. {
  112. $unwind: {
  113. path: "$types",
  114. preserveNullAndEmptyArrays: true
  115.  
  116. }
  117. },
  118.  
  119. // query status production
  120. {
  121. $lookup: {
  122. from: "status",
  123. localField: "statusId",
  124. foreignField: "_id",
  125. as: "status",
  126. }
  127. },
  128. {
  129. $unwind: {
  130. path: "$status",
  131. preserveNullAndEmptyArrays: true
  132.  
  133. }
  134. },
  135.  
  136.  
  137. {
  138. $match: {
  139.  
  140. $or: [
  141. {'show': {$regex: searchRegex}},
  142. {'title': {$regex: searchRegex}},
  143. {"client.firstName": {$regex: searchRegex}},
  144. {"client.lastName": {$regex: searchRegex}},
  145. {"contacts.email": {$regex: searchRegex}},
  146. {"contacts.phone": {$regex: searchRegex}},
  147. {"contacts.address": {$regex: searchRegex}},
  148. {"contacts.firstName": {$regex: searchRegex}},
  149. {"contacts.lastName": {$regex: searchRegex}},
  150. {"client.company": {$regex: searchRegex}},
  151. {"types.name": {$regex: searchRegex}},
  152. {"types.shortName": {$regex: searchRegex}},
  153. {"status.name": {$regex: searchRegex}},
  154. ]
  155. }
  156. },
  157.  
  158. ];
  159.  
  160.  
  161. let orders = _.get(filter, 'order', []);
  162.  
  163.  
  164. let sortQuery = [
  165. // {$sort: {'client.company': -1}},
  166. ];
  167.  
  168.  
  169. _.each(orders, (order) => {
  170.  
  171. let orderArr = _.split(order, ' ');
  172.  
  173. if (orderArr && orderArr[0] && (orderArr[1] === "ASC" || orderArr[1] === 'DESC')) {
  174. let newSort = {};
  175. let orderKey = orderArr[0];
  176. let orderValue = orderArr[1];
  177.  
  178. newSort[orderKey] = orderValue === 'DESC' ? -1 : 1;
  179.  
  180.  
  181. sortQuery.push({
  182. $sort: newSort
  183. });
  184. }
  185.  
  186.  
  187. });
  188.  
  189.  
  190. let groupQuery = [{
  191. $group: {
  192. _id: "$_id",
  193. show: {$first: "$show"},
  194. title: {$first: "$title"},
  195. revenue: {$first: "$revenue"},
  196. client: {$first: "$client"},
  197. types: {$first: "$types"},
  198. status: {$first: "$status"},
  199. createdAt: {$first: "$createdAt"},
  200. updatedAt: {$first: "$updatedAt"}
  201. }
  202. }];
  203.  
  204.  
  205. query = query.concat(mainQuery);
  206. query = query.concat(sortQuery);
  207. query = query.concat(groupQuery);
  208. query = query.concat(sortQuery);
  209. query = query.concat([{$limit: limit + skip}, {$skip: skip}]);
  210.  
  211.  
  212. collection.aggregate(query, {allowDiskUse: true}, function (err, result) {
  213.  
  214. if (err) {
  215. return callback(err);
  216. }
  217.  
  218. // return callback(err, result);
  219.  
  220. let idObjects = {};
  221.  
  222. let items = [];
  223.  
  224. _.each(result, (item) => {
  225. let productionId = _.get(item, '_id', null);
  226. if (productionId) {
  227. items.push(productionId);
  228. idObjects = _.setWith(idObjects, productionId, true);
  229. }
  230.  
  231. });
  232.  
  233.  
  234. //return callback(err, _.values(idObjects));
  235. let findQuery = {where: {id: {inq: items}}};
  236. findQuery.limit = 10000;
  237. findQuery.skip = 0;
  238. if (filter && filter.order) {
  239. // findQuery.order = _.get(filter, 'order');
  240. }
  241. Production.find(findQuery, function (err, productions) {
  242. if (err) {
  243. return callback(err);
  244. } else {
  245.  
  246. let jsonClients = JSON.parse(JSON.stringify(productions));
  247. _.each(jsonClients, (production) => {
  248. idObjects[production.id] = production;
  249. });
  250.  
  251.  
  252. return callback(null, _.values(idObjects));
  253. }
  254.  
  255. });
  256.  
  257.  
  258. });
  259.  
  260. };
Advertisement
Add Comment
Please, Sign In to add comment