Browse Source

feat(server): Implement updateProductVariants

Michael Bromley 7 years ago
parent
commit
5ea04d2033

File diff suppressed because it is too large
+ 0 - 0
schema.json


+ 2 - 0
server/src/api/product/product.api.graphql

@@ -12,6 +12,8 @@ type Mutation {
     addOptionGroupToProduct(productId: ID!, optionGroupId: ID!): Product!
     "Remove an OptionGroup from a Product"
     removeOptionGroupFromProduct(productId: ID!, optionGroupId: ID!): Product!
+    "Update existing ProductVariants"
+    updateProductVariants(input: [UpdateProductVariantInput!]!): [ProductVariant]!
 }
 
 type ProductList implements PaginatedList {

+ 7 - 0
server/src/api/product/product.resolver.ts

@@ -1,6 +1,7 @@
 import { Mutation, Query, Resolver } from '@nestjs/graphql';
 
 import { PaginatedList } from '../../../../shared/shared-types';
+import { ProductVariant } from '../../entity/product-variant/product-variant.entity';
 import { Product } from '../../entity/product/product.entity';
 import { Translated } from '../../locale/locale-types';
 import { ProductVariantService } from '../../service/product-variant.service';
@@ -61,4 +62,10 @@ export class ProductResolver {
         const { productId, optionGroupId } = args;
         return this.productService.removeOptionGroupFromProduct(productId, optionGroupId);
     }
+
+    @Mutation()
+    @ApplyIdCodec()
+    async updateProductVariants(_, args): Promise<Array<Translated<ProductVariant>>> {
+        return this.productService.updateProductVariants(args.input);
+    }
 }

+ 14 - 5
server/src/entity/product-variant/product-variant.graphql

@@ -1,10 +1,10 @@
 type ProductVariant implements Node {
     id: ID!
     languageCode: LanguageCode!
-    sku: String
-    name: String
+    sku: String!
+    name: String!
     image: String
-    price: Int
+    price: Int!
     options: [ProductOption!]!
     translations: [ProductVariantTranslation!]!
 }
@@ -15,15 +15,24 @@ type ProductVariantTranslation {
     name: String!
 }
 
-input CreateProductVariantTranslation {
+input ProductVariantTranslationInput {
+    id: ID
     languageCode: LanguageCode!
     name: String!
 }
 
 input CreateProductVariantInput {
-    translations: [CreateProductVariantTranslation]!
+    translations: [ProductVariantTranslationInput!]!
     sku: String!
     image: String
     price: Int!
     optionCodes: [String]
 }
+
+input UpdateProductVariantInput {
+    id: ID!
+    translations: [ProductVariantTranslationInput!]!
+    sku: String!
+    image: String
+    price: Int!
+}

+ 4 - 4
server/src/entity/product/product.graphql

@@ -1,10 +1,10 @@
 type Product implements Node {
     id: ID!
     languageCode: LanguageCode!
-    name: String
-    slug: String
-    description: String
-    image: String
+    name: String!
+    slug: String!
+    description: String!
+    image: String!
     variants: [ProductVariant!]!
     optionGroups: [ProductOptionGroup!]!
     translations: [ProductTranslation!]!

+ 14 - 0
server/src/service/product.service.ts

@@ -6,6 +6,7 @@ import { ID, PaginatedList } from '../../../shared/shared-types';
 import { DEFAULT_LANGUAGE_CODE } from '../common/constants';
 import { assertFound } from '../common/utils';
 import { ProductOptionGroup } from '../entity/product-option-group/product-option-group.entity';
+import { ProductVariant } from '../entity/product-variant/product-variant.entity';
 import { ProductTranslation } from '../entity/product/product-translation.entity';
 import { CreateProductDto, UpdateProductDto } from '../entity/product/product.dto';
 import { Product } from '../entity/product/product.entity';
@@ -92,6 +93,19 @@ export class ProductService {
         return assertFound(this.findOne(updateProductDto.id, DEFAULT_LANGUAGE_CODE));
     }
 
+    async updateProductVariants(updateProductVariants: any[]): Promise<Array<Translated<ProductVariant>>> {
+        for (const variant of updateProductVariants) {
+            await this.connection.getRepository(ProductVariant).update(variant.id, variant);
+        }
+
+        return await this.connection
+            .getRepository(ProductVariant)
+            .findByIds(updateProductVariants.map(v => v.id))
+            .then(variants => {
+                return variants.map(v => translateDeep(v, DEFAULT_LANGUAGE_CODE));
+            });
+    }
+
     async addOptionGroupToProduct(productId: ID, optionGroupId: ID): Promise<Translated<Product>> {
         const product = await this.getProductWithOptionGroups(productId);
         const optionGroup = await this.connection.getRepository(ProductOptionGroup).findOne(optionGroupId);

Some files were not shown because too many files changed in this diff