-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(catalogItemsAggregate): add catalogItemsAggregate to sort produc…
…ts by featuredProductIds list. Signed-off-by: Machiko Yasuda <[email protected]>
- Loading branch information
1 parent
9a9dfc5
commit 2071e12
Showing
4 changed files
with
75 additions
and
3 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
imports/plugins/core/catalog/server/no-meteor/queries/catalogItemsAggregate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import ReactionError from "@reactioncommerce/reaction-error"; | ||
|
||
/** | ||
* @name catalogItemsAggregate | ||
* @method | ||
* @memberof Catalog/NoMeteorQueries | ||
* @summary query the Catalog by shop ID and/or tag ID in featured product order | ||
* @param {Object} context - an object containing the per-request state | ||
* @param {Object} params - request parameters | ||
* @param {String[]} [params.shopIds] - Shop IDs to include | ||
* @param {String} tagId - Tag ID | ||
* @return {Promise<MongoCursor>} - A MongoDB cursor for the proper query | ||
*/ | ||
export default async function catalogItemsAggregate(context, { shopIds, tagId } = {}) { | ||
const { collections } = context; | ||
const { Catalog, Tags } = collections; | ||
|
||
if ((!shopIds || shopIds.length === 0) && (!tagId)) { | ||
throw new ReactionError("invalid-param", "You must provide a tagId or shopIds or both"); | ||
} | ||
|
||
const tag = await Tags.findOne({ | ||
_id: tagId | ||
}); | ||
|
||
if (!tag) { | ||
throw new ReactionError("not-found", "Tag not found"); | ||
} | ||
|
||
// Get array of featured products ids, in order | ||
const order = tag.featuredProductIds; | ||
|
||
// Add a new field "order" to each product with their order in the array | ||
const addFields = { | ||
$addFields: { | ||
"__order": { | ||
"$indexOfArray": [order, "$product._id"] | ||
} | ||
} | ||
}; | ||
|
||
// Projection: Add a featuredPosition by order | ||
const projection = { | ||
$project: { | ||
_id: 1, | ||
__order: 1, | ||
product: 1, | ||
featuredPosition: { | ||
$cond: { | ||
if: { $lt: [ "$__order", 0] }, | ||
then: { $add: [ { $abs: "$__order" }, order.length ]}, | ||
else: "$__order" | ||
} | ||
} | ||
} | ||
}; | ||
|
||
// Sort by featuredPosition | ||
const sort = { | ||
$sort: { | ||
"featuredPosition": 1 | ||
} | ||
}; | ||
|
||
return { | ||
collection: Catalog, | ||
pipeline: [addFields, projection, sort] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters