Преглед на файлове

fix(core): Persist custom fields when creating new ProductVariantPrice (#4184)

Michael Bromley преди 1 ден
родител
ревизия
6f6396574c
променени са 2 файла, в които са добавени 82 реда и са изтрити 1 реда
  1. 73 1
      packages/core/e2e/custom-fields.e2e-spec.ts
  2. 9 0
      packages/core/src/service/services/product-variant.service.ts

+ 73 - 1
packages/core/e2e/custom-fields.e2e-spec.ts

@@ -1,4 +1,4 @@
-import { LanguageCode } from '@vendure/common/lib/generated-types';
+import { CurrencyCode, LanguageCode } from '@vendure/common/lib/generated-types';
 import {
     Asset,
     CustomFields,
@@ -1225,6 +1225,78 @@ describe('Custom fields', () => {
         ]);
     });
 
+    // https://github.com/vendurehq/vendure/issues/3909
+    it('persists custom fields when creating a new ProductVariantPrice', async () => {
+        // First, add EUR to the channel's available currencies
+        await adminClient.query(
+            gql`
+                mutation UpdateChannel($input: UpdateChannelInput!) {
+                    updateChannel(input: $input) {
+                        ... on Channel {
+                            id
+                            availableCurrencyCodes
+                        }
+                    }
+                }
+            `,
+            {
+                input: {
+                    id: 'T_1',
+                    availableCurrencyCodes: [CurrencyCode.USD, CurrencyCode.EUR],
+                },
+            },
+        );
+
+        // Now create a new price in EUR with custom fields
+        const { updateProductVariants } = await adminClient.query(
+            gql`
+                mutation UpdateProductVariants($input: [UpdateProductVariantInput!]!) {
+                    updateProductVariants(input: $input) {
+                        id
+                        prices {
+                            currencyCode
+                            price
+                            customFields {
+                                costPrice
+                            }
+                        }
+                    }
+                }
+            `,
+            {
+                input: [
+                    {
+                        id: 'T_1',
+                        prices: [
+                            {
+                                price: 129900,
+                                currencyCode: 'USD',
+                                customFields: {
+                                    costPrice: 100,
+                                },
+                            },
+                            {
+                                price: 99900,
+                                currencyCode: 'EUR',
+                                customFields: {
+                                    costPrice: 200,
+                                },
+                            },
+                        ],
+                    },
+                ],
+            },
+        );
+
+        expect(updateProductVariants[0].prices).toContainEqual({
+            currencyCode: 'EUR',
+            price: 99900,
+            customFields: {
+                costPrice: 200,
+            },
+        });
+    });
+
     describe('setting custom fields directly via a service method', () => {
         it('OrderService.addItemToOrder warns on unknown custom field', async () => {
             const orderService = server.app.get(OrderService);

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

@@ -603,8 +603,17 @@ export class ProductVariantService {
                     price,
                     variant: new ProductVariant({ id: productVariantId }),
                     currencyCode: currencyCode ?? ctx.channel.defaultCurrencyCode,
+                    customFields,
                 }),
             );
+            if (customFields) {
+                await this.customFieldRelationService.updateRelations(
+                    ctx,
+                    ProductVariantPrice,
+                    customFields,
+                    createdPrice,
+                );
+            }
             await this.eventBus.publish(new ProductVariantPriceEvent(ctx, [createdPrice], 'created'));
             additionalPricesToUpdate = await productVariantPriceUpdateStrategy.onPriceCreated(
                 ctx,