Browse Source

fix(core): Fix OrderLine deduplication with customField default values

Fixes #1612
Michael Bromley 3 years ago
parent
commit
9522f349a4

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

@@ -130,6 +130,7 @@ describe('Shop orders', () => {
                     { name: 'notes', type: 'string' },
                     { name: 'privateField', type: 'string', public: false },
                     { name: 'lineImage', type: 'relation', entity: Asset },
+                    { name: 'dropShip', type: 'boolean', defaultValue: false },
                 ],
             },
             orderOptions: {

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

@@ -574,12 +574,20 @@ export class OrderModifier {
         inputCustomFields: { [key: string]: any } | null | undefined,
         existingCustomFields?: { [key: string]: any },
     ): Promise<boolean> {
+        const customFieldDefs = this.configService.customFields.OrderLine;
         if (inputCustomFields == null && typeof existingCustomFields === 'object') {
             // A null value for an OrderLine customFields input is the equivalent
-            // of every property of an existing customFields object being null.
-            return Object.values(existingCustomFields).every(v => v === null);
+            // of every property of an existing customFields object being null
+            // or equal to the defaultValue
+            for (const def of customFieldDefs) {
+                const key = def.name;
+                const existingValue = existingCustomFields?.[key];
+                if (existingValue !== null && def.defaultValue && existingValue !== def.defaultValue) {
+                    return false;
+                }
+            }
+            return true;
         }
-        const customFieldDefs = this.configService.customFields.OrderLine;
 
         const customFieldRelations = customFieldDefs.filter(d => d.type === 'relation');
         let lineWithCustomFieldRelations: OrderLine | undefined;
@@ -600,7 +608,9 @@ export class OrderModifier {
                 const valuesMatch =
                     JSON.stringify(inputCustomFields?.[key]) === JSON.stringify(existingValue);
                 const undefinedMatchesNull = existingValue === null && inputCustomFields?.[key] === undefined;
-                if (!valuesMatch && !undefinedMatchesNull) {
+                const defaultValueMatch =
+                    inputCustomFields?.[key] === undefined && def.defaultValue === existingValue;
+                if (!valuesMatch && !undefinedMatchesNull && !defaultValueMatch) {
                     return false;
                 }
             } else if (def.type === 'relation') {