Browse Source

feat(server): generateVariantsForProduct accepts default price & sku

Michael Bromley 7 years ago
parent
commit
454f7249c2

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


+ 1 - 1
server/src/api/product/product.api.graphql

@@ -13,7 +13,7 @@ type Mutation {
     "Remove an OptionGroup from a Product"
     removeOptionGroupFromProduct(productId: ID!, optionGroupId: ID!): Product!
     "Create a set of ProductVariants based on the OptionGroups assigned to the given Product"
-    generateVariantsForProduct(productId: ID!): Product!
+    generateVariantsForProduct(productId: ID!, defaultPrice: Int, defaultSku: String): Product!
     "Update existing ProductVariants"
     updateProductVariants(input: [UpdateProductVariantInput!]!): [ProductVariant]!
 }

+ 2 - 2
server/src/api/product/product.resolver.ts

@@ -60,8 +60,8 @@ export class ProductResolver {
     @Mutation()
     @ApplyIdCodec()
     async generateVariantsForProduct(_, args): Promise<Translated<Product>> {
-        const { productId } = args;
-        await this.productVariantService.generateVariantsForProduct(productId);
+        const { productId, defaultPrice, defaultSku } = args;
+        await this.productVariantService.generateVariantsForProduct(productId, defaultPrice, defaultSku);
         return assertFound(this.productService.findOne(productId, DEFAULT_LANGUAGE_CODE));
     }
 

+ 7 - 3
server/src/service/product-variant.service.ts

@@ -49,7 +49,11 @@ export class ProductVariantService {
         return createdVariant;
     }
 
-    async generateVariantsForProduct(productId: ID): Promise<Array<Translated<ProductVariant>>> {
+    async generateVariantsForProduct(
+        productId: ID,
+        defaultPrice?: number,
+        defaultSku?: string,
+    ): Promise<Array<Translated<ProductVariant>>> {
         const product = await this.connection.getRepository(Product).findOne(productId, {
             relations: ['optionGroups', 'optionGroups.options'],
         });
@@ -66,8 +70,8 @@ export class ProductVariantService {
         const createVariants = optionCombinations.map(options => {
             const name = this.createVariantName(productName, options);
             return this.create(product, {
-                sku: 'sku-not-set',
-                price: 0,
+                sku: defaultSku || 'sku-not-set',
+                price: defaultPrice || 0,
                 optionCodes: options.map(o => o.code),
                 translations: [
                     {

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