Преглед изворни кода

feat(server): Create resolver for ProductVariant entity

Michael Bromley пре 7 година
родитељ
комит
c7c53f9d1c

+ 2 - 0
server/src/api/api.module.ts

@@ -40,6 +40,7 @@ import { OrderLineEntityResolver } from './resolvers/entity/order-line-entity.re
 import { ProductCategoryEntityResolver } from './resolvers/entity/product-category-entity.resolver';
 import { ProductEntityResolver } from './resolvers/entity/product-entity.resolver';
 import { ProductOptionGroupEntityResolver } from './resolvers/entity/product-option-group-entity.resolver';
+import { ProductVariantEntityResolver } from './resolvers/entity/product-variant-entity.resolver';
 import { ShopAuthResolver } from './resolvers/shop/shop-auth.resolver';
 import { ShopCustomerResolver } from './resolvers/shop/shop-customer.resolver';
 import { ShopOrderResolver } from './resolvers/shop/shop-order.resolver';
@@ -79,6 +80,7 @@ const entityResolvers = [
     ProductCategoryEntityResolver,
     ProductEntityResolver,
     ProductOptionGroupEntityResolver,
+    ProductVariantEntityResolver,
 ];
 
 @Module({

+ 31 - 0
server/src/api/resolvers/entity/product-variant-entity.resolver.ts

@@ -0,0 +1,31 @@
+import { Parent, ResolveProperty, Resolver } from '@nestjs/graphql';
+import { Option } from 'commander';
+
+import { Translated } from '../../../common/types/locale-types';
+import { ProductOption } from '../../../entity';
+import { ProductVariant } from '../../../entity/product-variant/product-variant.entity';
+import { ProductOptionService } from '../../../service';
+import { ProductVariantService } from '../../../service/services/product-variant.service';
+import { IdCodecService } from '../../common/id-codec.service';
+import { RequestContext } from '../../common/request-context';
+import { Ctx } from '../../decorators/request-context.decorator';
+
+@Resolver('ProductVariant')
+export class ProductVariantEntityResolver {
+    constructor(
+        private productVariantService: ProductVariantService,
+        private idCodecService: IdCodecService,
+    ) {}
+
+    @ResolveProperty()
+    async options(
+        @Ctx() ctx: RequestContext,
+        @Parent() productVariant: ProductVariant,
+    ): Promise<Array<Translated<ProductOption>>> {
+        if (productVariant.options) {
+            return productVariant.options as Array<Translated<ProductOption>>;
+        }
+        const productId = this.idCodecService.decode(productVariant.id);
+        return this.productVariantService.getOptionsForVariant(ctx, productId);
+    }
+}

+ 7 - 0
server/src/service/services/product-variant.service.ts

@@ -85,6 +85,13 @@ export class ProductVariantService {
             );
     }
 
+    getOptionsForVariant(ctx: RequestContext, variantId: ID): Promise<Array<Translated<ProductOption>>> {
+        return this.connection
+            .getRepository(ProductVariant)
+            .findOne(variantId, { relations: ['options'] })
+            .then(variant => (!variant ? [] : variant.options.map(o => translateDeep(o, ctx.languageCode))));
+    }
+
     async create(
         ctx: RequestContext,
         product: Product,