Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 43 additions & 44 deletions imports/plugins/core/core/server/publications/collections/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,59 @@ import { Counts } from "meteor/tmeasday:publish-counts";
import Reaction from "/imports/plugins/core/core/server/Reaction";

/**
* A shared way of creating a projection
* @summary A shared way of creating a projection
* @param {String} shopId - shopId to filter records by
* @param {Object} sort - An object containing a sort
* @param {Number} limit - An optional limit of how many records to return
* @param {Object} query - An optional query to match
* @param {Number} skip - An optional limit number of records to skip
* @returns {Array} An array of projection operators
* @private
*/
function createAggregate(shopId, sort = { createdAt: -1 }, limit = 0, query = {}, skip = 0) {
const aggregate = [{ $match: { "items.shopId": shopId, ...query } }];

if (sort) aggregate.push({ $sort: sort });
if (skip > 0) aggregate.push({ $skip: skip });
if (limit > 0) aggregate.push({ $limit: limit });

// NOTE: in Mongo 3.4 using the $in operator will be supported for projection filters
const aggregate = [
{ $match: { "items.shopId": shopId, ...query } },
{
$project: {
items: {
$filter: {
input: "$items",
as: "item",
cond: { $eq: ["$$item.shopId", shopId] }
}
},
billing: {
$filter: {
input: "$billing",
as: "billing",
cond: { $eq: ["$$billing.shopId", shopId] }
}
},
shipping: {
$filter: {
input: "$shipping",
as: "shipping",
cond: { $eq: ["$$shipping.shopId", shopId] }
}
},
cartId: 1,
shopId: 1, // workflow is still stored at the top level and used to showing status
workflow: 1,
discount: 1,
tax: 1,
email: 1,
createdAt: 1,
accountId: 1,
taxCalculationFailed: 1,
bypassAddressValidation: 1
}
},
{ $sort: sort },
{ $skip: skip }
];
aggregate.push({
$project: {
items: {
$filter: {
input: "$items",
as: "item",
cond: { $eq: ["$$item.shopId", shopId] }
}
},
billing: {
$filter: {
input: "$billing",
as: "billing",
cond: { $eq: ["$$billing.shopId", shopId] }
}
},
shipping: {
$filter: {
input: "$shipping",
as: "shipping",
cond: { $eq: ["$$shipping.shopId", shopId] }
}
},
cartId: 1,
shopId: 1, // workflow is still stored at the top level and used to showing status
workflow: 1,
discount: 1,
tax: 1,
email: 1,
createdAt: 1,
accountId: 1,
taxCalculationFailed: 1,
bypassAddressValidation: 1
}
});

if (limit > 0) {
aggregate.push({ $limit: limit });
}
return aggregate;
}

Expand Down