From 096851528aa3b5cb056d129e69a07eecbba73181 Mon Sep 17 00:00:00 2001 From: griest024 Date: Wed, 5 Jun 2024 15:22:18 -0400 Subject: [PATCH] feat(category): add injectable fragments and transform support to magento driver (#2828) --- .../driver/magento/src/category.service.ts | 6 +++-- .../magento/src/fragments/public_api.ts | 1 + .../driver/magento/src/fragments/token.ts | 26 ++++++++++++++++++ .../category/driver/magento/src/public_api.ts | 2 ++ .../src/queries/get-category-and-products.ts | 4 ++- .../category-transformer.service.ts | 16 ++++++++--- .../magento/src/transforms/public_api.ts | 2 ++ .../driver/magento/src/transforms/token.ts | 27 +++++++++++++++++++ .../driver/magento/src/transforms/type.ts | 10 +++++++ 9 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 libs/category/driver/magento/src/fragments/public_api.ts create mode 100644 libs/category/driver/magento/src/fragments/token.ts create mode 100644 libs/category/driver/magento/src/transforms/public_api.ts create mode 100644 libs/category/driver/magento/src/transforms/token.ts create mode 100644 libs/category/driver/magento/src/transforms/type.ts diff --git a/libs/category/driver/magento/src/category.service.ts b/libs/category/driver/magento/src/category.service.ts index f57b2aa2f9..92f9d37709 100644 --- a/libs/category/driver/magento/src/category.service.ts +++ b/libs/category/driver/magento/src/category.service.ts @@ -35,6 +35,7 @@ import { DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS, } from '@daffodil/product/driver/magento'; +import { MAGENTO_CATEGORY_EXTRA_FRAGMENTS } from './fragments/public_api'; import { MAGENTO_CATEGORY_CONFIG_TOKEN, DaffCategoryMagentoDriverConfig, @@ -81,6 +82,7 @@ export class DaffMagentoCategoryService implements DaffCategoryServiceInterface @Inject(MAGENTO_PRODUCT_CONFIG_TOKEN) private productConfig: DaffProductMagentoDriverConfig, @Inject(DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS) private extraProductFragments: DocumentNode[], @Inject(DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS) private extraProductPreviewFragments: DocumentNode[], + @Inject(MAGENTO_CATEGORY_EXTRA_FRAGMENTS) private extraCategoryFragments: DocumentNode[], ) {} get(categoryRequest: DaffCategoryIdRequest): Observable { @@ -89,7 +91,7 @@ export class DaffMagentoCategoryService implements DaffCategoryServiceInterface query: MagentoProductGetFilterTypes, }), this.apollo.query({ - query: MagentoGetCategoryAndProductsQuery([ + query: MagentoGetCategoryAndProductsQuery(this.extraCategoryFragments, [ ...this.extraProductFragments, ...this.extraProductPreviewFragments, ]), @@ -123,7 +125,7 @@ export class DaffMagentoCategoryService implements DaffCategoryServiceInterface category, filterTypes, ]) => this.apollo.query({ - query: MagentoGetCategoryAndProductsQuery([ + query: MagentoGetCategoryAndProductsQuery(this.extraCategoryFragments, [ ...this.extraProductFragments, ...this.extraProductPreviewFragments, ]), diff --git a/libs/category/driver/magento/src/fragments/public_api.ts b/libs/category/driver/magento/src/fragments/public_api.ts new file mode 100644 index 0000000000..6b36029d17 --- /dev/null +++ b/libs/category/driver/magento/src/fragments/public_api.ts @@ -0,0 +1 @@ +export * from './token'; diff --git a/libs/category/driver/magento/src/fragments/token.ts b/libs/category/driver/magento/src/fragments/token.ts new file mode 100644 index 0000000000..db7e3d1b98 --- /dev/null +++ b/libs/category/driver/magento/src/fragments/token.ts @@ -0,0 +1,26 @@ +import { DocumentNode } from '@apollo/client/core'; + +import { createMultiInjectionToken } from '@daffodil/core'; + + +export const { + /** + * A multi-provider injection token for providing category tree transform logic in the Magento driver. + * It is run after the standard transforms and passed both the current transformed Daffodil category and the Magento category. + */ + token: MAGENTO_CATEGORY_EXTRA_FRAGMENTS, + /** + * Provides category tree transforms for the Magento category driver. + * + * See {@link MAGENTO_CATEGORY_EXTRA_FRAGMENTS}. + * + * ```ts + * providers: [ + * ...magentoProvideCategoryExtraFragments( + * myCategoryExtraFragment + * ) + * ] + * ``` + */ + provider: magentoProvideCategoryExtraFragments, +} = createMultiInjectionToken('MAGENTO_CATEGORY_EXTRA_FRAGMENTS'); diff --git a/libs/category/driver/magento/src/public_api.ts b/libs/category/driver/magento/src/public_api.ts index 7de11480a6..d2660df60f 100644 --- a/libs/category/driver/magento/src/public_api.ts +++ b/libs/category/driver/magento/src/public_api.ts @@ -6,3 +6,5 @@ export * from './models/public_api'; export * from './queries/public_api'; export * from './transformers/public_api'; export * from './interfaces/public_api'; +export * from './transforms/public_api'; +export * from './fragments/public_api'; diff --git a/libs/category/driver/magento/src/queries/get-category-and-products.ts b/libs/category/driver/magento/src/queries/get-category-and-products.ts index cbe96a1df4..c2e9407afe 100644 --- a/libs/category/driver/magento/src/queries/get-category-and-products.ts +++ b/libs/category/driver/magento/src/queries/get-category-and-products.ts @@ -16,11 +16,12 @@ import { magentoCategoryTreeFragment } from './fragments/public_api'; export const DAFF_MAGENTO_GET_CATEGORY_AND_PRODUCTS_QUERY_NAME = 'MagentoGetCategoryAndProducts'; -export const MagentoGetCategoryAndProductsQuery = (extraProductFragments: DocumentNode[] = []) => gql` +export const MagentoGetCategoryAndProductsQuery = (extraCategoryFragments: DocumentNode[] = [], extraProductFragments: DocumentNode[] = []) => gql` query ${DAFF_MAGENTO_GET_CATEGORY_AND_PRODUCTS_QUERY_NAME}($categoryFilters: CategoryFilterInput, $productFilter: ProductAttributeFilterInput!, $search: String, $pageSize: Int, $currentPage: Int, $sort: ProductAttributeSortInput) { categoryList(filters: $categoryFilters) { ...categoryTree + ${daffBuildFragmentNameSpread(...extraCategoryFragments)} } products(filter: $productFilter, search: $search, pageSize: $pageSize, currentPage: $currentPage, sort: $sort) { @@ -45,5 +46,6 @@ ${magentoProductFragment} ${magentoSearchResultPageInfoFragment} ${magentoProductSortFieldsFragment} ${magentoProductAggregationsFragment} +${daffBuildFragmentDefinition(...extraCategoryFragments)} ${daffBuildFragmentDefinition(...extraProductFragments)} `; diff --git a/libs/category/driver/magento/src/transformers/category-transformer.service.ts b/libs/category/driver/magento/src/transformers/category-transformer.service.ts index b352c86cf8..247531dc5d 100644 --- a/libs/category/driver/magento/src/transformers/category-transformer.service.ts +++ b/libs/category/driver/magento/src/transformers/category-transformer.service.ts @@ -1,4 +1,7 @@ -import { Injectable } from '@angular/core'; +import { + Inject, + Injectable, +} from '@angular/core'; import { DaffCategory, @@ -10,14 +13,21 @@ import { MagentoBreadcrumb, MagentoCategory, } from '../models/public_api'; +import { + MAGENTO_CATEGORY_EXTRA_TRANSFORMS, + MagentoCategoryTreeTransform, +} from '../transforms/public_api'; @Injectable({ providedIn: 'root', }) export class DaffMagentoCategoryTransformerService { + constructor( + @Inject(MAGENTO_CATEGORY_EXTRA_TRANSFORMS) private extraTransforms: Array, + ) {} transform(category: MagentoCategory, products: MagentoProduct[]): DaffCategory { - return { + return this.extraTransforms.reduce((acc, transform) => transform(acc, category, products), { id: category.uid, url: `/${category.url_path}${category.url_suffix}`, canonicalUrl: category.canonical_url, @@ -31,7 +41,7 @@ export class DaffMagentoCategoryTransformerService { .sort((a, b) => a.level - b.level) || null, product_ids: products.map(product => product.sku), total_products: products.length, - }; + }); } private transformBreadcrumb(breadcrumb: MagentoBreadcrumb, category: MagentoCategory): DaffCategoryBreadcrumb { diff --git a/libs/category/driver/magento/src/transforms/public_api.ts b/libs/category/driver/magento/src/transforms/public_api.ts new file mode 100644 index 0000000000..452c6b52f9 --- /dev/null +++ b/libs/category/driver/magento/src/transforms/public_api.ts @@ -0,0 +1,2 @@ +export * from './token'; +export * from './type'; diff --git a/libs/category/driver/magento/src/transforms/token.ts b/libs/category/driver/magento/src/transforms/token.ts new file mode 100644 index 0000000000..018d14226f --- /dev/null +++ b/libs/category/driver/magento/src/transforms/token.ts @@ -0,0 +1,27 @@ +// workaround https://github.com/graycoreio/daffodil/issues/1667 +import { DaffCategory } from '@daffodil/category'; +import { createMultiInjectionToken } from '@daffodil/core'; + +import { MagentoCategoryTreeTransform } from './type'; + +export const { + /** + * A multi-provider injection token for providing category transform logic in the Magento driver. + * It is run after the standard transforms and passed both the current transformed Daffodil category and the Magento category. + */ + token: MAGENTO_CATEGORY_EXTRA_TRANSFORMS, + /** + * Provides category transforms for the Magento category driver. + * + * See {@link MAGENTO_CATEGORY_EXTRA_TRANSFORMS}. + * + * ```ts + * providers: [ + * ...magentoProvideCategoryExtraTransforms( + * myCategoryExtraTransform + * ) + * ] + * ``` + */ + provider: magentoProvideCategoryExtraTransforms, +} = createMultiInjectionToken('MAGENTO_CATEGORY_EXTRA_TRANSFORMS'); diff --git a/libs/category/driver/magento/src/transforms/type.ts b/libs/category/driver/magento/src/transforms/type.ts new file mode 100644 index 0000000000..b9ecfdce0b --- /dev/null +++ b/libs/category/driver/magento/src/transforms/type.ts @@ -0,0 +1,10 @@ +import { DaffCategory } from '@daffodil/category'; +import { MagentoProduct } from '@daffodil/product/driver/magento'; + +import { MagentoCategory } from '../models/public_api'; + +/** + * A transform for the Magento driver that can add extra fields or otherwise modify the category driver response. + */ +export type MagentoCategoryTreeTransform = + (daffCategory: DaffCategory, magentoCategory: T, products: MagentoProduct[]) => V;