Skip to content

Commit

Permalink
feature: adds optional flag to filter out sould out products
Browse files Browse the repository at this point in the history
  • Loading branch information
willopez committed Apr 5, 2019
1 parent 0ecf20c commit abb3292
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ import ReactionError from "@reactioncommerce/reaction-error";
* @param {String[]} [params.tags] - Tag IDs to include (OR)
* @return {Promise<MongoCursor>} - A MongoDB cursor for the proper query
*/
export default async function catalogItems(context, { shopIds, tagIds } = {}) {
export default async function catalogItems(context, { isSoldOut, shopIds, tagIds } = {}) {
const { collections } = context;
const { Catalog } = collections;

if ((!shopIds || shopIds.length === 0) && (!tagIds || tagIds.length === 0)) {
throw new ReactionError("invalid-param", "You must provide tagIds or shopIds or both");
}

// If isSoldOut filter is provided, add it to the query
let isSoldOutFilter = {};
if (isSoldOut === true || isSoldOut === false) {
isSoldOutFilter = { "product.isSoldOut": { $eq: isSoldOut } };
}

const query = {
"product.isDeleted": { $ne: true },
...isSoldOutFilter,
"product.isVisible": true
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@ import ReactionError from "@reactioncommerce/reaction-error";
* @param {String} tagId - Tag ID
* @return {Promise<MongoCursor>} - A MongoDB cursor for the proper query
*/
export default async function catalogItemsAggregate(context, { shopIds, tagId } = {}) {
export default async function catalogItemsAggregate(context, { isSoldOut, 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");
}

// If isSoldOut filter is provided, add it to the query
let isSoldOutFilter = {};
if (isSoldOut === true || isSoldOut === false) {
isSoldOutFilter = { "product.isSoldOut": { $eq: isSoldOut } };
}

// Match all products that belong to a single tag
const match = {
$match: {
"shopId": { $in: shopIds },
"product.tagIds": tagId,
"product.isDeleted": { $ne: true },
...isSoldOutFilter,
"product.isVisible": true
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import ReactionError from "@reactioncommerce/reaction-error";
* @return {Promise<Object>} A CatalogItemConnection object
*/
export default async function catalogItems(_, args, context) {
const { shopIds: opaqueShopIds, tagIds: opaqueTagIds, ...connectionArgs } = args;
const { shopIds: opaqueShopIds, tagIds: opaqueTagIds, isSoldOut, ...connectionArgs } = args;

const shopIds = opaqueShopIds && opaqueShopIds.map(decodeShopOpaqueId);
const tagIds = opaqueTagIds && opaqueTagIds.map(decodeTagOpaqueId);
Expand All @@ -30,6 +30,7 @@ export default async function catalogItems(_, args, context) {
}
const tagId = tagIds[0];
const aggregationParams = await context.queries.catalogItemsAggregate(context, {
isSoldOut,
shopIds,
tagId
});
Expand All @@ -44,6 +45,7 @@ export default async function catalogItems(_, args, context) {
}

const query = await context.queries.catalogItems(context, {
isSoldOut,
shopIds,
tagIds
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ extend type Query {
"Optionally provide a list of tag IDs to further filter the item list"
tagIds: [ID],

"Optionally provide a flag to filter out sold out products"
isSoldOut: Boolean,

after: ConnectionCursor,
before: ConnectionCursor,
first: ConnectionLimitInt,
Expand Down

0 comments on commit abb3292

Please sign in to comment.