Преглед изворни кода

fix(core): Fix order line custom field comparison logic

Fixes #1670
Michael Bromley пре 3 година
родитељ
комит
dc3ea9cdc6

+ 88 - 24
packages/core/e2e/shop-order.e2e-spec.ts

@@ -381,6 +381,74 @@ describe('Shop orders', () => {
                 );
                 );
             });
             });
 
 
+            // https://github.com/vendure-ecommerce/vendure/issues/1670
+            it('adding a second item after adjusting custom field adds new OrderLine', async () => {
+                const { addItemToOrder: add1 } = await shopClient.query<AddItemToOrder.Mutation>(
+                    ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
+                    {
+                        productVariantId: 'T_3',
+                        quantity: 1,
+                    },
+                );
+                orderResultGuard.assertSuccess(add1);
+                expect(add1!.lines.length).toBe(2);
+                expect(add1!.lines[1].quantity).toBe(1);
+
+                const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
+                    orderLineId: add1.lines[1].id,
+                    quantity: 1,
+                    customFields: {
+                        notes: 'updated notes',
+                    },
+                });
+                expect(adjustOrderLine.lines[1].customFields).toEqual({
+                    lineImage: null,
+                    notes: 'updated notes',
+                });
+                const { activeOrder: ao1 } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
+                expect(ao1.lines[1].customFields).toEqual({
+                    lineImage: null,
+                    notes: 'updated notes',
+                });
+                const updatedNotesLineId = ao1.lines[1].id;
+
+                const { addItemToOrder: add2 } = await shopClient.query<AddItemToOrder.Mutation>(
+                    ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
+                    {
+                        productVariantId: 'T_3',
+                        quantity: 1,
+                    },
+                );
+                orderResultGuard.assertSuccess(add2);
+                expect(add2!.lines.length).toBe(3);
+                expect(add2!.lines[1].quantity).toBe(1);
+                expect(add2!.lines[2].quantity).toBe(1);
+
+                const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
+                expect(activeOrder.lines.find((l: any) => l.id === updatedNotesLineId)?.customFields).toEqual(
+                    {
+                        lineImage: null,
+                        notes: 'updated notes',
+                    },
+                );
+
+                // clean up
+                await shopClient.query<RemoveItemFromOrder.Mutation, RemoveItemFromOrder.Variables>(
+                    REMOVE_ITEM_FROM_ORDER,
+                    {
+                        orderLineId: add2!.lines[1].id,
+                    },
+                );
+                const { removeOrderLine } = await shopClient.query<
+                    RemoveItemFromOrder.Mutation,
+                    RemoveItemFromOrder.Variables
+                >(REMOVE_ITEM_FROM_ORDER, {
+                    orderLineId: add2!.lines[2].id,
+                });
+                orderResultGuard.assertSuccess(removeOrderLine);
+                expect(removeOrderLine.lines.length).toBe(1);
+            });
+
             it('addItemToOrder with relation customField', async () => {
             it('addItemToOrder with relation customField', async () => {
                 const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation>(
                 const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation>(
                     ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
                     ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
@@ -446,28 +514,6 @@ describe('Shop orders', () => {
 
 
             it('adjustOrderLine updates relation reference', async () => {
             it('adjustOrderLine updates relation reference', async () => {
                 const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
                 const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
-
-                const ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS = gql`
-                    mutation ($orderLineId: ID!, $quantity: Int!, $customFields: OrderLineCustomFieldsInput) {
-                        adjustOrderLine(
-                            orderLineId: $orderLineId
-                            quantity: $quantity
-                            customFields: $customFields
-                        ) {
-                            ... on Order {
-                                lines {
-                                    id
-                                    customFields {
-                                        notes
-                                        lineImage {
-                                            id
-                                        }
-                                    }
-                                }
-                            }
-                        }
-                    }
-                `;
                 const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
                 const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
                     orderLineId: activeOrder.lines[2].id,
                     orderLineId: activeOrder.lines[2].id,
                     quantity: 1,
                     quantity: 1,
@@ -565,7 +611,7 @@ describe('Shop orders', () => {
                 AdjustItemQuantity.Mutation,
                 AdjustItemQuantity.Mutation,
                 AdjustItemQuantity.Variables
                 AdjustItemQuantity.Variables
             >(ADJUST_ITEM_QUANTITY, {
             >(ADJUST_ITEM_QUANTITY, {
-                orderLineId: 'T_8',
+                orderLineId: 'T_10',
                 quantity: 101,
                 quantity: 101,
             });
             });
             orderResultGuard.assertErrorResult(adjustOrderLine);
             orderResultGuard.assertErrorResult(adjustOrderLine);
@@ -581,7 +627,7 @@ describe('Shop orders', () => {
                 AdjustItemQuantity.Mutation,
                 AdjustItemQuantity.Mutation,
                 AdjustItemQuantity.Variables
                 AdjustItemQuantity.Variables
             >(ADJUST_ITEM_QUANTITY, {
             >(ADJUST_ITEM_QUANTITY, {
-                orderLineId: 'T_8',
+                orderLineId: 'T_10',
                 quantity: 0,
                 quantity: 0,
             });
             });
             orderResultGuard.assertSuccess(adjustLine2);
             orderResultGuard.assertSuccess(adjustLine2);
@@ -2124,3 +2170,21 @@ export const ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS = gql`
     }
     }
     ${UPDATED_ORDER_FRAGMENT}
     ${UPDATED_ORDER_FRAGMENT}
 `;
 `;
+
+const ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS = gql`
+    mutation ($orderLineId: ID!, $quantity: Int!, $customFields: OrderLineCustomFieldsInput) {
+        adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity, customFields: $customFields) {
+            ... on Order {
+                lines {
+                    id
+                    customFields {
+                        notes
+                        lineImage {
+                            id
+                        }
+                    }
+                }
+            }
+        }
+    }
+`;

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

@@ -582,8 +582,14 @@ export class OrderModifier {
             for (const def of customFieldDefs) {
             for (const def of customFieldDefs) {
                 const key = def.name;
                 const key = def.name;
                 const existingValue = existingCustomFields?.[key];
                 const existingValue = existingCustomFields?.[key];
-                if (existingValue !== null && def.defaultValue && existingValue !== def.defaultValue) {
-                    return false;
+                if (existingValue != null) {
+                    if (def.defaultValue != null) {
+                        if (existingValue !== def.defaultValue) {
+                            return false;
+                        }
+                    } else {
+                        return false;
+                    }
                 }
                 }
             }
             }
             return true;
             return true;