Browse Source

refactor(core): Add property resolver for Product.optionGroups

Michael Bromley 6 years ago
parent
commit
c06d77d132

+ 5 - 5
packages/core/e2e/__snapshots__/import.e2e-spec.ts.snap

@@ -350,16 +350,16 @@ Array [
     "id": "T_4",
     "id": "T_4",
     "name": "Artists Smock",
     "name": "Artists Smock",
     "optionGroups": Array [
     "optionGroups": Array [
-      Object {
-        "code": "artists-smock-colour",
-        "id": "T_4",
-        "name": "colour",
-      },
       Object {
       Object {
         "code": "artists-smock-size",
         "code": "artists-smock-size",
         "id": "T_3",
         "id": "T_3",
         "name": "size",
         "name": "size",
       },
       },
+      Object {
+        "code": "artists-smock-colour",
+        "id": "T_4",
+        "name": "colour",
+      },
     ],
     ],
     "slug": "artists-smock",
     "slug": "artists-smock",
     "variants": Array [
     "variants": Array [

+ 11 - 0
packages/core/src/api/resolvers/entity/product-entity.resolver.ts

@@ -2,9 +2,11 @@ import { Parent, ResolveProperty, Resolver } from '@nestjs/graphql';
 
 
 import { Translated } from '../../../common/types/locale-types';
 import { Translated } from '../../../common/types/locale-types';
 import { Collection } from '../../../entity/collection/collection.entity';
 import { Collection } from '../../../entity/collection/collection.entity';
+import { ProductOptionGroup } from '../../../entity/product-option-group/product-option-group.entity';
 import { ProductVariant } from '../../../entity/product-variant/product-variant.entity';
 import { ProductVariant } from '../../../entity/product-variant/product-variant.entity';
 import { Product } from '../../../entity/product/product.entity';
 import { Product } from '../../../entity/product/product.entity';
 import { CollectionService } from '../../../service/services/collection.service';
 import { CollectionService } from '../../../service/services/collection.service';
+import { ProductOptionGroupService } from '../../../service/services/product-option-group.service';
 import { ProductVariantService } from '../../../service/services/product-variant.service';
 import { ProductVariantService } from '../../../service/services/product-variant.service';
 import { ApiType } from '../../common/get-api-type';
 import { ApiType } from '../../common/get-api-type';
 import { RequestContext } from '../../common/request-context';
 import { RequestContext } from '../../common/request-context';
@@ -16,6 +18,7 @@ export class ProductEntityResolver {
     constructor(
     constructor(
         private productVariantService: ProductVariantService,
         private productVariantService: ProductVariantService,
         private collectionService: CollectionService,
         private collectionService: CollectionService,
+        private productOptionGroupService: ProductOptionGroupService,
     ) {}
     ) {}
 
 
     @ResolveProperty()
     @ResolveProperty()
@@ -36,4 +39,12 @@ export class ProductEntityResolver {
     ): Promise<Array<Translated<Collection>>> {
     ): Promise<Array<Translated<Collection>>> {
         return this.collectionService.getCollectionsByProductId(ctx, product.id, apiType === 'shop');
         return this.collectionService.getCollectionsByProductId(ctx, product.id, apiType === 'shop');
     }
     }
+
+    @ResolveProperty()
+    async optionGroups(
+        @Ctx() ctx: RequestContext,
+        @Parent() product: Product,
+    ): Promise<Array<Translated<ProductOptionGroup>>> {
+        return this.productOptionGroupService.getOptionGroupsByProductId(ctx, product.id);
+    }
 }
 }

+ 14 - 0
packages/core/src/service/services/product-option-group.service.ts

@@ -41,6 +41,20 @@ export class ProductOptionGroupService {
             .then(group => group && translateDeep(group, ctx.languageCode, ['options']));
             .then(group => group && translateDeep(group, ctx.languageCode, ['options']));
     }
     }
 
 
+    getOptionGroupsByProductId(ctx: RequestContext, id: ID): Promise<Array<Translated<ProductOptionGroup>>> {
+        return this.connection.manager
+            .find(ProductOptionGroup, {
+                relations: ['options'],
+                where: {
+                    product: { id },
+                },
+                order: {
+                    id: 'ASC',
+                },
+            })
+            .then(groups => groups.map(group => translateDeep(group, ctx.languageCode, ['options'])));
+    }
+
     async create(ctx: RequestContext, input: CreateProductOptionGroupInput): Promise<Translated<ProductOptionGroup>> {
     async create(ctx: RequestContext, input: CreateProductOptionGroupInput): Promise<Translated<ProductOptionGroup>> {
         const group = await this.translatableSaver.create({
         const group = await this.translatableSaver.create({
             input,
             input,

+ 0 - 3
packages/core/src/service/services/product.service.ts

@@ -37,7 +37,6 @@ export class ProductService {
     private readonly relations = [
     private readonly relations = [
         'featuredAsset',
         'featuredAsset',
         'assets',
         'assets',
-        'optionGroups',
         'channels',
         'channels',
         'facetValues',
         'facetValues',
         'facetValues.facet',
         'facetValues.facet',
@@ -70,7 +69,6 @@ export class ProductService {
             .then(async ([products, totalItems]) => {
             .then(async ([products, totalItems]) => {
                 const items = products.map(product =>
                 const items = products.map(product =>
                     translateDeep(product, ctx.languageCode, [
                     translateDeep(product, ctx.languageCode, [
-                        'optionGroups',
                         'facetValues',
                         'facetValues',
                         ['facetValues', 'facet'],
                         ['facetValues', 'facet'],
                     ]),
                     ]),
@@ -93,7 +91,6 @@ export class ProductService {
             return;
             return;
         }
         }
         return translateDeep(product, ctx.languageCode, [
         return translateDeep(product, ctx.languageCode, [
-            'optionGroups',
             'facetValues',
             'facetValues',
             ['facetValues', 'facet'],
             ['facetValues', 'facet'],
         ]);
         ]);