Browse Source

fix(core): Fix regression in correctly setting OrderLine.featuredAsset

v3.0.2 introduced a bug which caused the OrderLine.featuredAsset not to
get set if the ProductVariant did not have a featuredAsset
Michael Bromley 1 year ago
parent
commit
7d070f2324

+ 1 - 1
packages/core/e2e/fixtures/e2e-products-full.csv

@@ -9,7 +9,7 @@ Gaming PC         ,gaming-pc         ,"This pc is optimised for gaming, and is a
                   ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,R7-2700|240GB SSD,CGS480VR1064,1099.95,standard   ,100        ,false         ,             ,
                   ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,i7-8700|120GB SSD,CGS480VR1065,931.20 ,standard   ,100        ,false         ,             ,
                   ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,R7-2700|120GB SSD,CGS480VR1066,949.20 ,standard   ,100        ,false         ,             ,
-Hard Drive        ,hard-drive        ,"Boost your PC storage with this internal hard drive, designed just for desktop and all-in-one PCs."                                                                                                                                                                                                                                                                                                                                                                                         ,vincent-botta-736919-unsplash.jpg ,category:electronics|category:computers,HDD capacity   ,1TB              ,IHD455T1    ,37.99  ,standard   ,100        ,false         ,             ,
+Hard Drive        ,hard-drive        ,"Boost your PC storage with this internal hard drive, designed just for desktop and all-in-one PCs."                                                                                                                                                                                                                                                                                                                                                                                         ,vincent-botta-736919-unsplash.jpg ,category:electronics|category:computers,HDD capacity   ,1TB              ,IHD455T1    ,37.99  ,standard   ,100        ,false         ,florian-olivo-1166419-unsplash.jpg,
                   ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,2TB              ,IHD455T2    ,53.74  ,standard   ,100        ,false         ,             ,
                   ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,3TB              ,IHD455T3    ,78.96  ,standard   ,100        ,false         ,             ,
                   ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,4TB              ,IHD455T4    ,92.99  ,standard   ,100        ,false         ,             ,

+ 19 - 0
packages/core/e2e/graphql/generated-e2e-shop-types.ts

