1
0
Эх сурвалжийг харах

Merge branch 'minor' into major

Michael Bromley 3 жил өмнө
parent
commit
fac805d19a

+ 32 - 0
packages/core/e2e/order.e2e-spec.ts

@@ -217,6 +217,38 @@ describe('Orders resolver', () => {
             expect(result.order!.id).toBe('T_2');
         });
 
+        it('order with calculated line properties', async () => {
+            const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(
+                gql`
+                    query GetOrderWithLineCalculatedProps($id: ID!) {
+                        order(id: $id) {
+                            id
+                            lines {
+                                id
+                                linePriceWithTax
+                                quantity
+                            }
+                        }
+                    }
+                `,
+                {
+                    id: 'T_2',
+                },
+            );
+            expect(result.order!.lines).toEqual([
+                {
+                    id: 'T_3',
+                    linePriceWithTax: 167880,
+                    quantity: 1,
+                },
+                {
+                    id: 'T_4',
+                    linePriceWithTax: 791640,
+                    quantity: 3,
+                },
+            ]);
+        });
+
         it('sort by total', async () => {
             const result = await adminClient.query<
                 Codegen.GetOrderListQuery,

+ 22 - 22
packages/core/src/entity/order-line/order-line.entity.ts

@@ -38,7 +38,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
     @ManyToOne(type => Asset)
     featuredAsset: Asset;
 
-    @OneToMany(type => OrderItem, item => item.line)
+    @OneToMany(type => OrderItem, item => item.line, { eager: true })
     items: OrderItem[];
 
     @Index()
@@ -52,7 +52,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The price of a single unit, excluding tax and discounts.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get unitPrice(): number {
         return this.firstActiveItemPropOr('unitPrice', 0);
     }
@@ -61,7 +61,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The price of a single unit, including tax but excluding discounts.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get unitPriceWithTax(): number {
         return this.firstActiveItemPropOr('unitPriceWithTax', 0);
     }
@@ -70,7 +70,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * Non-zero if the `unitPrice` has changed since it was initially added to Order.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get unitPriceChangeSinceAdded(): number {
         const firstItem = this.activeItems[0];
         if (!firstItem) {
@@ -87,7 +87,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * Non-zero if the `unitPriceWithTax` has changed since it was initially added to Order.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get unitPriceWithTaxChangeSinceAdded(): number {
         const firstItem = this.activeItems[0];
         if (!firstItem) {
@@ -109,7 +109,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * correct price to display to customers to avoid confusion
      * about the internal handling of distributed Order-level discounts.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get discountedUnitPrice(): number {
         return this.firstActiveItemPropOr('discountedUnitPrice', 0);
     }
@@ -118,7 +118,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The price of a single unit including discounts and tax
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get discountedUnitPriceWithTax(): number {
         return this.firstActiveItemPropOr('discountedUnitPriceWithTax', 0);
     }
@@ -129,7 +129,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * Order-level discounts. This value is the true economic value of the OrderItem, and is used in tax
      * and refund calculations.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get proratedUnitPrice(): number {
         return this.firstActiveItemPropOr('proratedUnitPrice', 0);
     }
@@ -138,17 +138,17 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The `proratedUnitPrice` including tax.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get proratedUnitPriceWithTax(): number {
         return this.firstActiveItemPropOr('proratedUnitPriceWithTax', 0);
     }
 
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get quantity(): number {
         return this.activeItems.length;
     }
 
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get adjustments(): Adjustment[] {
         return this.activeItems.reduce(
             (adjustments, item) => [...adjustments, ...(item.adjustments || [])],
@@ -156,12 +156,12 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
         );
     }
 
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get taxLines(): TaxLine[] {
         return this.firstActiveItemPropOr('taxLines', []);
     }
 
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get taxRate(): number {
         return this.firstActiveItemPropOr('taxRate', 0);
     }
@@ -170,7 +170,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The total price of the line excluding tax and discounts.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get linePrice(): number {
         return summate(this.activeItems, 'unitPrice');
     }
@@ -179,7 +179,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The total price of the line including tax but excluding discounts.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get linePriceWithTax(): number {
         return summate(this.activeItems, 'unitPriceWithTax');
     }
@@ -188,7 +188,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The price of the line including discounts, excluding tax.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get discountedLinePrice(): number {
         return summate(this.activeItems, 'discountedUnitPrice');
     }
@@ -197,12 +197,12 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The price of the line including discounts and tax.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get discountedLinePriceWithTax(): number {
         return summate(this.activeItems, 'discountedUnitPriceWithTax');
     }
 
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get discounts(): Discount[] {
         const priceIncludesTax = this.items?.[0]?.listPriceIncludesTax ?? false;
         // Group discounts together, so that it does not list a new
@@ -232,7 +232,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The total tax on this line.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get lineTax(): number {
         return summate(this.activeItems, 'unitTax');
     }
@@ -243,7 +243,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * Order-level discounts. This value is the true economic value of the OrderLine, and is used in tax
      * and refund calculations.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get proratedLinePrice(): number {
         return summate(this.activeItems, 'proratedUnitPrice');
     }
@@ -252,12 +252,12 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The `proratedLinePrice` including tax.
      */
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get proratedLinePriceWithTax(): number {
         return summate(this.activeItems, 'proratedUnitPriceWithTax');
     }
 
-    @Calculated()
+    @Calculated({ relations: ['items'] })
     get proratedLineTax(): number {
         return summate(this.activeItems, 'proratedUnitTax');
     }