Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default async function productsByTagId(context, params) {
const { connectionArgs, shopId, tagId } = params;
const { collections, userHasPermission } = context;
const { Products, Tags } = collections;
// Check for owner or admin permissions from the user before allowing the query
if (!userHasPermission(["owner", "admin", "tag/admin", "tag/edit"], shopId)) {
throw new ReactionError("access-denied", "User does not have permission");
}
return arrayJoinPlusRemainingQuery({
arrayFieldPath: "featuredProductIds",
collection: Tags,
connectionArgs,
joinCollection: Products,
joinFieldPath: "_id",
joinSelector: { hashtags: tagId, shopId },
joinSortOrder: "asc",
positionFieldName: "position",
selector: { _id: tagId },
sortByForRemainingDocs: "createdAt",
sortOrderForRemainingDocs: "asc"
});
}
export default async function productsByTagId(context, params) {
const { connectionArgs, shopId, tagId } = params;
const { checkPermissions, collections } = context;
const { Products, Tags } = collections;
// Check for owner or admin permissions from the user before allowing the query
await checkPermissions(["owner", "admin", "tag/admin", "tag/edit"], shopId);
return arrayJoinPlusRemainingQuery({
arrayFieldPath: "featuredProductIds",
collection: Tags,
connectionArgs,
joinCollection: Products,
joinFieldPath: "_id",
joinSelector: { hashtags: tagId, shopId },
joinSortOrder: "asc",
positionFieldName: "position",
selector: { _id: tagId },
sortByForRemainingDocs: "createdAt",
sortOrderForRemainingDocs: "asc"
});
}
const { Catalog, Tags } = collections;
if (!tagId) throw new ReactionError("invalid-param", "You must provide a tagId");
const selector = {
"product.tagIds": tagId,
"product.isDeleted": { $ne: true },
"product.isVisible": true,
...catalogBooleanFilters
};
if (shopIds && shopIds.length > 0) {
selector.shopId = { $in: shopIds };
}
return arrayJoinPlusRemainingQuery({
arrayFieldPath: "featuredProductIds",
collection: Tags,
connectionArgs,
joinCollection: Catalog,
joinFieldPath: "product.productId",
joinSelector: selector,
joinSortOrder: "asc",
positionFieldName: "position",
selector: { _id: tagId },
sortByForRemainingDocs: "createdAt",
sortOrderForRemainingDocs: "asc"
});
}