@@ -3351,6 +3351,7 @@ export type UpdatedOrderFragment = {
         linePrice: number;
         linePriceWithTax: number;
         productVariant: { id: string };
+        featuredAsset?: { id: string } | null;
         discounts: Array<{
             adjustmentSource: string;
             amount: number;
@@ -3395,6 +3396,7 @@ export type AddItemToOrderMutation = {
                       linePrice: number;
                       linePriceWithTax: number;
                       productVariant: { id: string };
+                      featuredAsset?: { id: string } | null;
                       discounts: Array<{
                           adjustmentSource: string;
                           amount: number;
@@ -3429,6 +3431,7 @@ export type AddItemToOrderMutation = {
                   linePrice: number;
                   linePriceWithTax: number;
                   productVariant: { id: string };
+                  featuredAsset?: { id: string } | null;
                   discounts: Array<{
                       adjustmentSource: string;
                       amount: number;
@@ -4731,6 +4734,14 @@ export const UpdatedOrderFragmentDoc = {
                                 { kind: 'Field', name: { kind: 'Name', value: 'unitPriceWithTax' } },
                                 { kind: 'Field', name: { kind: 'Name', value: 'linePrice' } },
                                 { kind: 'Field', name: { kind: 'Name', value: 'linePriceWithTax' } },
+                                {
+                                    kind: 'Field',
+                                    name: { kind: 'Name', value: 'featuredAsset' },
+                                    selectionSet: {
+                                        kind: 'SelectionSet',
+                                        selections: [{ kind: 'Field', name: { kind: 'Name', value: 'id' } }],
+                                    },
+                                },
                                 {
                                     kind: 'Field',
                                     name: { kind: 'Name', value: 'discounts' },
@@ -5305,6 +5316,14 @@ export const AddItemToOrderDocument = {
                                 { kind: 'Field', name: { kind: 'Name', value: 'unitPriceWithTax' } },
                                 { kind: 'Field', name: { kind: 'Name', value: 'linePrice' } },
                                 { kind: 'Field', name: { kind: 'Name', value: 'linePriceWithTax' } },
+                                {
+                                    kind: 'Field',
+                                    name: { kind: 'Name', value: 'featuredAsset' },
+                                    selectionSet: {
+                                        kind: 'SelectionSet',
+                                        selections: [{ kind: 'Field', name: { kind: 'Name', value: 'id' } }],
+                                    },
+                                },
                                 {
                                     kind: 'Field',
                                     name: { kind: 'Name', value: 'discounts' },

+ 3 - 0
packages/core/e2e/graphql/shop-definitions.ts

@@ -87,6 +87,9 @@ export const UPDATED_ORDER_FRAGMENT = gql`
             unitPriceWithTax
             linePrice
             linePriceWithTax
+            featuredAsset {
+                id
+            }
             discounts {
                 adjustmentSource
                 amount

+ 54 - 0
packages/core/e2e/shop-order.e2e-spec.ts

@@ -8,6 +8,7 @@ import {
     mergeConfig,
 } from '@vendure/core';
 import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
+import { fail } from 'assert';
 import gql from 'graphql-tag';
 import path from 'path';
 import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
@@ -44,6 +45,7 @@ import {
     GET_COUNTRY_LIST,
     GET_CUSTOMER,
     GET_CUSTOMER_LIST,
+    GET_PRODUCT_WITH_VARIANTS,
     GET_SHIPPING_METHOD_LIST,
     UPDATE_COUNTRY,
     UPDATE_PRODUCT,
@@ -1283,6 +1285,58 @@ describe('Shop orders', () => {
             expect(billingAddress.defaultBillingAddress).toBe(true);
             expect(billingAddress.defaultShippingAddress).toBe(false);
         });
+
+        it('sets OrderLine.featuredAsset to that of ProductVariant if defined', async () => {
+            const { product } = await adminClient.query<
+                Codegen.GetProductWithVariantsQuery,
+                Codegen.GetProductWithVariantsQueryVariables
+            >(GET_PRODUCT_WITH_VARIANTS, {
+                id: 'T_4',
+            });
+            const variantWithFeaturedAsset = product?.variants.find(v => !!v.featuredAsset);
+            if (!variantWithFeaturedAsset) {
+                fail(`Could not find expected variant with a featuredAsset`);
+                return;
+            }
+            const { addItemToOrder } = await shopClient.query<
+                CodegenShop.AddItemToOrderMutation,
+                CodegenShop.AddItemToOrderMutationVariables
+            >(ADD_ITEM_TO_ORDER, {
+                productVariantId: variantWithFeaturedAsset.id,
+                quantity: 1,
+            });
+            orderResultGuard.assertSuccess(addItemToOrder);
+            expect(addItemToOrder.lines.length).toBe(1);
+            expect(addItemToOrder.lines[0].productVariant.id).toBe(variantWithFeaturedAsset?.id);
+            expect(addItemToOrder.lines[0].featuredAsset?.id).toBe(
+                variantWithFeaturedAsset.featuredAsset?.id,
+            );
+        });
+
+        it('sets OrderLine.featuredAsset to that of Product if ProductVariant has no featuredAsset', async () => {
+            const { product } = await adminClient.query<
+                Codegen.GetProductWithVariantsQuery,
+                Codegen.GetProductWithVariantsQueryVariables
+            >(GET_PRODUCT_WITH_VARIANTS, {
+                id: 'T_4',
+            });
+            const variantWithoutFeaturedAsset = product?.variants.find(v => !v.featuredAsset);
+            if (!variantWithoutFeaturedAsset) {
+                fail(`Could not find expected variant without a featuredAsset`);
+                return;
+            }
+            const { addItemToOrder } = await shopClient.query<
+                CodegenShop.AddItemToOrderMutation,
+                CodegenShop.AddItemToOrderMutationVariables
+            >(ADD_ITEM_TO_ORDER, {
+                productVariantId: variantWithoutFeaturedAsset.id,
+                quantity: 1,
+            });
+            orderResultGuard.assertSuccess(addItemToOrder);
+            expect(addItemToOrder.lines.length).toBe(2);
+            expect(addItemToOrder.lines[1].productVariant.id).toBe(variantWithoutFeaturedAsset?.id);
+            expect(addItemToOrder.lines[1].featuredAsset?.id).toBe(product?.featuredAsset?.id);
+        });
     });
 
     describe('ordering as authenticated user', () => {

+ 1 - 1
packages/core/src/service/helpers/order-modifier/order-modifier.ts

@@ -166,7 +166,7 @@ export class OrderModifier {
         }
 
         const productVariant = await this.getProductVariantOrThrow(ctx, productVariantId, order);
-        const featuredAssetId = productVariant.featuredAssetId ?? productVariant.featuredAssetId;
+        const featuredAssetId = productVariant.featuredAssetId ?? productVariant.product.featuredAssetId;
         const orderLine = await this.connection.getRepository(ctx, OrderLine).save(
             new OrderLine({
                 productVariant,