Browse Source

feat(server): Implement remove ProductOptionGroup

Michael Bromley 7 years ago
parent
commit
e25123a8c3

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

@@ -10,6 +10,8 @@ type Mutation {
     updateProduct(input: UpdateProductInput): Product!
     "Add an OptionGroup to a Product"
     addOptionGroupToProduct(productId: ID!, optionGroupId: ID!): Product!
+    "Remove an OptionGroup from a Product"
+    removeOptionGroupFromProduct(productId: ID!, optionGroupId: ID!): Product!
 }
 
 type ProductList implements PaginatedList {

+ 16 - 3
server/src/api/product/product.resolver.ts

@@ -47,13 +47,26 @@ export class ProductResolver {
     async updateProduct(_, args): Promise<Translated<Product>> {
         const { input } = args;
         const product = await this.productService.update(this.idCodecService.decode(input));
-        return this.idCodecService.decode(product);
+        return this.idCodecService.encode(product);
     }
 
     @Mutation()
     async addOptionGroupToProduct(_, args): Promise<Translated<Product>> {
         const { productId, optionGroupId } = args;
-        const product = await this.productService.addOptionGroupToProduct(productId, optionGroupId);
-        return this.idCodecService.decode(product);
+        const product = await this.productService.addOptionGroupToProduct(
+            this.idCodecService.decode(productId),
+            this.idCodecService.decode(optionGroupId),
+        );
+        return this.idCodecService.encode(product);
+    }
+
+    @Mutation()
+    async removeOptionGroupFromProduct(_, args): Promise<Translated<Product>> {
+        const { productId, optionGroupId } = args;
+        const product = await this.productService.removeOptionGroupFromProduct(
+            this.idCodecService.decode(productId),
+            this.idCodecService.decode(optionGroupId),
+        );
+        return this.idCodecService.encode(product);
     }
 }

+ 18 - 6
server/src/service/product.service.ts

@@ -93,12 +93,7 @@ export class ProductService {
     }
 
     async addOptionGroupToProduct(productId: ID, optionGroupId: ID): Promise<Translated<Product>> {
-        const product = await this.connection
-            .getRepository(Product)
-            .findOne(productId, { relations: ['optionGroups'] });
-        if (!product) {
-            throw new I18nError(`error.product-with-id-not-found`, { productId });
-        }
+        const product = await this.getProductWithOptionGroups(productId);
         const optionGroup = await this.connection.getRepository(ProductOptionGroup).findOne(optionGroupId);
         if (!optionGroup) {
             throw new I18nError(`error.option-group-with-id-not-found`, { optionGroupId });
@@ -111,7 +106,24 @@ export class ProductService {
         }
 
         await this.connection.manager.save(product);
+        return assertFound(this.findOne(productId, DEFAULT_LANGUAGE_CODE));
+    }
+
+    async removeOptionGroupFromProduct(productId: ID, optionGroupId: ID): Promise<Translated<Product>> {
+        const product = await this.getProductWithOptionGroups(productId);
+        product.optionGroups = product.optionGroups.filter(g => g.id !== optionGroupId);
 
+        await this.connection.manager.save(product);
         return assertFound(this.findOne(productId, DEFAULT_LANGUAGE_CODE));
     }
+
+    private async getProductWithOptionGroups(productId: ID): Promise<Product> {
+        const product = await this.connection
+            .getRepository(Product)
+            .findOne(productId, { relations: ['optionGroups'] });
+        if (!product) {
+            throw new I18nError(`error.product-with-id-not-found`, { productId });
+        }
+        return product;
+    }
 }