Skip to content

Commit

Permalink
fix(core): Fix regression in correctly setting OrderLine.featuredAsset
Browse files Browse the repository at this point in the history
v3.0.2 introduced a bug which caused the OrderLine.featuredAsset not to
get set if the ProductVariant did not have a featuredAsset
  • Loading branch information
michaelbromley committed Sep 11, 2024
1 parent dcf50fc commit 7d070f2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/core/e2e/fixtures/e2e-products-full.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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 , ,
Expand Down
19 changes: 19 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3351,6 +3351,7 @@ export type UpdatedOrderFragment = {
linePrice: number;
linePriceWithTax: number;
productVariant: { id: string };
featuredAsset?: { id: string } | null;
discounts: Array<{
adjustmentSource: string;
amount: number;
Expand Down Expand Up @@ -3395,6 +3396,7 @@ export type AddItemToOrderMutation = {
linePrice: number;
linePriceWithTax: number;
productVariant: { id: string };
featuredAsset?: { id: string } | null;
discounts: Array<{
adjustmentSource: string;
amount: number;
Expand Down Expand Up @@ -3429,6 +3431,7 @@ export type AddItemToOrderMutation = {
linePrice: number;
linePriceWithTax: number;
productVariant: { id: string };
featuredAsset?: { id: string } | null;
discounts: Array<{
adjustmentSource: string;
amount: number;
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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' },
Expand Down
3 changes: 3 additions & 0 deletions packages/core/e2e/graphql/shop-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export const UPDATED_ORDER_FRAGMENT = gql`
unitPriceWithTax
linePrice
linePriceWithTax
featuredAsset {
id
}
discounts {
adjustmentSource
amount
Expand Down
54 changes: 54 additions & 0 deletions packages/core/e2e/shop-order.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7d070f2

Please sign in to comment.