Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Search
- * @param {string} filter Filter
- * @param {Function(Error, object)} callback
- */
- Production.search = function (filter, callback) {
- let collection = Production.getDataSource().connector.collection('production');
- let search = _.get(filter, 'search', '');
- let searchRegex = new RegExp(search, 'i');
- let limit = _.get(filter, 'limit', 500);
- let skip = _.get(filter, 'skip', 0);
- // search able for relations
- //Client
- //Prod Type
- //Show,
- //Title
- let query = [];
- let relations = _.get(Production, 'definition.settings.relations');
- let include = _.get(Production, 'definition.settings.scope.include', []);
- let mainQuery = [
- // query relation:client
- {
- $lookup: {
- localField: "clientId",
- from: 'client',
- foreignField: '_id',
- as: 'client'
- }
- },
- {
- $unwind: {
- path: "$client",
- preserveNullAndEmptyArrays: true
- }
- },
- // query relation:client_contact
- {
- $lookup: {
- localField: "client._id",
- from: 'client_contact',
- foreignField: 'clientId',
- as: 'client_contact'
- }
- },
- {
- $unwind: {
- path: "$client_contact",
- preserveNullAndEmptyArrays: true
- }
- },
- // query relation: contact
- {
- $lookup: {
- from: "contact",
- localField: "client_contact.contactId",
- foreignField: "_id",
- as: "contacts",
- }
- },
- {
- $unwind: {
- path: "$contacts",
- preserveNullAndEmptyArrays: true
- }
- },
- // query relation production type
- {
- $lookup: {
- from: "production_type",
- localField: '_id',
- foreignField: 'productionId',
- as: 'production_type'
- }
- },
- {
- $unwind: {
- path: "$production_type",
- preserveNullAndEmptyArrays: true
- }
- },
- // let query relation type
- {
- $lookup: {
- from: "type",
- localField: "production_type.typeId",
- foreignField: "_id",
- as: "types",
- }
- },
- {
- $unwind: {
- path: "$types",
- preserveNullAndEmptyArrays: true
- }
- },
- // query status production
- {
- $lookup: {
- from: "status",
- localField: "statusId",
- foreignField: "_id",
- as: "status",
- }
- },
- {
- $unwind: {
- path: "$status",
- preserveNullAndEmptyArrays: true
- }
- },
- {
- $match: {
- $or: [
- {'show': {$regex: searchRegex}},
- {'title': {$regex: searchRegex}},
- {"client.firstName": {$regex: searchRegex}},
- {"client.lastName": {$regex: searchRegex}},
- {"contacts.email": {$regex: searchRegex}},
- {"contacts.phone": {$regex: searchRegex}},
- {"contacts.address": {$regex: searchRegex}},
- {"contacts.firstName": {$regex: searchRegex}},
- {"contacts.lastName": {$regex: searchRegex}},
- {"client.company": {$regex: searchRegex}},
- {"types.name": {$regex: searchRegex}},
- {"types.shortName": {$regex: searchRegex}},
- {"status.name": {$regex: searchRegex}},
- ]
- }
- },
- ];
- let orders = _.get(filter, 'order', []);
- let sortQuery = [
- // {$sort: {'client.company': -1}},
- ];
- _.each(orders, (order) => {
- let orderArr = _.split(order, ' ');
- if (orderArr && orderArr[0] && (orderArr[1] === "ASC" || orderArr[1] === 'DESC')) {
- let newSort = {};
- let orderKey = orderArr[0];
- let orderValue = orderArr[1];
- newSort[orderKey] = orderValue === 'DESC' ? -1 : 1;
- sortQuery.push({
- $sort: newSort
- });
- }
- });
- let groupQuery = [{
- $group: {
- _id: "$_id",
- show: {$first: "$show"},
- title: {$first: "$title"},
- revenue: {$first: "$revenue"},
- client: {$first: "$client"},
- types: {$first: "$types"},
- status: {$first: "$status"},
- createdAt: {$first: "$createdAt"},
- updatedAt: {$first: "$updatedAt"}
- }
- }];
- query = query.concat(mainQuery);
- query = query.concat(sortQuery);
- query = query.concat(groupQuery);
- query = query.concat(sortQuery);
- query = query.concat([{$limit: limit + skip}, {$skip: skip}]);
- collection.aggregate(query, {allowDiskUse: true}, function (err, result) {
- if (err) {
- return callback(err);
- }
- // return callback(err, result);
- let idObjects = {};
- let items = [];
- _.each(result, (item) => {
- let productionId = _.get(item, '_id', null);
- if (productionId) {
- items.push(productionId);
- idObjects = _.setWith(idObjects, productionId, true);
- }
- });
- //return callback(err, _.values(idObjects));
- let findQuery = {where: {id: {inq: items}}};
- findQuery.limit = 10000;
- findQuery.skip = 0;
- if (filter && filter.order) {
- // findQuery.order = _.get(filter, 'order');
- }
- Production.find(findQuery, function (err, productions) {
- if (err) {
- return callback(err);
- } else {
- let jsonClients = JSON.parse(JSON.stringify(productions));
- _.each(jsonClients, (production) => {
- idObjects[production.id] = production;
- });
- return callback(null, _.values(idObjects));
- }
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment