|
| 1 | +/* |
| 2 | +
|
| 3 | +Copyright 2026 Adobe. All rights reserved. |
| 4 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 6 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 9 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 10 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 11 | +governing permissions and limitations under the License. |
| 12 | +
|
| 13 | +*/ |
| 14 | + |
| 15 | +const { requestSaaS } = require("./utils"); |
| 16 | +const { |
| 17 | + CategoriesQuery, |
| 18 | + CategoryTreeQuery, |
| 19 | + CategoryTreeBySlugsQuery, |
| 20 | +} = require("./queries"); |
| 21 | + |
| 22 | +const MAX_TREE_DEPTH = 3; |
| 23 | + |
| 24 | +/** |
| 25 | + * Checks whether category families are configured (not the [null] default). |
| 26 | + * |
| 27 | + * @param {Array} families - The categoryFamilies array from runtime config. |
| 28 | + * @returns {boolean} |
| 29 | + */ |
| 30 | +function hasFamilies(families) { |
| 31 | + return Array.isArray(families) && families.length > 0; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Resolves all category slugs belonging to the given ACO category families. |
| 36 | + * |
| 37 | + * Uses BFS traversal of the categoryTree API: |
| 38 | + * 1. Query each family's root categories and their immediate childrenSlugs. |
| 39 | + * 2. Query those children (with depth) to retrieve their descendants. |
| 40 | + * 3. Repeat until no unresolved childrenSlugs remain. |
| 41 | + * |
| 42 | + * Handles trees of arbitrary depth even when the API caps depth at |
| 43 | + * MAX_TREE_DEPTH per call — each iteration advances up to that many levels. |
| 44 | + * |
| 45 | + * @param {Object} context - Request context (config, logger, headers, etc.). |
| 46 | + * @param {string[]} families - ACO category family identifiers. |
| 47 | + * @returns {Promise<string[]>} Flat array of all unique category slugs. |
| 48 | + */ |
| 49 | +async function getCategorySlugsFromFamilies(context, families) { |
| 50 | + console.debug("Getting category slugs from families:", families); |
| 51 | + const allSlugs = new Set(); |
| 52 | + |
| 53 | + for (const family of families) { |
| 54 | + console.debug("Getting category slugs from family:", family); |
| 55 | + // Get root-level categories for this family |
| 56 | + const firstLevel = await requestSaaS( |
| 57 | + CategoryTreeQuery, |
| 58 | + "getCategoryTree", |
| 59 | + { family }, |
| 60 | + context, |
| 61 | + ); |
| 62 | + |
| 63 | + let pending = []; |
| 64 | + for (const cat of firstLevel.data.categoryTree) { |
| 65 | + allSlugs.add(cat.slug); |
| 66 | + pending.push(...(cat.childrenSlugs || [])); |
| 67 | + } |
| 68 | + |
| 69 | + // BFS: resolve children level by level until no new slugs remain |
| 70 | + while (pending.length) { |
| 71 | + // Mark pending as seen before querying to prevent re-processing |
| 72 | + for (const slug of pending) allSlugs.add(slug); |
| 73 | + |
| 74 | + const childrenRes = await requestSaaS( |
| 75 | + CategoryTreeBySlugsQuery, |
| 76 | + "getCategoryTreeBySlugs", |
| 77 | + { family, slugs: pending, depth: MAX_TREE_DEPTH }, |
| 78 | + context, |
| 79 | + ); |
| 80 | + |
| 81 | + // First pass: capture any descendant slugs included due to depth traversal |
| 82 | + for (const cat of childrenRes.data.categoryTree) { |
| 83 | + allSlugs.add(cat.slug); |
| 84 | + } |
| 85 | + |
| 86 | + // Second pass: collect only new childrenSlugs for next iteration |
| 87 | + pending = []; |
| 88 | + for (const cat of childrenRes.data.categoryTree) { |
| 89 | + for (const child of cat.childrenSlugs || []) { |
| 90 | + if (!allSlugs.has(child)) pending.push(child); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + console.debug("Category slugs resolved:", [...allSlugs]); |
| 96 | + |
| 97 | + return [...allSlugs]; |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Retrieves all ACCS categories grouped by level. |
| 102 | + * |
| 103 | + * Returns a sparse array indexed by category level so callers can iterate |
| 104 | + * shallowest levels first (used for the early-exit optimization when |
| 105 | + * fetching products by category). |
| 106 | + * |
| 107 | + * @param {Object} context - Request context (config, logger, headers, etc.). |
| 108 | + * @returns {Promise<string[][]>} Sparse array where index N holds urlPath strings at level N. |
| 109 | + */ |
| 110 | +async function getCategories(context) { |
| 111 | + const categoriesRes = await requestSaaS( |
| 112 | + CategoriesQuery, |
| 113 | + "getCategories", |
| 114 | + {}, |
| 115 | + context, |
| 116 | + ); |
| 117 | + const byLevel = []; |
| 118 | + for (const { urlPath, level } of categoriesRes.data.categories) { |
| 119 | + const idx = parseInt(level); |
| 120 | + byLevel[idx] = byLevel[idx] || []; |
| 121 | + byLevel[idx].push(urlPath); |
| 122 | + } |
| 123 | + return byLevel; |
| 124 | +} |
| 125 | + |
| 126 | +module.exports = { getCategorySlugsFromFamilies, getCategories, hasFamilies }; |
0 commit comments